Essential Git Commands and Concepts

Configuration

Git uses a series of configuration files to store settings. The scope of these configurations can be system-wide, user-specific, or repository-specific.

  • git config --system <key> <value>: Sets a configuration value in the system-wide file, typically located at /etc/gitconfig.
  • git config --global <key> <value>: Sets a configuration value for the current user in their home directory (~/.gitconfig). This overrides system settings.
  • git config <key> <value>: Sets a configuration value specific to the current repository in .git/config. This overrides both system and global settings.
  • git config --list: Displays all configuration settings.

Repository Structure

  • Working Directory: The project's files on your local machine.
  • Staging Area (Index): A file (.git/index) that stores information about what will go into the next commit. It acts as an intermediate area.
  • Repository: The .git directory, which contains all the metadata and object database for your project's history.

File States

Files in a Git repository can be in one of several states:

  • Untracked: A new file not yet known to Git. Use git add to start tracking it.
  • Tracked: A file known to Git. Tracked files can be:
    • Unmodified: The file matches the latest commit. Editing it changes its state to Modified.
    • Modified: The file has been changed since the last commit. Use git add to stage it.
    • Staged: The file is marked to be included in the next commit. Use git commit to save it to the repository. After committing, it becomes Unmodified.

Basic Operations

  • git init: Initializes a new, empty Git repository in the current directory.
  • git clone <url>: Creates a local copy of a remote repository.
  • git add <file>: Stages a specific file for the next commit.
  • git add .: Stages all changes in the current driectory for the next commit.
  • git commit -m "message": Commits the staged changes to the local repository with a descriptive message.
  • git commit -am "message": A shortcut to commit all modified and tracked files, skipping the staging step.
  • git status: Shows the status of the working directory and staging area.
  • git status -s: Shows a concise, short status output.
  • git reset HEAD: Unstages all files from the staging area.
  • git reset HEAD <file>: Unstages a specific file from the staging area.
  • git diff: Shows unstaged changes between the working directory and the staging area.
  • git diff --stat: Shows a summary of unstaged changes.
  • git diff --cached: Shows staged changes between the staging area and the last commit.
  • git diff HEAD: Shows all changes (staged and unstaged) between the working directory and the last commit.
  • git rm -r -f --cached <file>: Removes a file from the repository's tracking list without deleting it from the working directory. The -r flag is for recursiev removal, -f forces the operation.
  • git mv <source> <destination>: Moves or renames a file and stages the change.

Branch Management

  • git branch: Lists all local branches. The current branch is highlighted.
  • git branch <branchname>: Creates a new branch.
  • git branch -d <branchname>: Deletes a branch. The branch must be fully merged.
  • git checkout <branchname>: Switches to the specified branch.
  • git checkout -b <branchname>: Creates a new branch and immediately switches to it.
  • git merge <branchname>: Merges the specified branch into the current branch.
  • Merge Conflicts: Occur when changes in the branches being merged overlap. Git will pause and mark the conflicting files. You must manually resolve the conflicts, then stage and commit the resolved files.

Viewing History

  • git log: Displays the commit history for the current branch.
  • git log --oneline: Shows a condensed, one-line-per-commit history.
  • git log --oneline --decorate --graph: Displays a graph of the commit history with branch and tag labels.
  • git log --reverse --oneline: Shows the commit history in reverse chronological order.
  • git log --author="Author Name" --oneline -5: Shows the last 5 commits by a specific author.
  • git log --oneline --before="3 weeks ago" --after="2020-01-01" --no-merges: Shows commits within a date range, excluding merge commits.

Tagging

Tags are used to mark specific points in history, often for releases.

  • git tag: Lists all tags in the repository.
  • git tag <tagname>: Creates a lightweight tag at the current commit.
  • git tag -a <tagname> -m "message" <commit-id>: Creates an annotated tag with a message at a specific commit.
  • git tag -s <tagname> -m "message" <commit-id>: Creates a signed tag with a GPG signature.

Remote Repositories

  • git remote add <shortname> <url>: Adds a new remote repository and assigns it a short name.
  • git remote -v: Lists all configured remote repositories and their URLs.
  • git remote rm <shortname>: Removes a remote repository configuration.
  • git push <shortname> <branch>: Pushes local commits to the specified remote branch.
  • git fetch <shortname>: Downloads changes from the remote repository but does not merge them into the current branch.
  • git pull <shortname> <branch>: A combination of git fetch and git merge that pulls and integrates changes from a remote branch.

Additional Tips

  • git tag -d <tagName>: Deletes a local tag.
  • git push origin :refs/tags/<tagName>: Deletes a remote tag.
  • git push -f: Force-pushes changes, overwriting the remote branch. Use with extreme caution, as it can rewrite history for other collaborators.
  • git config --global core.autocrlf false: Disables automatic CRLF (Carriage Return Line Feed) to LF (Line Feed) conversion, which can be useful for cross-platform projects.

Tags: Git version-control Branching tagging remote-repositories

Posted on Sun, 04 Oct 2026 16:40:37 +0000 by backyard