Git Cheatsheet for Minimalists

Creating Snapshots
Browsing History
Branching & Merging
Rewriting History
Housekeeping(Personal Favorites)

Basic Git Concepts

  • Working copy (working tree file): It refers to the file in the repository that appears on the hard disk.
  • Index (staging area or cache): it refers to you have git add-ed, or, what would be committed if you were to run git commit.
  • HEAD: It refers to the "current" or "active" branch, when we need to check out a branch (referring to your attempt to match the branch with what is in the working copy), only one can be checked out at a time.

git init

Initialize a new git repository

1git init

git add

Stage files

1git add main.go Makefile          # Stages multiple files
2git add .                         # Stages the current directory and all its content

git restore

Unstage files (undo git add)

1git restore --staged server.go    # Copies the last version of server.go from repo to index

Discard local changes

1git restore server.go             # Copies server.go from index to working directory
2git restore server.go main.go     # Restores multiple files in working directory
3git restore .                     # Discards all local changes (except untracked files)
4git clean -fd                     # Removes all untracked files

Restore earlier version of a file

1git restore --source=HEAD~2 server.go

To recap git restore:

 1# Restoring the working tree from the index
 2git restore main.go
 4# Equivalent to
 5git restore --worktree main.go
 7# Restoring index content from HEAD
 8git restore --staged main.go
10# Restoring the working tree and index from HEAD
11git restore --staged --worktree main.go

git status

View status

1git status                        # Full status
2git status -s                     # Short status

git commit

Commit staged files

1git commit                        # Opens the default editor to type a long message
2git commit -m "Message"           # Commits with a one-line message

Amend the last commit

1git commit --amend

Skip the staging area

1git commit -am "Message"

git rm

Remove files

1git rm bs.go                      # Removes from working directory and staging area
2git rm --cached bs.go             # Removes from staging area only

Remove a directory from remote repository after adding them to .gitignore

1git rm -r --cached node_modules
2git commit -m 'remove ignored directory node_modules'
3git push origin main

git mv

Rename or move files

1git mv server.go main.go

git diff

View staged/unstaged changes

1git diff                          # Shows unstaged changes
2git diff --staged                 # Shows staged changes
3git diff --cached                 # Same as the above

Compare commits

1git diff HEAD~2 HEAD              # Shows the changes between two commits
2git diff HEAD~2 HEAD file.txt     # Changes to file.txt only

git log

View history

1git log                           # Full history
2git log --oneline                 # Summary
3git log --reverse                 # Lists the commits from the oldest to the newest

View history of a file

1git log file.txt                  # Shows the commits that touched file.txt
2git log --stat file.txt           # Shows statistics (the number of changes) for file.txt
3git log --patch file.txt          # Shows the patches (changes) applied to file.txt

git show

View a commit

1git show 921a2ff                  # Shows the given commit
2git show HEAD                     # Shows the last commit
3git show HEAD~2                   # Two steps before the last commit
4git show HEAD:server.go           # Shows the version of server.go stored in the last commit

git log

View the history

1git log --stat                    # Shows the list of modified files
2git log --patch                   # Shows the actual changes (patches)

Filter the history

1git log -3                        # Shows the last 3 entries
2git log --author=crazyoptimist
3git log --before="2021-01-01"
4git log --after="one week ago"
5git log --grep="GUI"              # Commits with "GUI" in their message
6git log -S"GUI"                   # Commits with "GUI" in their patches
7git log hash1..hash2              # Range of commits
8git log file.txt                  # Commits that touched file.txt

Format the log output

1git log --pretty=format:"%an committed %H"

git config

Set committer(without --global flag, it affects the current repo only)

1git config --global crazyoptimist
2git config --global [email protected]

Default init branch name

1git config --global init.defaultBranch main

Create an alias

1git config --global alias.lg "log --oneline"


1git config --global core.autocrlf input           # Unix
2git config --global core.autocrlf true            # Windows

git bisect

Find bad commits

1git bisect start
2git bisect bad                    # Marks the current commit as a bad commit
3git bisect good ca49180           # Marks the given commit as a good commit
4git bisect reset                  # Terminates the bisect session

git shortlog

Find contributors

1git shortlog

git blame

Find authors of lines

1git blame file.txt                # Shows the author of each line in file.txt

git tag


1git tag v1.0                            # Tags the last commit as v1.0
2git tag v1.0 5e7a828                    # Tags an earlier commit
3git tag                                 # Lists all the tags
4git tag -d v1.0                         # Deletes the given tag
5git tag -a v1.0.0-rc1                   # Creates an annotated tag
6git push origin v1.0.0-rc1              # Pushes the tag to origin
7git push origin --delete v1.0.0-rc1     # Deletes the tag from origin
8git push --tags                         # Push all tags

git switch

1git switch dev                      # Switch to branch `dev`
2git switch -c dev                   # Create and switch
3git switch -                        # Switch to the previous branch
4git switch -d master~1              # Switch to the commit `master~1`

git branch

1git branch bugfix                   # Creates a new branch called bugfix
2git branch -d bugfix                # Deletes the bugfix branch
3git branch --merged                 # Shows the merged branches
4git branch --no-merged              # Shows the unmerged branches
5git branch -r                       # Shows remote tracking branches
6git branch -vv                      # Shows local & remote tracking branches
7git branch -D feature/one feature/two bugfix/one

compare branches

1git log master..bugfix              # Lists the commits in the bugfix branch not in master
2git diff master..bugfix             # Shows the summary of changes

git stash

1git stash push -m "some comments"   # Creates a new stash. Without `push`, does the same.
2git statsh pop 1                    # Apply and remove the given stash from stash stack
3git stash list                      # Lists all the stashes
4git stash show [email protected]{1}            # Shows the given stash
5git stash show 1                    # shortcut for [email protected]{1}
6git stash apply 1                   # Applies the given stash to the working dir
7git stash drop 1                    # Deletes the given stash
8git stash clear                     # Deletes all the stashes

git merge

1git merge bugfix                    # Merges the bugfix branch into the current branch
2git merge --no-ff bugfix            # Creates a merge commit even if FF is possible
3git merge --squash bugfix           # Performs a squash merge
4git merge --abort                   # Aborts the merge

git rebase

1git rebase master                   # Changes the base of the current branch against master

Interactively rebase

1git rebase -i HEAD~5

These two commands do the same(changing ownership of previous commits) for last x commits

1git rebase -i HEAD~6 -x "git commit --amend --author 'Author Name <[email protected]>' --no-edit"
2git rebase -i COMMIT_SHA -x "git commit --amend --author 'Author Name <[email protected]>' -CHEAD"

git cherry-pick

1git cherry-pick dad47ed             # Applies the given commit on the current branch

git clone

1git clone url

git fetch

1git fetch origin master             # Fetches master from origin
2git fetch                           # Shortcut for "git fetch CURRENT_BRANCH"

git pull

1git pull                            # Fetch & merge current remote branch into the current local branch
2git pull --rebase                   # Fetch & rebase current remote branch into the current local branch
3git push origin master              # Pushes master to origin
4git push                            # Shortcut for "git push origin CURRENT_BRANCH"

git push

1git push -u origin bugfix           # Pushes bugfix to origin
2git push -d origin bugfix           # Removes bugfix from origin

git remote

Add/remove remotes

1git remote                          # Shows remote repos
2git remote add upstream url         # Adds a new remote called upstream
3git remote rm upstream              # Remotes upstream

Purge all non-existing remote branches from your local repo

1git remote prune origin --dry-run                 # Runs preflight check
2git remote prune origin

git reset

Undo commits

1git reset --soft HEAD^              # Removes the last commit, keeps changed staged
2git reset --mixed HEAD^             # Unstages the changes as well
3git reset --hard HEAD^              # Discards local changes

git revert

Revert commits

1git revert 72856ea                  # Reverts the given commit
2git revert HEAD~3..                 # Reverts the last three commits
3git revert --no-commit HEAD~3..

git reflog

Recover lost commits

1git reflog                          # Shows the history of HEAD
2git reflog show bugfix              # Shows the history of bugfix pointer

Happy gitting! :)

comments powered by Disqus