Git Cheatsheet - 2021


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


Creating Snapshots

Initializing a repository

1git init

Staging files

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

Viewing the status

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

Committing the staged files

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

Skipping the staging area

1git commit -am "Message"

Removing files

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

Renaming or moving files

1git mv server.go main.go

Viewing the staged/unstaged changes

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

Viewing history

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

Viewing 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

Unstaging files (undoing git add)

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

Discarding 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

Restoring an earlier version of a file

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

Browsing History

Viewing the history

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

Filtering 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

Formatting the log output

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

Creating an alias

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

Viewing a commit

1git show HEAD~2
2git show HEAD~2:file1.txt         # Shows the version of file stored in this commit

Comparing commits

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

Checking out a commit

1git checkout dad47ed              # Checks out the given commit
2git checkout master               # Checks out the master branch

Finding a bad commit

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

Finding contributors

1git shortlog

Viewing the 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

Finding the author of lines

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

Tagging

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

Branching & Merging

Managing branches

1git branch bugfix                   # Creates a new branch called bugfix
2git checkout bugfix                 # Switches to the bugfix branch
3git switch bugfix                   # Same as the above
4git switch -C bugfix                # Creates and switches
5git branch -d bugfix                # Deletes the bugfix branch

Comparing branches

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

Stashing

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

Merging

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

Viewing the merged branches

1git branch --merged                 # Shows the merged branches
2git branch --no-merged              # Shows the unmerged branches

Rebasing

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

Cherry picking

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

Collaboration

Cloning a repository

1git clone url

Syncing with remotes

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

Sharing Tags

1git tag -a v1.0.0-rc1               # Creates an annotated tag
2git push origin v1.0.0-rc1          # Pushes the tag to origin
3git push origin --delete v1.0.0-rc1 # Deletes the tag from origin

Sharing branches

1git branch -r                       # Shows remote tracking branches
2git branch -vv                      # Shows local & remote tracking branches
3git push -u origin bugfix           # Pushes bugfix to origin
4git push -d origin bugfix           # Removes bugfix from origin

Managing remotes

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

Rewriting History

Undoing 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

Reverting commits

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

Recovering lost commits

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

Amending the last commit

1git commit --amend

Interactive rebasing

1git rebase -i HEAD~5

Change ownership of previous commits

1# below two do the same job, for last x commits
2git rebase -i HEAD~6 -x "git commit --amend --author 'Author Name <[email protected]>' --no-edit"
3# or
4git rebase -i COMMIT_SHA -x "git commit --amend --author 'Author Name <[email protected]>' -CHEAD"

Housekeeping(Personal Favorites)

Auto-CRLF

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

Default init branch name

1git config --global init.defaultBranch main

Purge all un-existing remote branches on local

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

Remove local branches

1git branch -D feature/one feature/two bugfix/one

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

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

Happy gitting! :)

comments powered by Disqus