Alex Bollbach
Alex Bollbach

Reputation: 4570

Git - error while merging because of local changes in master

(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?

Upvotes: 2

Views: 984

Answers (1)

VonC
VonC

Reputation: 1323223

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.

https://backlogtool.com/git-guide/en/img/post/stepup/capture_stepup1_3_3.png

(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.

https://cms-assets.tutsplus.com/uploads/users/585/posts/22988/image/git-stash-stashing-changes.png

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

Upvotes: 4

Related Questions