如何签出到另一个分支并强制覆盖更改

时间:2018-08-20 08:54:01

标签: git

我在branch1上进行了一些更改并将其提交后,我正尝试检出到branch2

>>git checkout branch2
error: Your local changes to the following files would be overwritten by checkout:
    .gitignore
Please, commit your changes or stash them before you can switch branches.
Aborting

但我看不到更新文件中提到的.gitignore文件:

>>git status
On branch branch1
Your branch is ahead of 'origin/branch1' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean
>>git stash 
No local changes to save

我也尝试过

>>git checkout branch2 -f

但是得到了

error: Entry '.gitignore' not uptodate. Cannot merge.

>>git rm --cached .gitignore

没有成功

如何忽略此branch2强制结帐到"error: Your local changes to the following files would be overwritten by checkout"

2 个答案:

答案 0 :(得分:1)

解决如下:

>>git rm --cached .gitignore
>>git reset HEAD /path/to/.gitignore

.gitignore出现在“暂无提交文件”中

>>git checkout -- .gitignore

答案 1 :(得分:0)

如果您需要检出branch2而不考虑在branch1上所做的更改

首先重置在branch1上所做的更改

git reset --hard

然后检出branch2

git checkout branch2

希望这会有所帮助。...:)