Git and GitHub Cheat Sheet
Setting up, saving work, history and diffs, branches, merge and rebase, conflicts, remotes, undoing mistakes, stash, tags, bisect and .gitignore.
Updated
Set up#
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git init # new repository here
git clone <url> # copy a repositorySave work#
git status # what has changed
git add file.txt # stage one file
git add . # stage everything here and below
git add -p # stage parts of files
git commit -m "Add login" # commit staged changes
git commit -am "Fix title" # stage tracked files and commit
git commit --amend # replace the last commitSee history and changes#
git log --oneline # short history
git log --oneline --graph --all # all branches as a graph
git log -n 5 --author="Asha" # filter
git log -S "text" # commits adding or removing text
git show HEAD~1 # one commit
git show HEAD~1:file.txt # a file in an old commit
git diff # unstaged changes
git diff --staged # what will be committed
git diff main...feature # what a branch changed
git blame file.txt # last change to each lineBranches#
git branch # list
git branch -a # include remote branches
git switch -c feature # create and switch
git switch main # switch
git switch - # previous branch
git branch -m new-name # rename current
git branch -d feature # delete merged branch
git branch -D feature # force deleteMerge and rebase#
git merge feature # bring feature into this branch
git merge --no-ff feature # always make a merge commit
git merge --squash feature # combine into one staged change
git merge --abort # cancel
git rebase main # replay this branch on main
git rebase --continue # after fixing a conflict
git rebase --abort # cancel
git rebase -i HEAD~3 # edit the last 3 commits
git cherry-pick <commit> # copy one commit hereFix a conflict#
<<<<<<< HEAD
your version
=======
their version
>>>>>>> featureEdit the file, delete the markers, then git add file and git commit (or git rebase --continue).
Remotes#
git remote -v # list remotes
git remote add origin <url> # connect a remote
git push -u origin main # first push of a branch
git push # later pushes
git fetch # download, change nothing
git pull # fetch and merge
git pull --rebase # fetch and rebase
git push --force-with-lease # after rebasing your own branch
git push origin --delete feature # delete a remote branch
git fetch --prune # forget deleted remote branchesUndo#
Stash#
git stash # save changes
git stash -u # include untracked files
git stash push -m "note" # with a message
git stash list
git stash pop # restore and remove
git stash apply # restore and keep
git stash dropTags#
git tag -a v1.0.0 -m "Release" # annotated tag
git tag # list
git push origin --tags # push tags
git tag -d v1.0.0 # delete locallyFind a bug#
git bisect start
git bisect bad # current commit is broken
git bisect good <commit> # this one worked
git bisect run npm test # automate it
git bisect reset # finish.gitignore#
node_modules/ # a folder, anywhere
*.log # by extension
/secret.txt # only in the root
!keep.log # exceptionAlready tracked? Use git rm --cached file.
References#
Want the full explanations? Go to the Learn Git and GitHub course →