If you find Git confusing, this cheat sheet is for you!

Note that I intentionally skipped basic commands like git commit, git pull/push, etc. This cheat sheet focuses on some “advanced” uses of Git.

Navigation — Jump to the previous branch git checkout –

See the history

Git log --all --grep='homepage --author="Maxence"Copy the code

Oops: Reset a commit you didn’t want to keep, but now you want to roll it back?

Git reset HEAD@{4} #... Or... Git reset --hard < commit hash >Copy the code

How can I clean up the mess I made in the local warehouse?

git fetch origin
git checkout master
git reset --hard origin/master
Copy the code

Git diff master git diff master my-branch

Custom to submit

Amend-m "better commit log" Git add. && git commit --amend --no-edit # Empty commit -- can be used to re-trigger CI to build git commit -- allow-empty-m "chore: re-trigger build"Copy the code

Squash commits Let’s say I want to rebase the last 3 commits:

  • git rebase -i HEAD~3

  • Keep the pick in the first row and replace the remaining commits with squash or S

  • Clean up the commit log and save it (type :wq in the vi editor to save it)

    pick 64d26a1 feat: add index.js s 45f0259 fix: update index.js s 8b15b0a fix: typo in index.js

correction

Let’s say you want to add something to submit feD14a4C.

Git commit branch

Git add. Git commit --fixup HEAD~1 # git rebase -i HEAD~3 --autosquash # ` : wq `)Copy the code

Rebase executes commands on each commit. If there are many features, there may be multiple commits in a branch. If the test fails, you want to find the commit that caused the test to fail. At this point you can use the rebase –exec command to execute commands on each commit.

Git rebase HEAD~3 --exec "NPM test"Copy the code

There’s more to staging than git stash and git stash pop;)

Git stash list # stash stash apply stash@{1} git stash drop Stash @ {1} #... Or use a command... git stash pop stash@{1}Copy the code

Clean up the

# remove remote warehouse there is no branch of git fetch - p # remove all contain ` greenkeeper ` branch of git fetch -p && git branch - remote | fgrep greenkeeper | sed 's/^.\{9\}//' | xargs git push origin --deleteCopy the code

I use the Hub as a Git wrapper. If you want to do the same, you can set an alias: alias git=’hub’ # open your browser to the repository URL (GitHub repository only) Git Browse

alias g='git' alias glog='git log --oneline --decorate --graph' alias gst='git status' alias gp='git push' alias ga='git Add 'alias gc='git commit -v' # 🤘 alias yolo='git push --force' {AUTHOR =${AUTHOR:=" 'git config user.name`"} since=yesterday if [[ $(date +%u) == 1 ]] ; then since="2 days ago" fi git log --all --since "$since" --oneline --author="$AUTHOR" }Copy the code