1

I have some local changes and I know the repo has moved ahead since my last pull. When I do git pull, i get below message -

$ git pull Enter passphrase for key '/c/Users/xxx/.ssh/id_rsa': remote: Counting objects: 65, done. remote: Compressing objects: 100% (53/53), done. remote: Total 65 (delta 29), reused 0 (delta 0) Unpacking objects: 100% (65/65), 16.35 KiB | 7.00 KiB/s, done. From bitbucket.org:abc/someproject ashljkl..db9e852 feature/somefeature -> origin/feature/somefeature error: Your local changes to the following files would be overwritten by merge: src/main/java/com/somefilepath Please commit your changes or stash them before you merge. Aborting Updating ashljkl..db9e852 

From this SOF post: How do I ignore an error on 'git pull' about my local changes would be overwritten by merge? I could figure out how to resolve this issue. 1) git stash local change 2) git pull 3) then git stash pop the stashed changes.

But I want to know if there is an further easier way of handling this - just say to git pull with some options/flags to merge the changes from repo with my local changes directly. In the process of merging, if there are conflicts, i am fine with git showing conflicts and placing conflict markers in the source files that have conflicts.

9
  • 2
    This has nothing to do with conflicts. Commented Apr 4, 2021 at 8:23
  • git pull --autostash will stash your local changes and pull in new changes. not sure if there are any options thing that will merge automatically with a pull. In any case git seems to complain of a conflict during merge. Commented Apr 4, 2021 at 9:54
  • 1
    The issue is that you have uncommitted changes. I don't believe git entertains merging uncommitted changes. Git looks for a clean working tree. You need to tell it explicitly what to do with the uncommitted changes. But I am also interested in knowing the workaround. Lets hope someone chimes in. Commented Apr 4, 2021 at 10:26
  • 1
    You have a few options, delete, stash, or commit the changes. Commented Apr 4, 2021 at 13:04
  • 1
    As far as I know, all git commands are local except pull, fetch, and push. Commented Apr 4, 2021 at 13:11

1 Answer 1

1

There is no simple easy way to do this. The simplest, best way to go about this is to stash your changes, and then pull.

The reason this is the case is that any sort of merge that happens here involves the index. The merge would write changes into the index and then check out the files. Your changes may or may not be in the index. If you have not run git add on that particular version, it will not be in the index, and any merge operation would destroy it during the checkout rather than actually performing a merge.

If those changes are in the index, you could try to do a git fetch and then perform a merge yourself with git read-tree -um, checking out the files. However, in this case, the three-way merge that Git does isn't the same as a normal recursive merge, so you lose any ability to have things like renames work. As such, while this is possible, it's not a good choice, and you will likely be unhappy with the results. You'd also have to do the merge base calculation yourself (or script it), which is a bunch of hassle.

So the best solution is to stash here.

Sign up to request clarification or add additional context in comments.

1 Comment

this answers - The reason this is the case is that any sort of merge that happens here involves the index +1