作者:叉腰大魔王
鏈接:https://www.jianshu.com/p/955ddee023bb
Git 使用過程中遇到的問題以及解決辦法

git 是項目當中最常用的代碼管理庫,熟練的使用git不是萬能的,但不能熟練的使用git是萬萬不能的,歸納了一下真正開始在多人協作的代碼庫中提交自己的代碼時遇到的問題。

git fetch 失效的問題

在項目工程中,主要使用的是兩個分支,一個是主分支master,一個是開發分支develop,我們一般在develop中進行開發,master分支中用來存重大版本的代碼。當需要獲取最新的代碼時,使用git fetch 或者 $ git fetch origin develop:develop 命令從遠程develop分支上拉取最新的代碼。

但有時候這個命令會失效,拉取不到最新的代碼,出現這樣的錯誤提示

fatal: Refusing to fetch into current branch refs/heads/develop of non-bare repository
fatal: The remote end hung up unexpectedly

這種時候,先切換到master分支,然後再從master分支fetch分支develop上的代碼,就能夠成功了。

$ git fetch origin develop:develop
fatal: Refusing to fetch into current branch refs/heads/develop of non-bare repository
fatal: The remote end hung up unexpectedly
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git fetch origin develop:develop
From 172.20.10.91:developers/android_do_as
5ee9941..ff421cf develop -> develop

究其原因,就和fatal的提示一樣,在非bare的git倉庫中,如果你要同步的本地跟蹤分支是當前分支,就會出現拒絕fetch的情況。也就是說不可以在非bare的git倉庫中通過fetch快進你的當前分支與遠程同步。

git 錯誤提交或者錯誤的合併瞭解決方案

第一次在代碼庫中提交代碼,心情比較激動,直接本地多次提交之後,就和遠程分支給merge了。命令看起來是這麼用的,但這樣就會在代碼線上弄出一條新線,而不是一條線,多麼醜啊。還好沒有push到遠程去,所以就要看看如何解決,把它弄成一條線了。

當已經錯誤的提交,或者是錯誤的合併已經產生了,首先,要想辦法回到過去,我多想回到過去,再回到你的身邊。

$ git reflog
ff421cf HEAD@{0}: checkout: moving from master to develop
efaaa61 HEAD@{1}: checkout: moving from develop to master

首先用git reflog 命令,看看最近自己做過什麼,哪裏是自己想回去的地方。

$ git reset --hard 72b075e
$ git clean --f

然後再使用 reset命令,複製自己想要去的地方的哈希碼,穿越時光回到過去。順便 clean 一下,保持清潔。

這樣你就能去到任何你想去的地方,so happy。

但如何把多個提交合併成一個提交呢?爲了保持代碼樹的乾淨漂亮,在本地的多次提交保存,弄成一次提交再推到遠程去可能會更加好一點,所以可以是用 rebase 命令進行衍合。

在自己多次提交的本地分支上進行衍合一般不會出現衝突,找準本地分支的第一次提交的哈希碼值,或者數清楚自己提交了幾次用rebase命令就可以將多次提交合併成一次提交

$ git rebase -i head~11

使用這個命令之後,會進入到vim模式,將自己需要的提交信息設置爲p,其他的設置爲s,最後使用:wq退出保存即可。

# Rebase 6a2eb6d..f7d0bd7 onto 6a2eb6d
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

這是對命令的提示。

但最好還是不要面對需要這樣使用rebase的情景,應該好好使用commit 命令的參數。比如:

git commit --amend

使用這個命令,會將本次提交寫到上次的提交中去,就不會產生多條的提交信息。

git rebase 中的衝突處理

在合併代碼的過程中,可以使用merge,也可以使用rebase,如果使用merge,合併之後推上遠程就會有兩條線,但使用rebase就只會產生一條線,變得簡潔而優雅,所以在合併代碼的過程中建議儘量使用rebase命令。

在合併代碼中可能會遇到衝突,當提示有衝突時,我們可以使用外部的圖形化衝突處理工具來處理衝突。這裏使用的是KDiff3來處理衝突。

  1. 安裝Kdiff3 軟件。(最好使用默認路徑)
  2. 添加kdiff3到git mergetool裏。 git config --global merge.tool kdiff3
  3. 添加kdiff3路徑到 git global config裏。git config --global mergetool.kdiff3.path "C:\Program Files\KDiff3\kdiff3.exe"
  4. 以後merge發生衝突時:git mergetool 來做圖形化merge。進入編輯衝突。

帶問號的纔是衝突。當兩者都需要保存時,右擊標記處,選完B後,再次右擊,選擇C。A是最原始的代碼,B是自己的,C是別人的。

在這裏需要注意的是,首先要設置好kDiff3的默認編碼,和自己的工程編碼一樣,要不解決完衝突之後還要解決亂碼問題。。。。

相關文章