1. cherry-pick 强制替换
You can tell it to always prefer the changes of the commit you are cherry-picking:
git cherry-pick commitish --strategy-option theirs
commitish
can be a SHA-1 hash of a commit, or a branch-name
for the lastest commit of that branch, branch-name~1
for the commit before that etc.
If you want to do the reverse, use:
git cherry-pick commitish --strategy-option ours
The shorthand for --strategy-option
is -X
(uppercase X).
- Reference:
2. 设置全局 ignore 配置
$ git config --global core.excludesfile ~/.gitignore_global
- Reference
3. 合并多个最近提交
$ git rebase -i origin/master
如果原始提交为:
pick 16b5fcc Code in, tests not passing
pick c964dea Getting closer
pick 06cf8ee Something changed
pick 396b4a3 Tests pass
pick 9be7fdb Better comments
pick 7dba9cb All done
提交顺序从上到下分别为从旧到新,所以需要将新的提交合并到旧的提交上,因此 pick
的应该是最旧的那个 commit,squash
的是被合并的新的 commit:
pick 16b5fcc Code in, tests not passing
squash c964dea Getting closer
squash 06cf8ee Something changed
squash 396b4a3 Tests pass
squash 9be7fdb Better comments
squash 7dba9cb All done
4. 忽略 pyc 文件
> cat .gitignore
*.pyc
然后通过下面这个命令清除已有的 pyc 文件:
$ find . -name "*.pyc" -exec git rm -f "{}" \;
5. pull 拉取所有的分支
[root@liqiang.io] # git fetch --all
[root@liqiang.io] # git pull --all
[root@liqiang.io] # git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
6. push 推送所有的分支和 tag 到远程
[root@liqiang.io] # git push REMOTE --mirror
7. 远程分支
- 查看远程分支
[root@liqiang.io]# git branch -a
git branch -a
* k8s
master
v3.5.x
v4.0.x
remotes/origin/debug
remotes/origin/k8s
remotes/origin/master
remotes/origin/v3.5.x
remotes/origin/v4.0.x
8. keywords: git tips
9. git 合并所有的 commits
- 方式一:删除
.git
目录之后重新提交 - 方式二:
git reset $(git commit-tree HEAD^{tree} -m "A new start")
- Ref
10. git 重置根提交
效果:
- 原始提交记录:
A <--- B <--- C <--- D <--- E <--- F
- 修改后记录:
D <--- E <--- F
- 命令:
git filter-branch --parent-filter "sed 's/-p C//'" HEAD
10. 删除最后一次提交
[root@liqiang.io]# git reset --hard HEAD~1
11. 忽略空格的变化查看文件修改
[root@liqiang.io]# git diff -w
12. 清除无效的远程追踪
[root@liqiang.io]# git remote prune origin
13. 从 Git 记录中删除记录但是不删文件
[root@liqiang.io]# git rm --cached -r .idea
14. 修改最后一个提交的作者
[root@liqiang.io]# git commit --amend --author="Author Name <[email protected]>"