Comprehensive reference for git commands cheatsheet covering essential concepts, practical examples, and production best practices.
Git is a distributed version control system used to track changes in source code during software development. This reference page compiles the most essential Git commands, their exact syntax, and common usage patterns for engineers working with Git daily. It is structured around the tasks engineers perform, from setting up a repository to resolving merge conflicts and managing branches.
Start a new project or prepare an existing directory for version control.
git init
# Initialize with a specific template directory
git init --template=/path/to/template
# Set user identity (applies to all repositories)
git config --global user.name "Alice Johnson"
git config --global user.email "[email protected]"
# Set default branch name (default is 'master', but 'main' is now common)
git config --global init.defaultBranch main
# Configure editor for commit messages
git config --global core.editor vim
The git init command creates a .git directory with subdirectories for objects, refs, and configuration. The --template flag allows customizing the initial structure of the repository. The core.editor setting defines which editor opens when writing a commit message.
Track changes in files and prepare them for commit.
# Add a single file to the staging area
git add README.md
# Add all files (tracked and untracked)
git add .
# Add all changes in a specific directory
git add src/
# Add changes from a specific file, but also stage deleted files
git add -u
# Add all changes including new files and modifications, but skip deleted files
git add -A
# Stage specific lines or hunks interactively
git add -p
# Stage changes in a file with interactive prompts
git add -i
The git add -u stages only modified and deleted files, while git add -A includes new, modified, and deleted. The -p flag enables patch-based staging, allowing the user to select which hunks of a file to stage. The -i flag provides an interactive menu, ideal for complex staging workflows.
Capture changes in the staging area as a version.
# Commit all staged changes with a message
git commit -m "Add user authentication"
# Commit with a message from a file
git commit -F commit-message.txt
# Amend the last commit (add changes, edit message, or both)
git commit --amend
# Amend with a new message and include changes from the index
git commit --amend -m "Fix login redirect logic"
# Create a commit without staging first (stages all changes)
git commit -a -m "Update documentation"
The --amend flag updates the most recent commit. If no changes are staged, it includes all modified files. If changes are staged, it combines staged changes with the amended commit. The -a flag automatically stages all tracked files before committing.
Manage parallel lines of development.
# Create a new branch and switch to it
git checkout -b feature/user-profile
# Create a new branch without switching
git branch feature/email-notifications
# Switch between branches
git checkout main
# Switch to the previous branch
git checkout -
# Delete a local branch
git branch -d feature/login-flow
# Delete a branch and force delete if not merged
git branch -D feature/sso
# Merge a branch into the current branch
git merge feature/user-profile
# Merge with fast-forward only (no commit if already up-to-date)
git merge --ff-only main
# Merge with a merge commit, even if fast-forwardable
git merge --no-ff feature/new-ui
# Merge with a merge commit and a custom message
git merge -m "Merge feature: new dashboard" feature/dashboard
Branches are lightweight pointers to commits. The checkout -b command creates and checks out a new branch in one step. The merge --ff-only option ensures a clean history when the current branch is a direct ancestor of the target branch. The --no-ff flag forces a merge commit, preserving the branch’s lineage.
Inspect the state of the repository.
# Show the most recent commits
git log
# Show commits with a compact, one-line format
git log --oneline
# Show commits with a graph of branches
git log --oneline --graph
# Show commits since a specific date
git log --since="2 weeks ago"
# Show commits affecting a specific file
git log --follow -- src/main.py
# Show changes between two commits
git diff HEAD~3 HEAD
# Show changes between the working directory and the staging area
git diff
# Show changes between the staging area and the last commit
git diff --staged
# Show changes in the working directory only
git diff --stat
# Show the status of tracked, untracked, and staged files
git status -s
The --follow flag traces a file’s history across renames. The -s flag produces a concise, short-form output: M for modified, A for added, D for deleted. git diff --staged is equivalent to git diff --cached.
Handle conflicts during a merge.
# Perform a merge that produces conflicts
git merge feature/user-profile
# When merge fails, edit conflict files manually
# Conflict markers look like:
<<<<<<< HEAD
This line is from the current branch.
=======
This line is from the feature branch.
>>>>>>> feature/user-profile
# Resolve conflicts, then stage the resolved files
git add src/user.py
# After resolving all conflicts, complete the merge
git commit
# Continue a merge after resolving conflicts
git merge --continue
# Abort a merge in progress
git merge --abort
# Use external merge tool (e.g., vimdiff, meld, kdiff3)
git mergetool
Merge conflicts are resolved by editing files with <<<<<<<, =======, and >>>>>>> markers. After editing, each file must be added to the staging area. The merge --continue command commits the merge after all conflicts are resolved. The merge --abort command undoes the entire merge process.
Push and pull changes between local and remote repositories.
# Add a remote repository
git remote add origin https://github.com/alice/project.git
# List all remotes
git remote -v
# Push to a remote branch
git push origin main
# Push all local branches to origin
git push --all origin
# Push a specific branch to remote
git push origin feature/user-profile
# Set upstream branch (tracks remote branch)
git push --set-upstream origin feature/user-profile
# Fetch updates from a remote without merging
git fetch origin
# Pull updates from remote (fetch + merge)
git pull origin main
# Pull with rebase instead of merge
git pull --rebase origin main
# Push with force, overwriting remote history
git push --force origin main
# Push with force, but only if the remote branch has not changed
git push --force-with-lease origin main
The --set-upstream flag establishes a tracking relationship between a local branch and a remote branch. The --force-with-lease option is safer than --force, as it checks that the remote branch has not been updated since the last fetch.
Common patterns for team and release workflows.
# Create a feature branch from a release branch
git checkout -b feature/new-login-flow release/v1.2
# Rebase feature branch onto main
git checkout feature/new-login-flow
git rebase main
# Squash multiple commits into one
git rebase -i HEAD~3
# Interactive rebase prompt:
# pick 123abc4 Add login form
# squash 567def8 Add validation rules
# squash 901abc2 Add test cases
# edit 234def5 Update documentation
#
# Save and exit to apply changes.
# After rebase, push with force
git push --force-with-lease origin feature/new-login-flow
# Create a tag for a release
git tag -a v1.0.0 -m "Release version 1.0.0"
# Push tags to remote
git push origin --tags
# Delete a tag locally and remotely
git tag -d v0.9.0
git push origin --delete v0.9.0
Interactive rebasing (-i) allows squashing, editing, reordering, and dropping commits. The tag -a command creates an annotated tag with a message. The --tags option pushes all local tags to the remote.
Silent pitfalls that engineers encounter.
git push fails with error: src refspec main does not match any when a branch does not exist locally.git pull merges changes but leaves the working directory dirty; use git pull --rebase to keep history linear.git add, a file is staged but git commit includes only changes from the staging area, not the working directory.git merge creates a merge commit even when fast-forwarding; use --ff-only to avoid it.--force-with-lease option prevents accidental overwrites during git push.git rebase can introduce conflicts that must be resolved in the same interactive session.git checkout -b new-branch creates a branch but does not switch to it if the branch already exists.Common configuration values used across projects.
# .git/config
[core]
editor = vim
autocrlf = true
whitespace = fix
[branch]
autosetupmerge = true
[remote "origin"]
url = https://github.com/alice/project.git
fetch = +refs/heads/*:refs/remotes/origin/*
[merge]
tool = vimdiff
commit = true
The autocrlf = true setting converts line endings automatically between Windows and Unix. The whitespace = fix setting trims trailing whitespace and flags trailing spaces in diffs. The autosetupmerge flag enables automatic tracking of upstream branches.
This page was rewritten on 14 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy