Skip to content
Laviyx Learn

Try

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#

Terminal
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git init                        # new repository here
git clone <url>                 # copy a repository

Save work#

Terminal
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 commit

See history and changes#

Terminal
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 line

Branches#

Terminal
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 delete

Merge and rebase#

Terminal
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 here

Fix a conflict#

Text
<<<<<<< HEAD
your version
=======
their version
>>>>>>> feature

Edit the file, delete the markers, then git add file and git commit (or git rebase --continue).

Remotes#

Terminal
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 branches

Undo#

SituationCommand
Discard edits in a filegit restore file
Unstage a filegit restore --staged file
Undo a commit, keep it stagedgit reset --soft HEAD~1
Undo a commit, keep the editsgit reset HEAD~1
Undo a commit, lose the editsgit reset --hard HEAD~1
Undo a pushed commit safelygit revert <commit>
Find lost commitsgit reflog
Delete untracked filesgit clean -n, then git clean -f

Stash#

Terminal
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 drop

Tags#

Terminal
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 locally

Find a bug#

Terminal
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#

Text
node_modules/        # a folder, anywhere
*.log                # by extension
/secret.txt          # only in the root
!keep.log            # exception

Already tracked? Use git rm --cached file.

References#

NameMeaning
HEADWhere you are now
HEAD~1The parent commit
HEAD@{1}Where HEAD was before the last move
origin/mainYour record of main on the remote
main..featureCommits in feature that are not in main

Want the full explanations? Go to the Learn Git and GitHub course →