0

Trying to understand git behavior.

When I execute git pull I get a warning

error: Your local changes to the following files would be overwritten
by merge:
        foo/bar
Please, commit your changes or stash them before you can merge. Aborting

But if I git stash save, git pull, and then git stash pop, the changes are applied without any problem.

Auto-merging foo/bar
...

Why can't/won't git pull do the same thing?

joanis
  • 10,635
  • 14
  • 30
  • 40
Chaim Geretz
  • 826
  • 5
  • 23
  • 1
    `git pull` = `git fetch` + `git merge` ... pulling does a merge, hence the error message. – Tim Biegeleisen Jun 30 '16 at 13:38
  • Note that if you want a smarter `git pull` that does the stash/pull/pop for you, consider [git-smart](https://github.com/geelen/git-smart) and it's `git smart-pull` command. – user229044 Jun 30 '16 at 13:40
  • the git stash save/pop workflow works on uncommitted code. How does git know what you want to do w/ your uncommitted code? I'd rather it prompt you to say what you want rather than just either blowing it away w/ a merge OR automatically stashing it away. You must handle uncommitted code manually before you try to merge your stuff w/ upstream changes (purpose of pull...) – g19fanatic Jun 30 '16 at 13:46

1 Answers1

2

There is no guarantee, in the general case, that the merge can be performed automatically. It may leave conflicts that need manual resolution. In this case, it would be quite frustrating to the user if they forgot about local changes they were working on and just wanted to "undo" the merge to get back to where they were. If you commit or stash first, you have the ability to abort/undo the merge, because if you committed then you'll still have the commit around and if the stash pop did not auto-merge without conflicts, then the stash will not be removed.

jholtrop
  • 406
  • 4
  • 6