Git Commands Cheat Sheet
A dense, reference-style guide to the Git commands you use every day, organized by task
Git at a Glance
Git is a distributed version control system that tracks changes to files, lets you work on parallel lines of development with branches, and synchronizes work between collaborators through remotes. This cheat sheet groups the most useful commands by task so you can find what you need fast. Each command is shown verbatim with a one-line explanation. Angle brackets like <branch> denote placeholders you replace with your own values.
Setup & Configuration
Identify yourself and customize Git before your first commit
git config --global user.name "Your Name"
Set the author name attached to your commits for every repository on this machine.
git config --global user.email "[email protected]"
Set the author email used on commits globally; omit --global to set it only for the current repo.
git config user.email "[email protected]"
Override the email for the current repository only (local config takes precedence over global).
git config --global init.defaultBranch main
Make new repositories start with a branch named main instead of master.
git config --global core.editor "code --wait"
Set the editor Git opens for commit messages and interactive sessions.
git config --list --show-origin
List all effective configuration values and which file each came from.
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --decorate"
Create shortcuts so git co runs git checkout and git lg shows a compact graph.
Starting a Repository
Create a new repo or copy an existing one
git init
Initialize a new, empty Git repository in the current directory.
git init my-project
Create the directory my-project and initialize a repository inside it.
git clone https://github.com/user/repo.git
Copy a remote repository, including its full history, into a new local directory.
git clone https://github.com/user/repo.git my-dir
Clone into a specific directory name instead of the repository's default name.
git clone --depth 1 https://github.com/user/repo.git
Shallow clone with only the latest commit to save time and bandwidth.
Staging & Committing
Move changes from the working tree to the staging area and into history
git status
Show which files are staged, modified, or untracked.
git status -s
Show the same information in a compact, two-column short format.
git add file.txt
Stage a specific file so its changes will be included in the next commit.
git add .
Stage all changes in the current directory and below, including new files.
git add -p
Interactively review and stage individual hunks, letting you split a file's changes across commits.
git commit -m "Add login form validation"
Record the staged changes with a short message in one step.
git commit
Commit staged changes, opening your editor to write a full multi-line message.
git commit -am "Quick fix"
Stage all tracked, modified files and commit them in a single command (does not add new files).
git commit --amend
Replace the most recent commit, letting you edit its message or add newly staged changes. Avoid on commits already pushed and shared.
git commit --amend --no-edit
Add staged changes to the last commit while keeping its existing message.
Inspecting History & Changes
See what changed, when, and by whom
git log
Show the full commit history with author, date, and message.
git log --oneline --graph --decorate --all
Display a compact, one-line-per-commit ASCII graph of all branches and refs.
git log -p file.txt
Show the commit history of a file along with the diff introduced by each commit.
git diff
Show unstaged changes between your working tree and the staging area.
git diff --staged
Show changes that are staged and will be in the next commit (also --cached).
git diff main..feature
Show the differences between two branches or commits.
git show <commit>
Display the metadata and full diff of a single commit.
git blame file.txt
Annotate each line of a file with the commit and author that last changed it.
Branching
Work on parallel lines of development
git branch
List local branches; the current one is marked with an asterisk.
git branch -a
List all branches, including remote-tracking branches.
git branch feature-x
Create a new branch at the current commit without switching to it.
git switch feature-x
Switch to an existing branch (modern replacement for git checkout <branch>).
git switch -c feature-x
Create a new branch and switch to it in one step.
git checkout -b feature-x
Older equivalent of switch -c: create and check out a new branch.
git branch -d feature-x
Delete a branch that has already been merged; use -D to force-delete an unmerged branch.
git branch -m old-name new-name
Rename a branch; omit the old name to rename the current branch.
Merging & Rebasing
Combine work from different branches
git merge feature-x
Merge feature-x into the current branch, creating a merge commit when histories diverge.
git merge --no-ff feature-x
Always create a merge commit even when a fast-forward is possible, preserving branch topology.
git rebase main
Replay the current branch's commits on top of main, producing a linear history.
git rebase -i HEAD~3
Interactively reorder, squash, edit, or drop the last three commits. Rewrites history, so avoid on shared branches.
git merge --abort
Cancel an in-progress merge and restore the pre-merge state.
git rebase --abort
Cancel an in-progress rebase and return to the original branch state.
Resolving Conflicts
When Git stops on a conflict, edit the marked files (<<<<<<<, =======, >>>>>>>) to keep the desired result, then continue.
git status
Identify which files are unmerged and still need resolution.
git add resolved-file.txt
Mark a conflicted file as resolved by staging it.
git rebase --continue # or: git merge --continue
Resume the operation once all conflicts are staged.
Working with Remotes
Synchronize your repository with servers and collaborators
git remote -v
List configured remotes and their fetch/push URLs.
git remote add origin https://github.com/user/repo.git
Add a new remote named origin pointing at the given URL.
git fetch origin
Download new commits and refs from the remote without changing your working branch.
git pull
Fetch from the tracked remote branch and merge (or rebase) it into the current branch.
git pull --rebase
Fetch and rebase local commits on top of the remote branch instead of merging.
git push
Upload local commits on the current branch to its tracked remote branch.
git push -u origin feature-x
Push a branch and set it as the upstream so later git push/pull need no arguments.
git push --force-with-lease
Force-push safely: overwrites the remote branch only if no one else has pushed since your last fetch. Prefer this over --force.
Undoing Changes
Discard, unstage, reset, or reverse work
git restore file.txt
Discard unstaged changes in a file, restoring it from the index.
git restore --staged file.txt
Unstage a file, keeping its working-tree changes intact.
git reset --soft HEAD~1
Undo the last commit but keep its changes staged.
git reset --mixed HEAD~1
Undo the last commit and unstage its changes, leaving them in the working tree (the default).
git reset --hard HEAD~1
Undo the last commit and discard all its changes. Destructive: lost working-tree edits cannot be recovered.
git revert <commit>
Create a new commit that reverses a previous one, safe for shared/published history.
git clean -fd
Remove untracked files and directories; add -n first to preview what would be deleted.
Stashing
Set aside in-progress work without committing
git stash
Save tracked, modified changes and revert the working tree to a clean state.
git stash push -m "wip: refactor parser"
Stash changes with a descriptive label for easier identification later.
git stash -u
Include untracked files in the stash as well.
git stash list
Show all saved stashes with their references (e.g. stash@{0}).
git stash pop
Reapply the most recent stash and remove it from the stash list.
git stash apply stash@{1}
Reapply a specific stash while keeping it in the list.
git stash drop stash@{0}
Delete a single stash entry without applying it.
Tags
Mark release points and important commits
git tag
List all tags in the repository.
git tag v1.0.0
Create a lightweight tag pointing at the current commit.
git tag -a v1.0.0 -m "Release 1.0.0"
Create an annotated tag with a message, author, and date (preferred for releases).
git push origin v1.0.0
Push a single tag to the remote (tags are not pushed by default).
git push --tags
Push all local tags to the remote at once.
Useful Extras
Powerful commands worth keeping in your toolkit
git cherry-pick <commit>
Apply the changes from a specific commit onto the current branch.
git reflog
Show the history of where HEAD has pointed; invaluable for recovering "lost" commits after a bad reset or rebase.
git bisect start
git bisect bad
git bisect good v1.0.0
Binary-search through history to find the commit that introduced a bug; Git checks out midpoints for you to test until you run git bisect reset.
echo "node_modules/" >> .gitignore
List patterns in a .gitignore file so matching untracked files are never staged. Already-tracked files must be removed with git rm --cached.
Common Workflows
End-to-end command sequences for everyday tasks
Feature Branch Flow
Develop a feature in isolation, then merge it back into the main line once it is reviewed.
git switch main
git pull
git switch -c feature/new-search
# ... edit files ...
git add -p
git commit -m "Add fuzzy search to results page"
git push -u origin feature/new-search
# open a pull request, get review, then merge
Syncing a Fork
Keep your fork up to date with the original ("upstream") repository.
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git switch main
git merge upstream/main # or: git rebase upstream/main
git push origin main
Tips & Gotchas
- Never rewrite shared history: avoid
commit --amend,rebase, andreset --hardon commits others have already pulled. - Prefer
--force-with-leaseover--force: it refuses to overwrite remote work you have not seen. - Lost a commit? Check
git reflog— almost anything committed in the last 30+ days is recoverable. resetvsrevert: usereseton private branches andreverton published ones.- Stage in pieces:
git add -pkeeps commits small and focused.
Related Resources
Continue building your development skills
Programming Resources
Tutorials, guides, and references spanning languages, tools, and software engineering practices.
Explore ResourcesData Science with Python Cheat Sheet
Quick reference for NumPy, pandas, and the core Python data-analysis workflow.
View Cheat SheetUI Design Cheat Sheet
Layout, typography, color, and spacing principles for building clean interfaces.
View Cheat SheetFull-Stack Development Course
Learn to build complete web applications with version control woven throughout.
View Course