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:
foo.bar
Please, commit your changes or stash them before you can merge.
Aborting
But isn't there a way to let git pull
do the stash
and pop
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.
As you already mentioned this is the way to do it. You can use it in alias to save you typing and to use shortcut or you can use it in a single line (can be an alias as well)
git stash && git pull --rebase && git stash pop
It will do the same thing as you done but in a single line (&&) and is you set as alias it will even be shorter.
The following lines will display the incoming/outgoing changes before you pull/push
As the comment above stated, setting the two config values doesn't currently work with
git pull
, as the autostash config only applies to actual rebases. These git commands do what you want:Or set it as an alias:
Then do:
Of course, this alias can be renamed as desired.
To save few seconds for oncoming explorers, here is a summary (thanks to @VonC):
With Git 2.6+ you can use the following:
This
--rebase
makes git-pull userebase
instead ofmerge
, so settings/options like--ff-only
won't apply.I am using an alias to pull with
--ff-only
by default (git pull --ff-only
), and can then usegup
(from above) in case a fast-forward merge is not possible or there are stashed changes.For Git 2.6+ (released 28 Sept 2015)
The only
git config
setting which would be of interest is:combine that with:
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)Note: if you want to pull without autostash (even though
rebase.autoStash true
is set), you have since git 2.9 (June 2016):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:
Warning: before Git 2.14 (Q3 2017), "
git pull --rebase --autostash
" didn't auto-stash when the local history fast-forwards to the upstream.See commit f15e7cf (01 Jun 2017) by Tyler Brazier (
tylerbrazier
).(Merged by Junio C Hamano --
gitster
-- in commit 35898ea, 05 Jun 2017)Update: Mariusz Pawelski asks in the comments an interesting question:
Answer:
The original thread discussing this autostash feature, it was implemented originally both for
git pull
(merge) andgit pull --rebase
.But... Junio C Hamano (Git maintainer) noted that:
So, regarding a classic pull-merge, it is better to: