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 - Copy the code

See the history

1.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?

1.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 origingit checkout mastergit reset --hard origin/master Copy the code

See the difference between my branch and my master

git diff master.. my-branchCopy the code

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 submitted

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

-git rebase -i HEAD~3 -save the commit logs on the first row and squash the rest of the commit logs.

1.pick 64d26a1 feat: add index.jss 45f0259 fix: update index.jss 8b15b0a fix: typo in index.js Copy the code

correction

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



Git commit branch

1.git add .git commit --fixup HEAD~1Git rebase -i HEAD~3 --autosquash# save and exit the file.Copy the code

Execute commands on each commit when rebase

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



The staging

It’s not just git stash and git stash pop;)

Git stash list# stash stash apply stash@{1} 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

GitHub = Git + Hub

I use the Hub as a wrapper around Git. If you want to do the same, you can set an alias: alias git=’hub’

Git Browse (GitHub repositoryCopy the code

Bonus: My favorite Git alias

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'${AUTHOR =${AUTHOR:=" 'git config user.name '"} since=yesterday if ${AUTHOR:=" 'git config user  [[ $(date +%u) == 1 ]] ; then since="2 days ago" fi git log --all --since "$since" --oneline --author="$AUTHOR"}Copy the code

The above content is some of my own feelings, share out welcome correction, incidentally beg a wave of attention, have ideas of partners can comment or private letter I oh ~



Author: Java Technology architecture


Source:
Today’s headline