Home > Enterprise >  How to make git pull/merge automatically merge into the working tree
How to make git pull/merge automatically merge into the working tree

Time:10-12

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 :)

CodePudding user response:

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.

CodePudding user response:

Say git pull --no-ff. This will permit Git to perform a true merge if both sides have changes that the other lacks.

However, a better solution is: don't say pull. I never do. It's a silly command and is too heavily laden with confusing configuration options. Instead, say

git fetch
git merge

The latter will do a true merge if it has to, but the point is, it will always do something, rather than balking as your pull does.

  •  Tags:  
  • git
  • Related