0

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.

Nick K
  • 23
  • 3
  • You couldn't partially stash. Stash results in clean working directory. You could `stash`, `pull`, `stash pop` and resolve your conflicts (in you case by resetting conflicting files, I suppose) – markalex Mar 10 '23 at 21:27
  • @markalex One can [`git stash push`](https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-push-p--patch-S--staged-k--no-keep-index-u--include-untracked-a--all-q--quiet-m--messageltmessagegt--pathspec-from-fileltfilegt--pathspec-file-nul--ltpathspecgt82308203) a list of files. I.e., it's possible to stash partially. – phd Mar 10 '23 at 21:31
  • @phd, sorry, was wrong. Then I guess it might be even possible to do what OP asked. – markalex Mar 10 '23 at 21:44
  • Hey, wouldn't `git pull -X theirs` be sufficient for this case? Taken from [here](https://stackoverflow.com/questions/10697463/resolve-git-merge-conflicts-in-favor-of-their-changes-during-a-pull) – markalex Mar 10 '23 at 21:51
  • 1
    @markalex "*I guess it might be even possible to do what OP asked.*" I doubt it. It seems to be impossible to know in advance which files will conflict during `git pull`. – phd Mar 10 '23 at 21:54
  • @phd, right. For some reason i thought about using `diff` after `fetch` in some clever way, but I completely forgot, that we are talking about working copy, and not committed changes. – markalex Mar 10 '23 at 22:05
  • @markalex Well, I think `git fetch` + a lot of scripting could help. But I'm certainly not gonna try. :-) – phd Mar 10 '23 at 22:12
  • @phd I think we discussed it from purely theoretical standpoint, you know - for science. On practice, I believe, `git pull -X theirs` should do what author wanted. – markalex Mar 10 '23 at 22:38

1 Answers1

0

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.

hlovdal
  • 26,565
  • 10
  • 94
  • 165