How to git stash only the conflicting files in a git pull?

164 Views Asked by At

I'm looking for a command(s) to git stash only the list of files that would be overwritten/conflicting when doing a git pull, all at once. I just want to accept all the remote changes and deal with all conflicts by stashing local.

I've only been discarding local changes that conflict or stashing all changes (I want to keep those that don't conflict), which isn't ideal.

1

There are 1 best solutions below

0
hlovdal On BEST ANSWER

Stop using git stash and instead use temporary commits.

By doing that there is nothing special handling, just straight forward, normal git commands:

git commit -am "==== before pull ===="    # Temporary commit
git pull --rebase
git reset HEAD^                           # Undo temporary commit

In case of conflicts, use KDiff3 to resolve them.


The above is as simple as it gets. Alternatively if you want a bit more control (say pull brings in a lot of changes and you only want to deal with some of it at the beginning) you could work with two branches, somebranch (which is tracking origin/somebranch) and somebranch.mywork which is the unpublished work you are doing.

Then you let somebranch strictly follow origin/somebranch and then rebase somebranch.mywork on top of that. When you eventually want to push changes from somebranch.mywork you simply (fast-forward) merge those changes into somebranch and push.