Category: Git

  • git push 多个 remote

    如果都是 master 的话

    git remote | xargs -n 1 git push

    同一个 remote 有多个 push url, git push origin branch_name

    [remote "origin"]
    	url = [email protected]:jpuyy/terraform-gke.git
    	fetch = +refs/heads/*:refs/remotes/origin/*
    	pushurl = [email protected]:jpuyy/terraform-gke.git
    	pushurl = https://self-managed-git.com/common/terraform-gke.git
    
  • git 在每一次 commit 中清理文件

    有两个很大的二进制文件,在版本库中没有一点用处。
    使用核武器 filter-branch ,将每一次提交中出现这两个文件的时候删掉,然后重新提交,所以 commit 的 hash 值都会变掉。

    git filter-branch -f --tree-filter 'rm -f nginx/files/GeoLiteCity.dat nginx/files/GeoIP.dat' HEAD

    强制 push 到 origin

    git push origin --force

    你的合作者现在混乱了。需要将远端的 master rebase 过来

    git rebase --onto origin/master master master
    git fetch
    git reset --hard origin/master

    强制 pull 下来

    git pull --force
  • git patch 的应用

    根据提交来创建 patch,并把创建的 patch 应用到其他 repo 上

    当要修补一个 bug 或开发新特性,新创建一个分支

    git checkout -b fix

    在做了一些提交之后,基于 master 输出一个 patch

    git format-patch master --stdout > fix.patch

    接下来把 patch 文件搞到其他 repo 中

    查看此 patch 有哪些改变

    git apply --stat fix.patch

    测试是不是可以应用 patch 上去

    git apply --check  fix.patch

    如果没有报错,可以进行下一步,应用 patch

    git am --signoff < fix.patch
  • 做一次 git 完美提交

    分离主题和内容
    主题控制在 50 个字符
    主题第一个字母大写
    主题行不要用句号结束
    主题行使用祈使语气
    内容每 72 个字符换行
    在内容里解释改变的大致内容 what 和原因 why (改变的方式 how 在 git diff 里可以看到,不用说)

    http://chris.beams.io/posts/git-commit/

  • git diff 高亮显示改变的单词

    平时 git diff 都是以行级别的

    如果以单词级别的可以这样

    git diff --color-words
    git diff --word-diff
  • git cherry pick 任意选 commit merge 过来

    在 git 里,每一个 commit 都带有一个 sha1 的标识,在任何分支,任何状态下,如果你知道这次提交的相关情况,可以将一次 commit merge 过来。

    git cherry-pick 2379df