guettli guettli - 1 year ago 86
Git Question

Can "git pull" automatically stash and pop pending changes?

I know how to solve this:

user@host$ git pull
Updating 9386059..6e3ffde
error: Your local changes to the following files would be overwritten by merge:
Please, commit your changes or stash them before you can merge.

But isn't there a way to let
git pull
do the
dance for me?

If this command has a different name, it's ok.

Creating a shell alias for
git stash; git pull; git stash pop
is a solution, but I search for a better solution.

Answer Source

For Git 2.6+ (released 28 Sept 2015)

The only git config setting which would be of interest is:


When set to true, automatically create a temporary stash before the operation begins, and apply it after the operation ends.
This means that you can run rebase on a dirty worktree.

However, use with care: the final stash application after a successful rebase might result in non-trivial conflicts. Defaults to false.

combine that with:


When true, rebase branches on top of the fetched branch, instead of merging the default branch from the default remote when "git pull" is run.

git config pull.rebase true
git config rebase.autoStash true

That would be enough for a simple git pull to work even in a dirty tree.
No alias needed in that case.

See commit 53c76dc (04 Jul 2015) by Kevin Daudt (Ikke).
(Merged by Junio C Hamano -- gitster -- in commit e69b408, 17 Aug 2015)

pull: allow dirty tree when rebase.autostash enabled

rebase learned to stash changes when it encounters a dirty work tree, but git pull --rebase does not.

Only verify if the working tree is dirty when rebase.autostash is not enabled.

Note: if you want to pull without autostash (even though rebase.autoStash true is set), you have since git 2.9 (June 2016):

 pull --rebase --no-autostash

See commit 450dd1d, commit 1662297, commit 44a59ff, commit 5c82bcd, commit 6ddc97c, commit eff960b, commit efa195d (02 Apr 2016), and commit f66398e, commit c48d73b (21 Mar 2016) by Mehul Jain (mehul2029).
(Merged by Junio C Hamano -- gitster -- in commit 7c137bb, 13 Apr 2016)

Commit f66398e in particular includes:

pull --rebase: add --[no-]autostash flag

If rebase.autoStash configuration variable is set, there is no way to override it for "git pull --rebase" from the command line.

Teach "git pull --rebase" the --[no-]autostash command line flag which overrides the current value of rebase.autoStash, if set. As "git rebase" understands the --[no-]autostash option, it's just a matter of passing the option to underlying "git rebase" when "git pull --rebase" is called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download