Git (2020)

Git v2.28.0 (2020-07-27).

All things I know about Git to be dangerous. I use GitHub for Desktop client to view changes. Branches/Tags all called Refs.

Set default branch of git init

Requires Git 2.28:

git config --global init.defaultBranch main

Introducing init.defaultBranch

Get changes

git pull
git config --global pull.rebase true

View Changes

git status
git status --staged

View your log

git log --color --oneline --graph

Switch branch

git switch branch-name
git switch - # back to last branch

(formerly known as git checkout)

Hard/Soft reset N commits

git reset --hard HEAD~N
git reset --soft HEAD~N

Restore file

git restore filename

(formerly known as git checkout)

Force Push

git push --force-with-lease


Rebase is to pick up your commits onto new base point. Rebase is to rewrite commit messages, reorder commits, merge relevant commits, edit changes in a commit.

git fetch origin
git rebase origin/main

Find First Commit

git rev-list --max-parents=0 HEAD

See git/git@e83c5163. It is awesome.

Changing First Commit

git rebase -i --root

Rebase N commits

N=5 commits for example:

git rebase -i HEAD~5

Abort from

Any command resulted in phases, you can use --abort flag to abort from "in progress".

git rebase --abort
git merge --abort
git cherry-pick --abort
git am --abort

Temporarily save changes

git stash
git stash pop
git stash list

Clean Up repo

git prune
git gc
git fetch --all --prune

Multiline Commit Messages

git commit -m "Line 1" -m "Line 2 WoW"

Apply PR to branch

Install gh, then

git pr checkout 100


git am -3 <pull_request_url>

Getting the PR locally is useful when helping contributors change something trivial. Apply their PR to latest main branch (or a fresh branch off from main), perform the trivial fix, then merge their work into main branch.

I need to find some commits back

git reflog

git- script becomes git command

If you have a script called git-local-push, then you can use it as git local-push, and set alias then you can do git lp.

Rename Branch

Checkout this excellent guide: chancancode/branch-rename.


git rev-parse --abbrev-ref HEAD # current local branch name
git rev-parse HEAD # last commit sha

WIP flow

Sometimes need to switch to another branch to do some things but don’t want to lose local changes and use stash:

alias wip="git add . && git commit -m 'WIP'"
alias dewip="git reset --soft HEAD~1"

Throw Away All Changes

Be Careful!

alias idontcare="git reset . && git restore . && git clean -fd"

Diving Deeper