Git Cheat Sheet: The Essential Git Commands Every Developer Must Know
Git Cheat Sheet: The Essential Git Commands Every Developer Must Know
✓ Creating snapshots
✓ Browsing history
✓ Rewriting history
Table of Content
Creating Snapshots 6
Browsing History 8
Branching & Merging 10
Collaboration 12
Rewriting History 13
Creating Snapshots
Initializing a repository
git init
Staging files
git add file1.js # Stages a single file
git add file1.js file2.js # Stages multiple files
git add *.js # Stages with a pattern
git add . # Stages the current directory and all its content
Committing the staged files
git commit -m “Message” # Commits with a one-line message
git commit # Opens the default editor to type a long message
Removing files
git rm file1.js # Removes from working directory and staging area
git rm --cached file1.js # Removes from staging area only
Viewing a commit
git show 921a2ff # Shows the given commit
git show HEAD # Shows the last commit
git show HEAD~2 # Two steps before the last commit
git show HEAD:file.js # Shows the version of file.js stored in the last commit
Creating an alias
git config --global alias.lg “log --oneline"
Viewing a commit
git show HEAD~2
git show HEAD~2:file1.txt # Shows the version of file stored in this commit
Comparing commits
git diff HEAD~2 HEAD # Shows the changes between two commits
git diff HEAD~2 HEAD file.txt # Changes to file.txt only
Checking out a commit
git checkout dad47ed # Checks out the given commit
git checkout master # Checks out the master branch
Finding contributors
git shortlog
Tagging
git tag v1.0 # Tags the last commit as v1.0
git tag v1.0 5e7a828 # Tags an earlier commit
git tag # Lists all the tags
git tag -d v1.0 # Deletes the given tag
Branching & Merging
Managing branches
git branch bugfix # Creates a new branch called bugfix
git checkout bugfix # Switches to the bugfix branch
git switch bugfix # Same as the above
git switch -C bugfix # Creates and switches
git branch -d bugfix # Deletes the bugfix branch
Comparing branches
git log master..bugfix # Lists the commits in the bugfix branch not in master
git diff master..bugfix # Shows the summary of changes
Stashing
git stash push -m “New tax rules” # Creates a new stash
git stash list # Lists all the stashes
git stash show stash@{1} # Shows the given stash
git stash show 1 # shortcut for stash@{1}
git stash apply 1 # Applies the given stash to the working dir
git stash drop 1 # Deletes the given stash
git stash clear # Deletes all the stashes
Merging
git merge bugfix # Merges the bugfix branch into the current branch
git merge --no-ff bugfix # Creates a merge commit even if FF is possible
git merge --squash bugfix # Performs a squash merge
git merge --abort # Aborts the merge
Viewing the merged branches
git branch --merged # Shows the merged branches
git branch --no-merged # Shows the unmerged branches
Rebasing
git rebase master # Changes the base of the current branch
Cherry picking
git cherry-pick dad47ed # Applies the given commit on the current branch
Collaboration
Cloning a repository
git clone url
Sharing tags
git push origin v1.0 # Pushes tag v1.0 to origin
git push origin —delete v1.0
Sharing branches
git branch -r # Shows remote tracking branches
git branch -vv # Shows local & remote tracking branches
git push -u origin bugfix # Pushes bugfix to origin
git push -d origin bugfix # Removes bugfix from origin
Managing remotes
git remote # Shows remote repos
git remote add upstream url # Adds a new remote called upstream
git remote rm upstream # Remotes upstream
Rewriting History
Undoing commits
git reset --soft HEAD^ # Removes the last commit, keeps changed staged
git reset --mixed HEAD^ # Unstages the changes as well
git reset --hard HEAD^ # Discards local changes
Reverting commits
git revert 72856ea # Reverts the given commit
git revert HEAD~3.. # Reverts the last three commits
git revert --no-commit HEAD~3..
Interactive rebasing
git rebase -i HEAD~5