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


在項目里我們一般都會把自己第一次提交的配置文件忽略本地跟蹤

1
[Sun@webserver2 demo]$ git update-index --assume-unchanged <filename>

但是項目里的其他人如果不小心把該配置文件修改push到遠程倉庫之后,我們git pull代碼的時候就會報錯

1
2
3
4
5
6
7
8
9
10
11
12
13
[Sun@webserver2 demo]$ git add .
[Sun@webserver2 demo]$ git commit -m  'update:index.php'
[master f8a7428] update:index.php
  file changed, 1 insertion(+), 1 deletion(-)
[Sun@webserver2 demo]$ git pull
remote: Counting objects: 5,  done .
remote: Compressing objects: 100% (2 /2 ),  done .
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3 /3 ),  done .
From git.oschina.net:sunzmit /thinkphp
    1bc9485..c63dff3  master     -> origin /master
error: Your  local changes to the following files would be overwritten by merge:
     config.ini

大意是:您的本地更改的文件被合並覆蓋。並指出了會被覆蓋的文件

解決方法:

1.撤銷本地對文件的忽略

1
[Sun@webserver2 demo]$ git update-index --no-assume-unchanged config.ini

2.從最近的一次提交中讀取內容,備份當前的工作區的內容,將當前的工作區內容保存到Git棧中

1
2
3
[Sun@webserver2 demo]$ git stash
Saved working directory and index state WIP on master: f8a7428 update:index.php
HEAD is now at f8a7428 update:index.php

3.pull遠程倉庫代碼

1
2
3
4
[Sun@webserver2 demo]$ git pull
Merge made by the  'recursive' strategy.
  config.ini | 2 +-
  file changed, 1 insertion(+), 1 deletion(-)

4.從Git棧中讀取最近一次保存的內容,恢復工作區的相關內容

1
2
3
[Sun@webserver2 demo]$ git stash pop
Auto-merging config.ini
CONFLICT (content): Merge conflict  in config.ini

5.修改合並

1
2
3
4
5
6
7
8
[Sun@webserver2 demo]$ vim config.ini
<<<<<<< Updated upstream
This is a  test file !!!!!!!!!!!!!!!!
=======
This is a  test file
>>>>>>> Stashed changes
[Sun@webserver2 demo]$  cat test .txt
This is a  test file

<<<<<<< Updated upstream到=======中是從遠程倉庫pull下來別人的內容,=======到>>>>>>> Stashed changes中是我們本地文件內容,現在我們可以刪除其他,只保留自己的內容This is a test file

6.把文件回復到最新提交的版本,會保留修改內容

1
2
3
4
5
[Sun@webserver2 demo]$ git reset HEAD config.ini
Unstaged changes after reset:
M config.ini
[Sun@webserver2 demo]$  cat test .txt
This is a  test file

7.再次忽略本地跟蹤,完成!

1
[Sun@webserver2 demo]$ git update-index --assume-unchanged config.ini

8.最后不要忘記清除Git棧的備份

1
[Sun@webserver2 demo]$ git stash drop

本文永久地址:http://blog.it985.com/10665.html
本文出自 IT985博客 ,轉載時請注明出處及相應鏈接。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM