Git衝突:commit your changes or stash them before you can merge. 解決辦法

  • 2019 年 10 月 7 日
  • 筆記

Git衝突:commit your changes or stash them before you can merge. 解決辦法

2018年04月09日 18:18:29 明天是spring 閱讀數 2568

用git pull來更新代碼的時候,遇到了下面的問題:

1 2 3 4

error: Your local changes to the following files would be overwritten by merge:            ********************************************************* Please, commit your changes or stash them before you can merge. Aborting

出現這個問題的原因是其他人修改了xxx.php並提交到版本庫中去了,而你本地也修改了xxx.php,這時候你進行git pull操作就好出現衝突了,解決方法,在上面的提示中也說的很明確了。

1、保留本地的修改 的改法

1)直接commit本地的修改 —-也一般不用這種方法

2)通過git stash  —- 通常用這種方法

1 2 3

git stash git pull git stash pop

通過git stash將工作區恢復到上次提交的內容,同時備份本地所做的修改,之後就可以正常git pull了,git pull完成後,執行git stash pop將之前本地做的修改應用到當前工作區。

git stash: 備份當前的工作區的內容,從最近的一次提交中讀取相關內容,讓工作區保證和上次提交的內容一致。同時,將當前的工作區內容保存到Git棧中。

git stash pop: 從Git棧中讀取最近一次保存的內容,恢復工作區的相關內容。由於可能存在多個Stash的內容,所以用棧來管理,pop會從最近的一個stash中讀取內容並恢復。

git stash list: 顯示Git棧內的所有備份,可以利用這個列表來決定從那個地方恢復。

git stash clear: 清空Git棧。此時使用gitg等圖形化工具會發現,原來stash的哪些節點都消失了。

2、放棄本地修改 的改法  —-這種方法會丟棄本地修改的代碼,而且不可找回

1 2

git reset –hard git pull<br><br><br><br><br><br>

(adsbygoogle = window.adsbygoogle || []).push({});