git branch
# 当前分支
git branch -m <new_branch_name>
# 非当前分支
git branch -m <old_branch_name> <new_branch_name>
git log --graph --decorate --oneline --all
对照分支关系图梳理分支关系,在trunk based的分支模型中非常实用。
git checkout <another-branch>
git checkout -b <new-branch>
git checkout -b <new-branch> <existed branch or commit>
WIP
或者直接用.
这么一个最短记录,其中 WIP 是 work in progress 的缩写。另一种办法是暂时将当前修改压栈,等下次切换回来的时候弹栈就好了。这样做的好处是不会污染提交记录。# 压栈
git stash
# 出栈
git stash pop
# 查看栈中的记录
git stash list
有时候弹出栈顶的时候,本地代码已经修改过了,会发生冲突。这个时候即使解决完了冲突,栈顶的记录也不会消失,解决办法是手动删除栈内记录:
git stash drop <stash@{id}>
默认是stash@{0}
也就是栈顶。
git rebase <base-branch>
提倡加入 -i
参数,可以同时合并、删除、重新编辑本分支历史提交记录,只需要将每一条提交记录前面的pick修改为对应的squash、drop、edit等即可。
变基的时候,有时候会提示发生冲突,手动解决冲突之后,继续变基操作:
git rebase --continue
有时想要直接忽略掉后续冲突的提交,使用以下命令:
git rebase --skip
# 首先查看操作记录,找到变基之前的最后一次提交
git reflog
# 以此为基础,创建新分支 recovery 用于重新解决冲突
git checkout -b recovery <commit-id>
... 解决冲突并continue
# 切换回分支,重新变基
git checkout <work-branch>
git rebase recovery
# 删除无用分支
git branch -d recovery
git show <commit-id>
缺省查看最后一次提交的修改。
git commit --amend
如果只改动代码,不需要改动提交信息的话:
git commit --amend --no-edit
git reset HEAD~N
默认撤回到工作区(workspace),撤回到暂存区需要加参数--soft
,直接删除需要加--hard
。
git diff
对比暂存区与历史记录:
git diff --cached
对比工作区与某一历史记录的变化(如HEAD~1):
git diff HEAD~2
对比某文件(如a.txt)在工作区与某一历史记录的变化(如HEAD~2):
git diff HEAD~2 --a.txt
对比某文件(如a.txt)在某两个历史记录之间的变化(如HEAD~2和HEAD~5):
git diff HEAD~2 HEAD~5 --a.txt
du -hs .
du -hs *
git filter-branch --force --index-filter ‘git rm -rf --cached --ignore-unmatch big-file.jar‘ --prune-empty --tag-name-filter cat -- --all
删除本地文件残留回收空间:
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
原文:https://www.cnblogs.com/zhcpku/p/13441277.html