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
Each submission is displayed on a single line
git log --oneline
# Search all submissions logs for submissions containing "homepage"
git log --all --grep='homepage'
Get someone's commit log
git log --author="Maxence"
Copy the code
Oops: Reset a commit you didn’t want to keep, but now you want to roll it back?
Get all operation history
git reflog
# reset to commit accordingly
git reset HEAD@ {4}
#... Or...
git reset --hard < submitted hash value >
Copy the code
What’s Happening when You Git commit
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
See the difference between my branch and my master
git diff master..my-branch
Copy the code
Custom to submit
# Edit last submission
git commit Amend-m "Better commit logs"
Add something to the last commit, leaving the commit log unchanged
git add . && git commit --amend --no-edit
Empty commit -- can be used to re-trigger a CI build
git commit --allow-empty -m "chore: re-trigger build"
Copy the code
If you don’t know how to write a commit log, check out my article on the Angular style commit log convention.
Squash submitted
Let’s say I want to rebase the last three 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
Copy the code
correction
Let’s say you want to add something to submit feD14a4C.
git add .
git commit --fixup HEAD~1
Alternatively, HEAD~1 can be replaced with the submitted hash (fed14a4c)
git rebase -i HEAD~3 --autosquash
Enter ':wq' to save 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.
# Run 'NPM test' on the last three commits
git rebase HEAD~3 --exec "npm test"
Copy the code
The staging
It’s not just git stash and git stash pop;)
Save all files that are being tracked
git stash save "Log Message"
# list all temporary entries
git stash list
Get and delete the stub
git 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 branches that do not exist on the remote repository
git fetch -p
Remove all branches that contain 'greenkeeper'
git fetch -p && git branch --remote | fgrep greenkeeper | sed 's/^.\{9\}//' | xargs git push origin --delete
Copy 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’
# Open a browser to access the repository URL (GitHub repository only)
git browse
Copy the code
For other commands, see here.
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'
# It is used for weekly work reports at the station
git-standup() {
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
What is your favorite Git command?
Thank you for your time. I hope you found this article helpful!
Original article by Maxence Poutord, reproduced with permission from New Frontend.