0

I've found surprisingly little on this topic after searching. The only similar question I found on SO is this one:

Why git pull won't/can't merge local changes?

which doesn't really answer the question IMO. I recall when using previous version control systems such as SVN that when you pull in new changes it will automatically merge them in with your working tree, even when they touch the same file.

For me, the vast majority of the time, when I run a git pull, it is theoretically possible to merge into my local changes without any merge conflicts, even when the same files are touched. In those cases, how can I get git to do this merge automatically at least in the case when the merge can be done with no conflicts?

I'd be happy with the current behavior (of having to first stash) if git detects that there would be merge conflicts; but otherwise, I'd like it to just do it automatically. Actually, the ideal behavior for me would be that, even in files where there would be merge conflicts, it will leave the <<<< markers so that I can go in and fix it.

Is there an option to do this that I am missing? If not, is there a reason that the git designers have not allowed this (even as an opt-in)? Seems strange that more people haven't complained about this :)

dpacbach
  • 121
  • 1
  • 6
  • `git pull` does automerge into the working tree. That's exactly what it does. What does it do for you that's different from what you expect? – matt Oct 11 '22 at 13:38
  • @matt It automerges only if the sets of files modified locally and remotely are disjoint. If there are non-conflicting changes to the same file both remotely and locally, `git pull` refuses to proceed. – joanis Oct 11 '22 at 13:41
  • @joanis Then the user can simply say `git pull --no-ff` as a one-off (or every time, if desired). – matt Oct 11 '22 at 13:53
  • 1
    @matt OP wants to automate the merge, not block fast forward merges. I just tested anyway, and where there are local and remote changes to a given file, `git pull --no-ff` also refuses to proceed. – joanis Oct 11 '22 at 13:54
  • My advice is: make a commit, and use `git fetch` followed by `git rebase`. The stuff SVN used to do was nasty. – torek Oct 11 '22 at 20:45

1 Answers1

2

I believe the option you are looking for is --autostash, which automatically does the stashing and unstashing for you:

git pull --autostash

From git pull -h:

--autostash automatically stash/stash pop before and after

I'm personally not a fan of such automation because I like having more control on my merges, but I just tested and it works well:

  • when there is no conflict, it does just what you asked, merging your local changes and the remote ones together;
  • when there is a conflict, that information is displayed and the stash is kept so you can fix things as necessary.
joanis
  • 10,635
  • 14
  • 30
  • 40
  • Thanks, this would be what I wanted, but it seems to give me an error: "fatal: --[no-]autostash option is only valid with --rebase." So looks like it only works when rebasing, which is not typically what I do. I'm using git 2.25.1 at the moment... are you using a different version? – dpacbach Oct 12 '22 at 14:44
  • I'm on git version 2.37.3.windows.1. You should consider updating your Git because you are missing a number of critical vulnerability patches, including the one described here: https://github.blog/2022-04-12-git-security-vulnerability-announced/ – joanis Oct 12 '22 at 18:17
  • thanks; after trying a later version of git I got it working, and it does indeed do exactly what I wanted. Thanks! – dpacbach Oct 13 '22 at 15:13