Git Command Line Cheat Sheet
Here’s a list of Git commands I use on a regular basis. I also tend to forget many of them regularly so it helps to keep them in one place for easy reference.
If you have oh-my-zsh installed it comes with an assortment of Git aliases that covers many of the commands listed below.
Setup
Add a remote repository:
git remote add origin git@github.com:username/my-repo.gitPush and set up an upstream reference for a branch (first time only):
git push -u origin my-branchCommitting
Interactively stage changes hunk-by-hunk:
git add --patchAmend the most recent commit:
git commit --amendAmend the most recent commit without editing the message:
git commit --amend --no-editBranching
Create and check out a new branch:
git checkout -b my-branchChecks out a remote branch:
git fetch && git checkout my-branchDelete a branch locally:
git branch -d my-branchDelete a branch remotely:
git push origin :my-branchList remote branches:
git branch -rRename a branch:
git branch -m current-branch-name new-branch-nameStashing
Save your current changes in a new stash with a message:
git stash save my-stashList your stashes:
git stash listRestore a stash at index 0:
git stash apply 0Delete a stash at index 0:
git stash drop 0Tags
Create a new tag:
git tag 1.0.0Push all tags to origin:
git push origin --tagsDelete a tag locally:
git tag -d 1.0.0Delete a tag remotely:
git push origin :refs/tags/1.0.0Cleaning up
Clear all uncommitted changes:
git reset --hardDelete all untracked files and directories:
git clean -f -dVisualization
Display a graphical one-line-per-commit representation of all branches and commits:
git log --oneline --decorate --graph --allDisplay the commits that contain the matching text (use -i to ignore case and --no-merges to exclude merges):
git log -p -S"console.log"Display the commits and changes associated with a specific file:
git log -p README.mdDisplay the commits and changes associated with a file after a certain date:
git log -p --after="2 weeks ago" README.mdFind a breaking commit with bisect
Begin a bisect session:
git bisect startMark the current commit as broken:
git bisect badMark the current commit as healthy, and check out the next testable commit:
git bisect goodTest the commit and run git bisect bad or git bisect good. Keep testing and marking until you are left with the commit that first introduced the bug.
Reset your repository to the original state:
git bisect reset


