盤點Git的那些冷門玩法

其實也不是很冷門,只是要實現以下需求時,筆者一時間竟然想不起來,還要藉助搜索引擎,於是記錄一下,算是 備忘。希望對大家也有幫助。

強制覆蓋master分支

最近對 Spring Cloud YES[1] 進行了升級,由於改動太多了,導致了大量的衝突,合併比較費勁。於是想用開發分支強制覆蓋master分支,以下是步驟。

解決方案

1 切換到develop分支下,並保證本地已經同步了遠端develop的最新代碼。

git checkout develop
git pull

2 把本地的develop分支強制(-f)推送到遠端master。

git push origin develop:master -f

3 切換到舊分支master。

git checkout master

4 下載遠程倉庫最新內容,不做合併。

git fetch --all

5 把HEAD指向master最新版本。

git reset --hard origin/master

參考文檔

Git強制覆蓋master分支[2]

修改提交人信息

近日用公司電腦提交代碼到個人Git倉庫(是的,阿里不禁GitHub,也不禁止用公司電腦提交私人倉庫哦,只要不涉及公司商業機密即可),忘記修改提交人信息了……於是導致提交信息都是個人公司郵箱等信息……

感覺這種信息比較敏感,所以想把提交記錄修改掉。

解決方案

以下是操作步驟,記錄一下,防止未來有類似需求時又到處搜索。

1 批量修改本地提交記錄:

git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='要改成的提交人用戶名'; GIT_AUTHOR_EMAIL='要改成的提交人郵箱'; \
GIT_COMMITTER_NAME='要修改的提交人用戶名'; GIT_COMMITTER_EMAIL='要修改的提交人郵箱';"

2 push

git push -u -f 

搞定!

根本

要想從根本上解決問題,應該養成習慣,在clone完項目後,就立馬為每個倉庫設置提交人信息:

git config user.name "itmuch"
git config user.email "[email protected]"

參考文檔

1 git 修改已提交的某一次的郵箱和用戶信息[3]

2 git修改提交作者和郵箱[4]

刪除tag

前幾天要發佈一個私人小項目,然而手誤,打錯標籤了,想要刪除。由於這種場景比較少,一時間竟然忘記怎麼玩了……大寫的尷尬。記錄下:

解決方案

# 本地刪除 
git tag -d [tag名稱]

# 遠程刪除
git push origin :refs/tags/[tag名稱]

示例:

git tag -d v1.0
git push origin :refs/tags/v0.9

參考文檔

操作標籤[5]

git status 亂碼

筆者使用 Iterm2 ,當執行 git status 如果文件名帶有中文,會出現亂碼,如下所示:

On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: "\345\276\205\345\217\221\345\270\203/git status\344\271\261\347\240\201.md"
/<file>/<file>

解決方案

git config --global core.quotepath false

此時,再次執行 git status 就會正常顯示啦:

On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: 待發布/git status亂碼.md
no changes added to commit (use "git add" and/or "git commit -a")
/<file>/<file>

參考文檔

當git遇上中文亂碼[6] ,裡面還有其他亂碼場景的解決方案。

合併兩個不相關的Git倉庫

17年總結的:合併兩個不相關的Git倉庫[7]

使用GitLab Mirrors同步Git倉庫

使用GitLab Mirrors同步Git倉庫[8]

使用post receive hook同步Git倉庫

使用post receive hook同步Git倉庫[9]

References

[1] Spring Cloud YES: https://github.com/eacdy/spring-cloud-yes

[2] Git強制覆蓋master分支: https://www.cnblogs.com/king-le0/p/10097583.html

[3] git 修改已提交的某一次的郵箱和用戶信息: https://segmentfault.com/q/1010000006999861

[4] git修改提交作者和郵箱: https://blog.csdn.net/diu_brother/article/details/51982993

[5] 操作標籤: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001376951885068a0ac7d81c3a64912b35a59b58a1d926b000

[6] 當git遇上中文亂碼: https://www.cnblogs.com/jason0529/p/8962842.html

[7] 合併兩個不相關的Git倉庫: http://www.itmuch.com/work/merge-two-git-repo-unrelated/

[8] 使用GitLab Mirrors同步Git倉庫: http://www.itmuch.com/work/git-repo-sync-with-gitlab-mirrors/

[9] 使用post receive hook同步Git倉庫: http://www.itmuch.com/work/git-repo-sync-with-post-receive/


分享到:


相關文章: