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.

Requires Git 2.28:

git config --global init.defaultBranch main

Introducing init.defaultBranch

git pull
git config --global pull.rebase true

git status
git status --staged

git log --color --oneline --graph

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

(formerly known as git checkout)

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

git restore filename

(formerly known as git checkout)

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

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

See git/git@e83c5163. It is awesome.

git rebase -i --root

N=5 commits for example:

git rebase -i HEAD~5

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

git stash
git stash pop
git stash list

git prune --dry-run --verbose
# review them
git prune --verbose

git gc
git fetch --all --prune

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

Install gh, then

git pr checkout 100

or

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.

git reflog

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.

Checkout this excellent guide: chancancode/branch-rename.

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

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"

Be Careful!

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

https://stackoverflow.com/a/9237511/517868

https://git-scm.com/book/en/v2/Git-Internals-The-Refspec