Error: Cannot pull with rebase: You have unstaged changes

310,112

Solution 1

Do git status, this will show you what files have changed. Since you stated that you don't want to keep the changes you can do git checkout -- <file name> or git reset --hard to get rid of the changes.

For the most part, git will tell you what to do about changes. For example, your error message said to git stash your changes. This would be if you wanted to keep them. After pulling, you would then do git stash pop and your changes would be reapplied.

git status also has how to get rid of changes depending on if the file is staged for commit or not.

Solution 2

If you want to keep your working changes while performing a rebase, you can use --autostash. From the documentation:

Before starting rebase, stash local modifications away (see git-stash[1]) if needed, and apply the stash when done.

For example:

git pull --rebase --autostash

Solution 3

Pulling with rebase is a good practice in general.

However you cannot do that if your index is not clean, i.e. you have made changes that have not been committed.

You can do this to work around, assuming you want to keep your changes:

  1. stash your changes with: git stash
  2. pull from master with rebase
  3. reapply the changes you stashed in (1) with: git stash apply sta[email protected]{0} or the simpler git stash pop

Solution 4

First start with a git status

See if you have any pending changes. To discard them, run

note that you will lose your changes by running this

git reset --hard

Solution 5

If you want to automatically stash your changes and unstash them for every rebase, you can do this:

git config --global rebase.autoStash true
Share:
310,112

Related videos on Youtube

user3597950
Author by

user3597950

Updated on October 22, 2021

Comments

  • user3597950
    user3597950 about 1 year

    I have started collaborating with a few friends on a project & they use the heroku git repository.

    I cloned the repository a few days ago and they have since made some changes so I am trying to get the latest updates

    I ran the git pull --rebase command as stated here(Is this the right way to do it?): https://devcenter.heroku.com/articles/sharing#merging-code-changes

    I get the following error:

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

    My guess is that I messed around with the code and now it wants me to either commit or discard(is that what does stash means?) the changes. Is this what is happening? If this is the case I would like to discard any changes I might have made and just get the updated code from the git repository.

    Any idea of what I can do?

  • user3597950
    user3597950 over 8 years
    This seemed to have work but now I am facing another error(started a new questions to not confuse future vistors): stackoverflow.com/questions/23518247/…
  • user3597950
    user3597950 over 8 years
    This seemed to have work but now I am facing another error(started a new questions to not confuse future vistors): stackoverflow.com/questions/23518247/…
  • user3597950
    user3597950 over 8 years
    This seemed to have work but now I am facing another error(started a new questions to not confuse future vistors): stackoverflow.com/questions/23518247/…
  • Kostas Rousis
    Kostas Rousis over 8 years
    @nehemiahjacob You can also git stash pop to apply the most recently stashed changes and to avoid memorizing the longer apply [email protected]{0}.
  • Alper
    Alper almost 7 years
    I have to do this all the time. Any easier way?
  • Kostas Rousis
    Kostas Rousis almost 7 years
    @alper usually you work on another (feature) branch. In my experience, I only fetch and rebase against master after I have committed my work, so there's no need to stash/pop. If you find yourself though doing that a lot during your development workflow, you can always make an alias in your .bashrc (or whatever you use): alias stashpull='git stash; git pull; git stash pop'
  • Erik Berkun-Drevnig
    Erik Berkun-Drevnig about 5 years
    Ah autostash, that saves me an extra two commands. This should be the correct answer IMO.
  • adl
    adl almost 5 years
    This is the modern answer.
  • Zoredache
    Zoredache over 4 years
    If you want autostash to be the default behavior you can set git config --global rebase.autoStash true Then you don't need to pass the switch.
  • jjmontes
    jjmontes about 4 years
    THIS, what I was looking for! (note that the command line switch is available from git 2.9, but the option rebase.autostash is available from 2.6).
  • Dan Dascalescu
    Dan Dascalescu almost 4 years
    This works great for git rebase --interactive too!
  • Nick
    Nick over 3 years
    Why isn't this the default?
  • Dan Korn
    Dan Korn over 3 years
    Thank you! I needed the --autostash flag for git rebase master!
  • luizfls
    luizfls almost 3 years
    @KostasRousis I have that exact alias, lol. I call it 'sppgit'
  • Karma Blackshaw
    Karma Blackshaw over 2 years
    I'm literally having this every single time, just recently. Before, pulling files that doesn't affect your current changes is fine but now, it requires everything you've change to stash. I cannot even push, I'm forced to use git push -f
  • Schleis
    Schleis over 2 years
    @KarmaBlackshaw You shouldn't be need to force push. If you have to do a force push, that means that your local history and the remote history are different and that is a different question than what this answer covers.
  • Karma Blackshaw
    Karma Blackshaw over 2 years
    It is indeed a different that what the scope of this question provides. But I did found a way by deleting the current branch and making a new branch from develop. I guess it was my branch that had some things configured wrongfully.
  • Andrew Koster
    Andrew Koster over 2 years
    This was the default, until like this week. No idea why they changed it.
  • Andrew Koster
    Andrew Koster over 2 years
    This is the only answer that makes the newest version of Git work the same way Git has always worked in the past. Why make people add --autostash when it can just be... automatic?
  • agirault
    agirault over 2 years
    You can also set it as default in your global git config, see here
  • osexpert
    osexpert over 1 year
    What is funny(?) is that this is like subversion has worked since like forever. The defaults in git are just out of contact with reality.
  • Vittore Marcas
    Vittore Marcas 12 months
    Use "git status" to check your local workspace. And use git use "git checkout -- <file>..." to discard changes in working directory. Also, "git pull --rebase" will OK.