Skip to content
abdallahmekky

CHEAT SHEET

Git cheat sheet

The commands you use every day, grouped by what you are trying to do. Danger zones are marked.

All cheat sheets

Setup

git config --global user.name "Your Name"        # who you are in commitsgit config --global user.email "you@example.com"git config --global init.defaultBranch main      # name for new reposgit config --global pull.rebase true             # pull with rebase by defaultgit config --global core.editor vim              # editor for commit messagesgit config --list                                # show the configuration

Starting

git init                          # turn the current folder into a repogit clone https://github.com/user/repo.git      # copy a repogit clone --depth 1 URL           # only the latest commit (faster)

Staging and committing

git status -sb                    # short status with the branchgit add file                      # stage a filegit add -p                        # stage changes chunk by chunkgit add .                         # stage everything in this foldergit commit -m "message"           # commit what is stagedgit commit -am "message"          # stage tracked files and commitgit commit --amend                # fix the last commit (message or files)git diff                          # unstaged changesgit diff --staged                 # what you are about to commitgit rm file                       # delete a file and stage itgit mv old new                    # rename a file and stage it

Branches

git branch                        # list local branchesgit branch -a                     # include remote branchesgit switch name                   # switch to a branchgit switch -c name                # create and switchgit merge name                    # merge a branch into the current onegit rebase main                   # replay your commits on top of maingit branch -d name                # delete a merged branchgit branch -m new-name            # rename the current branch

Remotes

git remote -v                           # show the remotesgit remote add origin URL               # add a remote called origingit fetch                               # download without merginggit pull                                # fetch and mergegit pull --rebase                       # fetch and rebase (no merge commit)git push                                # upload commitsgit push -u origin main                 # first push, remember the upstreamgit push origin --delete branch         # delete a remote branch

Undoing things

git restore file                  # discard changes in a file (cannot be undone)git restore --staged file         # unstage a file, keep the changesgit reset --soft HEAD~1           # undo the last commit, keep changes stagedgit reset --mixed HEAD~1          # undo the last commit, keep changes unstagedgit reset --hard HEAD~1           # DANGER: undo the commit and throw changes awaygit revert abc1234                # new commit that reverses a commit (safe for shared branches)git clean -n                      # preview untracked files that clean would deletegit clean -fd                     # DANGER: delete untracked files and foldersgit reflog                        # every place HEAD has been (your safety net)

History

git log --oneline --graph --decorate --all     # compact history of all branchesgit log -p file                                # history of one file with changesgit log --author="name"                        # commits by one persongit log -S "text"                              # commits that added or removed textgit show abc1234                               # one commit in detailgit blame file                                 # who changed each linegit diff main..feature                         # difference between two branches

Stash and tags

git stash                          # shelve uncommitted workgit stash -u                       # include untracked filesgit stash list                     # list the stashgit stash pop                      # bring it back and drop itgit stash apply                    # bring it back and keep itgit stash drop                     # delete the newest stashgit tag v1.0                       # lightweight taggit tag -a v1.0 -m "release 1.0"   # annotated taggit push --tags                    # upload tags

.gitignore

node_modules/.env*.logdist/.DS_Store

One pattern per line. Put the file in the repo root. Already tracked files need git rm --cached file first.

More cheat sheets

Questions

How do I undo the last commit but keep the changes?

Use git reset --soft HEAD~1. The commit is removed and its changes stay staged.

How do I create and switch to a new branch?

Use git switch -c branch-name. On older Git versions, git checkout -b branch-name does the same.

How do I discard changes in a file?

Use git restore file to discard working-tree changes. This cannot be undone, so check git diff first.

What is the difference between git fetch and git pull?

git fetch downloads new commits without changing your files. git pull fetches and then merges, or rebases with --rebase.