1. cherry-pick 强制替换

You can tell it to always prefer the changes of the commit you are cherry-picking:

  1. 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:

  1. git cherry-pick commitish --strategy-option ours

The shorthand for --strategy-option is -X (uppercase X).

2. 设置全局 ignore 配置

  1. $ git config --global core.excludesfile ~/.gitignore_global

3. 合并多个最近提交

  1. $ git rebase -i origin/master

如果原始提交为:

  1. pick 16b5fcc Code in, tests not passing
  2. pick c964dea Getting closer
  3. pick 06cf8ee Something changed
  4. pick 396b4a3 Tests pass
  5. pick 9be7fdb Better comments
  6. pick 7dba9cb All done

提交顺序从上到下分别为从旧到新,所以需要将新的提交合并到旧的提交上,因此 pick 的应该是最旧的那个 commit,squash 的是被合并的新的 commit:

  1. pick 16b5fcc Code in, tests not passing
  2. squash c964dea Getting closer
  3. squash 06cf8ee Something changed
  4. squash 396b4a3 Tests pass
  5. squash 9be7fdb Better comments
  6. squash 7dba9cb All done

4. 忽略 pyc 文件

  1. > cat .gitignore
  2. *.pyc

然后通过下面这个命令清除已有的 pyc 文件:

  1. $ find . -name "*.pyc" -exec git rm -f "{}" \;

5. pull 拉取所有的分支

  1. [root@liqiang.io] # git fetch --all
  2. [root@liqiang.io] # git pull --all
  3. [root@liqiang.io] # git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

6. push 推送所有的分支和 tag 到远程

  1. [root@liqiang.io] # git push REMOTE --mirror

7. 远程分支

  1. [root@liqiang.io]# git branch -a
  2. git branch -a
  3. * k8s
  4. master
  5. v3.5.x
  6. v4.0.x
  7. remotes/origin/debug
  8. remotes/origin/k8s
  9. remotes/origin/master
  10. remotes/origin/v3.5.x
  11. remotes/origin/v4.0.x

8. keywords: git tips

9. git 合并所有的 commits

10. git 重置根提交

效果:

10. 删除最后一次提交

  1. [root@liqiang.io]# git reset --hard HEAD~1

11. 忽略空格的变化查看文件修改

  1. [root@liqiang.io]# git diff -w

12. 清除无效的远程追踪

  1. [root@liqiang.io]# git remote prune origin

13. 从 Git 记录中删除记录但是不删文件

  1. [root@liqiang.io]# git rm --cached -r .idea

14. 修改最后一个提交的作者

  1. [root@liqiang.io]# git commit --amend --author="Author Name <[email protected]>"