1. 程式人生 > >Git pull 衝突解決

Git pull 衝突解決

1. 問題描述:

王修改了檔案A並且push到了git server上,這時李也在修改檔案A,但修改沒有完成,李希望獲得最新的程式碼,如果李直接pull的話會遇到一下問題:

error: Your local changes to the following files would be overwritten by merge:

***************************************

Please, commit your changes or stash them before you can merge.

造成衝突的原因:

很多命令都可能出現衝突,但從根本上來講,都是merge 和 patch(應用補丁)時產生衝突。 而rebase就是重新設定基準,然後應用補丁的過程,所以也會衝突。 git pull會自動merge,repo sync會自動rebase,所以git pull和repo sync也會產生衝突。當然git rebase就更不用說了。

2. 問題解決

2.1 commit your change

先提交你的改動,在更新程式碼

2.2 stash

git stash

git pull

git stash pop

git stash的作用是把本地的改動儲存起來,、

然後在git pull 這樣就不會有衝突的問題

最後git stash pop 就是把之前的改動merge到程式碼中。

2.3 merge

使用git merge --abort中止merge。merge manual中說,這條命令會盡力恢復到Merge之前的狀態(可能失敗!)。

merge manual中有一條警告:

Warning: Running git merge with uncommitted changes is discouraged: while possible, it leaves you in a state that is hard to back out of in the case of a conflict.

3. 參考文獻

[1] http://www.cnblogs.com/sinojelly/archive/2011/08/07/2130172.html

[2] http://blog.csdn.net/iefreer/article/details/7679631