A Git Cheat Sheet For That Quick Reminder
Switching from subversion to Git was a paradigm shift that took some getting use to, but looking back now, the switch was worth it. Rather than getting tied to any one Git UI, I wanted to get a grasp of plain old Git and the commands it offers.
With that, I’ve created this Git Cheat Sheet to help along the way.
Cloning a Repository
Notes | Commands |
---|---|
clone an existing repository | git clone "repository-path" |
create a new local repository | git init |
Branch Management
Notes | Commands |
---|---|
list local branches | git branch |
list local and remote branches | git branch -av |
create local branch | git branch "branch-name" |
create and checkout local branch | git checkout -b "branch-name" |
delete local branch that has had its changes pushed to a remote branch (cannot currently be checked out) | git branch -d "branch-name" |
delete local branch, ignoring whether or not its changes were pushed to a remote branch | git branch -D "branch-name" |
delete local and remote branch | git push origin --delete "branch-name" |
checkout branch | git checkout "branch-name" |
rename currently checked out local branch | git branch -m "new-branch-name" |
Handling Changes
Notes | Commands |
---|---|
see changes made to local branch | git status |
track and stage local changes | git add . |
commit staged only with a commit message | git commit -m "commit-message" |
commit unstaged and staged with a commit message | git commit -a -m "commit-message" |
amend changes to previous commit (before pushing) | git commit --amend |
temporarily stash local changes (resets local branch but preserves changes) | git stash |
apply previously stashed changes to the current branch | git stash pop |
Undoing (oops)
Notes | Commands |
---|---|
discard all changes in working directory | git reset --hard |
reset HEAD to a specific commit and discard local changes | git reset --hard |
reset HEAD to a specific commit but preserve local changes | git reset "commit-hash" |
discard all changes to specific file | git checkout HEAD |
revert commit by applying previous commit changes to HEAD then committing | git revert |
Tracking History
Notes | Commands |
---|---|
show all commits for current HEAD | git log |
show all commits for current HEAD for given file | git log -p "file" |
show all commits that HEAD has pointed to | git reflog |
Fetch, Pull & Push
Notes | Commands |
---|---|
grab changes from remote, but don't merge into current HEAD | git fetch |
grab changes from remote, don't merge, and clean-up remote references (known as pruning, prune) | git fetch -p |
grab changes and merge into current HEAD | git pull |
create and track remote branch, push all local changes to it | git push -u "remote" "branch-name" |
push all local changes to tracked remote branch for current HEAD | git push |
Merge & Rebase
Notes | Commands |
---|---|
merge commits from branch into current HEAD | git merge "source-branch-name" |
merge commits from source branch into target branch | git merge "source-branch-name" "target-branch-name" |
rebase current HEAD onto branch (e.g. make that branch the new base of your changes) | git rebase "branch" |
Hi, I’m Sam.
I’m a programmer and a DIYer. When I’m not finding things to build I enjoy cooking, hiking, camping and traveling the world with my best friend. Say Hello!