Git advanced usage, take it if you like!

Keywords: Java git Spring npm github


If you think git is confusing, then this copy is for you!

Please note that I intentionally skipped basic commands such as git commit and git pull/push. The theme of this short copy is some "advanced" uses of GIT.

Navigation - jump to previous branch

git checkout -

View history

# Each submission is shown on a single line
git log --oneline

# Search all submission logs for submissions containing "homepage"
git log --all --grep='homepage'

# Get someone's submission log
git log --author="Maxence"

Alas: * * * * reset a commit that you don't want to keep, but now you want to roll back?

#Get all operation history
git reflog

#Reset to corresponding submission
git reset HEAD@{4}
# …… Or
git reset --hard <Hash value submitted>

Ouch: * * * * I made a mess of the local warehouse. How should I clean it up?

git fetch origin
git checkout master
git reset --hard origin/master

View the differences between my branch and master

git diff

Custom submit

#Edit last commit
git commit --amend -m "Better commit log"

#Add some content in the last commit, keep the commit log unchanged. Git add. & & git commit -- amend -- no edit

#Empty commit - can be used to re trigger CI builds
git commit --allow-empty -m "chore: re-trigger build"

Square submit

For example, I want to rebase the last three submissions:

- git rebase -i HEAD~3
-Keep the pick of the first line, and replace the remaining commits with squash or s
-Clean up the commit log and save (type: wq in vi editor to save)

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


For example, I want to add some content to the submission of fed14a4c.

git submit branch

git add .
git commit --fixup HEAD~1
#Or you can replace HEAD~1 with the submitted hash value (fed14a4c)

git rebase -i HEAD~3 --autosquash
#Save and exit the file (input `: wq `) in VI)

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.

#Run the 'npm test' command on the last 3 commits
git rebase HEAD~3 --exec "npm test"

Temporary storage

Staging is more than just git stash and git stash pop;)

#Save all files being tracked
git stash save "log information"

#List all staging items
git stash list

#Get and delete staging items
git stash apply stash@{1}
git stash drop stash@{1}
# …… Or use a command
git stash pop stash@{1}


#Remove a branch that does not exist on the remote warehouse
git fetch -p

#Remove all branches containing 'greenkeeper'
git fetch -p && git branch --remote | fgrep greenkeeper | sed 's/^.\{9\}//' | xargs git push origin --delete

GitHub = Git + Hub

I use Hub as a package of GIT. If you want to do this, you can set an alias: alias git='hub '

#Open the browser to access the warehouse url (GitHub warehouse only) git browse

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'

# 🤘
alias yolo='git push --force'

#Weekly station meeting report
git-standup() {
 AUTHOR=${AUTHOR:="`git config`"}

    if [[ $(date +%u) == 1 ]] ; then
 since="2 days ago"

 git log --all --since "$since" --oneline --author="$AUTHOR"

By Maxence Poutord
Source: New Frontend website

Recommend to my blog to read more:

1.Java JVM, collection, multithreading, new features series

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Latest interview questions of Java, backend, architecture, Alibaba and other large factories

Feel good, don't forget to like + forward!

Posted by dwest on Sun, 17 May 2020 23:59:35 -0700