9

I get this message:

Cannot pull with rebase: You have unstaged changes.
Please commit or stash them.

Yes, I have changes which are not committed. I searched a way to rebase my uncommitted changes on top of the new code which I would get from a pull.

I found this: https://github.com/aanand/git-up

I want to know if this is still the way to go, or if there are more modern ways to go.

I use git version 1.8.1

guettli
  • 25,042
  • 81
  • 346
  • 663

4 Answers4

9

git-up is probably the more sophisticated way to solve this issue.
Otherwise, you need to stash, rebase and stash pop.

The "more modern way" will be available in git 1.8.5 (or 1.9, Q4 2013).
As I mention in "Git - How to edit old (not previous) commit with some of the unstaged changes from current index (current state)?":

"git rebase" learned "--[no-]autostash" option to save local changes instead of refusing to run (to which people's normal response was to stash them and re-run).


Since Git 2.9 (June 2016), you now have (as commented by artofwarfare):

git pull --rebase --autostash
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Just do `git pull --rebase --autostash`. Seems to have done exactly what I wanted. – ArtOfWarfare Oct 18 '17 at 14:06
  • @ArtOfWarfare Actually these days (I.e.4 year after this old answer), I prefer a simple `git pull`, because of `git config pull.rebase true; git config rebase.autoStash true` https://stackoverflow.com/a/30209750/6309: – VonC Oct 18 '17 at 14:12
  • I'm writing a script which I expect multiple people at my company will be using. I don't want it to only work if they have all that configured in `git` (and I don't want to have the script change or validate their configuration.) I want it to just work, and the command in my first comment seems to do so. – ArtOfWarfare Oct 18 '17 at 14:17
  • @ArtOfWarfare I agree, and have included that newer command in the answer for more visibility. But your people at your company must have Git 2.9+ (June 2016). – VonC Oct 18 '17 at 14:20
5

You can't really "rebase" your uncommitted changes since git does not know about them yet. You should stash your local changes before you run git pull --rebase then apply them back.

eoconnell
  • 101
  • 4
  • I know that this is possible, but a one-liner would be nice. – guettli Sep 18 '13 at 08:01
  • You could always write a bash script that turns those three steps into a one-liner. – eoconnell Sep 18 '13 at 14:36
  • *"You can't really "rebase" your uncommitted changes since git does not know about them yet..."* - git may not know about them, but the damn tool refuses to run nearly every command or even switch branches.... – jww May 02 '16 at 18:57
  • It's there as a safety precaution. Git doesn't want you to lose your changes. As VonC mentions, you can set the --autostash flag for one-offs or set rebase.autostash=true in your git config to apply it globally. – eoconnell May 09 '16 at 21:54
4

You can use the Python port of git-up: https://github.com/msiemens/PyGitUp

pip install git-up
guettli
  • 25,042
  • 81
  • 346
  • 663
  • Just for the records: Since you loose history information if you use rebase, we decided to use "merge" in our company. – guettli Sep 02 '14 at 07:50
3

I answer a little late but maybe that can be useful for someone.

If you are just looking for a one-liner to execute stash / pull rebase / stash pop, you can create an alias.

git config --global alias.spr '!f(){ git stash && git pull --rebase && git stash pop; };f'

This creates an alias named spr that does the three operations and allows you to quickly pull --rebase while you have unstaged changes.

git spr
Alexis N-o
  • 3,954
  • 26
  • 34