Git - error while merging because of local changes in master

我的未来我决定 提交于 2020-01-01 18:27:08

问题


(in topicBranch)

git add .
git commit -m "added new feature"
git checkout master
git merger topicBranch

gives me the following error:

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

Looking at the log, the file in question is just some breakpoint related file buried deeply within my xcuserdata directory. My thinking was to run git rm --cached path/to/that/file and the problematic file would be removed from the index and git would have no problems with a merge. But it still fails.

My questions are A) how can I get the merge to run? B) given that git uses the "index" as its main area of interest, why wouldn't removing the problematic file from the index allow for a merge?


回答1:


You should not remove that file from the index, but reset it or stash it (meaning remove the local changes: the file is still needed in the index):

git stash
# or
git checkout -- your/file # overwrite any local change
# or, nuclear option
git reset --hard

If stashed, the normal workflow is to git stash pop after the merge.
(I actually prefer pull.rebase + rebase.autostash, that way git does stash for me on git pull)

Uncommited changes that are stored in the stash can be taken out and applied to the original branch and other branches as well.

(source: "Git Beginners Guide: Switching branches")

This is where the git stash command comes into play. Like git reset --hard, it gives you a clean working directory, but it also records your incomplete changes internally.
After fixing the critical bug, you can re-apply these changes and pick up where you left off. You can think of git stash as a "pause button" for your in-progress work.

(source: "Quick Tip: Leveraging the Power of Git Stash")



来源:https://stackoverflow.com/questions/35568716/git-error-while-merging-because-of-local-changes-in-master

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!