Git:备忘单(高级)
如果你觉得 git 有点难懂,我创建了这个小抄!请注意,我故意省略了一些基本命令,比如git commit
…… git pull/push
。这个小抄旨在帮助大家更好地使用 git。
🧭 导航 - 前往上一个分支
git checkout -
🔍 获取历史记录
# Log in one line
git log --oneline
# Retrieve all commits by message
# Here all commit that contain 'homepage'
git log --all --grep='homepage'
# Retrieve all commit by author
git log --author="Maxence"
🙈糟糕 #1:我重置了一个不需要的提交。如何回滚?
# Get everything you did
git reflog
# then reset to the desired commit (i.e. HEAD@{4})
git reset HEAD@{4}
# ...or...
git reset --hard <commit-sha1>
有关此命令的更多详细信息,我写了这篇文章。
🤦♀️ 错误 #2:我把本地仓库弄混了。该如何清理?
git fetch origin
git checkout master
git reset --hard origin/master
# You're now up-to-date with master!
🕵🏻♂️我的分支和master
git diff master..my-branch
✔ 自定义提交
# Edit last commit
git commit --amend -m "A better message"
# Add something to the last commit without writing message again
git add . && git commit --amend --no-edit
# empty commit - can be useful to re-trigger CI build...
git commit --allow-empty -m "chore: re-trigger build"
如果您不知道在提交消息中要写什么,我写了一篇关于常规提交的文章。
♻️ 压缩提交
假设我想重新设置最后 3 次提交:
git rebase -i HEAD~3
squash
保留第一个“pick”,其余部分用“ ”(或“s
”)替换- 整理提交信息并保存(
:wq
在 vi 中)。
🎯修复
假设我想在提交中添加一些内容fed14a4c
git add .
git commit --fixup HEAD~1
# or replace HEAD~1 by the commit hash (fed14a4c)
git rebase -i HEAD~3 --autosquash
# save&quit the file (:wq in VI)
🕹在 rebase 时对每次提交执行命令
对于大量的功能,你可能会得到一个包含少量提交的分支。然后测试失败,你想找出“有问题的提交”。你可以使用rebase --exec
对历史记录中的每个提交执行一个命令。
# Will run "npm test" command on the last 3 commit ❤️
git rebase HEAD~3 --exec "npm run test"
🦋储藏
因为这不全是关于git stash
和git stash pop
;)
# save all tracked files
git stash save "your message"
# list your stashes
git stash list
# retrieve stash and delete
git stash apply stash@{1}
git stash drop stash@{1}
# ... or in 1 command
git stash pop stash@{1}
🗑 清洁
# remove branches that no longer exist on remote
git fetch -p
# remove all branch that contains "greenkeeper"
git fetch -p && git branch --remote | fgrep greenkeeper | sed 's/^.\{9\}//' | xargs git push origin --delete
🐙 GitHub = Git
+Hub
我使用Hub作为 git 的包装器。要启用它,你需要将 hub 设置为 git 的别名 ( alias git='hub'
)。
# Open browser and go to the repository url (GitHub only)
git browse
其他命令可在此处获得。
🦄 福利:我最喜欢的 git 别名
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'
# useful for daily stand-up
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"
}
那么你最喜欢的 git 命令是什么?
感谢您花时间阅读这篇文章。希望它对您有所帮助!如果您喜欢,请点个❤️或🦄!也欢迎在下方评论区或推特@_maxpou上留言或提问:)
最初发表于maxpou.fr。
文章来源:https://dev.to/maxpou/git-cheat-sheet-advanced-3a17