Git Cheat Sheet


A Git cheat sheet saves you from learning all the commands by heart.

Basics

git init <directory> Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
git clone <repo> Clone repo located at <repo> onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.
git add <directory> Stage all changes in <directory> for the next commit. Replace <directory> with a <file> to change a specific file. Use . to add every file.
git status List which files are staged, unstaged, and untracked.
git commit -m "My message" Commit the staged snapshot
git rm <file> Remove file
git mv <file-original> <file-renamed> Changes the file name and prepares it for commit
git checkout -- <file> Undo modifications (restore files from latest commited version)
git checkout <hash> -- <file> Restore file from a custom commit (in current branch):

Undoing Changes

git revert <hash> Go back to commit
git reset --soft <hash> Soft reset (move HEAD only; neither staging nor working dir is changed)
git reset --soft HEAD~ Undo latest commit

Update & Delete

git clean -n Test-delete untracked files
git clean -f Delete untracked files (not staging)
git reset HEAD <file> Unstage (undo adds)
git commit --amend -m "Message" Commit to most recent commit
git commit --amend -m "New Message" Update most recent commit message:

Branches

git branch Show branches
git branch branchname Create branch
git checkout branchname Change to branch
git checkout -b branchname Create and change to new branch
git branch --move <branch> <new branch name> Rename branch
git merge <branch> True merge (fast forward)
git merge --abort Stop merge (in case of conflicts)

Stashing

git stash save "Message" Put in stash
git stash list Show stash
git stash pop stash@{0} Use custom stash item and drop it
git stash apply stash@{0} Use custom stash item and do not drop it
git stash drop stash@{0} Delete custom stash item
git stash clear Delete complete stash

Gitignore & Gitkeep

open .gitignore Add or edit gitignore
touch dir/.gitkeep Track empty dir

Logging

git log Show commits
git log -p Show changes

Comparing

git diff Compare modified files
git diff --color-words <file> Compare modified files and highlight changes only
git diff --staged Compare modified files within the staging area
git diff master..branchname Compare branches
git diff --color-words master..branchname^ Compare branches like above
git diff <hash> Compare commits
git diff <hash>..HEAD  
git diff <old hash>..<new hash>  

Tagging

git tag Show all released versions
git tag v1.0.0 Create release version
git checkout v1.0.0 Checkout a specific release version:

Collaborate

git remote Show remote
git remote -v Show remote details
git remote add origin <remote> Add remote origin from a project
git remote rm origin Remove origin
git branch -r Show remote branches
git branch -a Show all branches
git pull Pull
git pull origin <branch Pull specific branch:
Need a simple way to do smarter deployments? Take a look at GitFTP-Deploy. Try free for 7 days.