Error: Cannot pull with rebase: You have unstaged changes
Asked Answered
A

12

265

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?

Allembracing answered 7/5, 2014 at 12:12 Comment(0)
P
249

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.

Ponder answered 7/5, 2014 at 12:20 Comment(6)
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/…Allembracing
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 -fLunge
@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.Ponder
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.Lunge
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.Subshrub
When I stash them, all modified files are rebased to the master version, but the newly added files remain. I need to take a copy of the modified files if I need them.Burier
Z
326

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
Zulema answered 6/4, 2017 at 18:20 Comment(9)
This is the modern answer.Gary
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.Galingale
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).Werewolf
This works great for git rebase --interactive too!Cletacleti
Why isn't this the default?Sulla
Thank you! I needed the --autostash flag for git rebase master!Peerless
This was the default, until like this week. No idea why they changed it.Heigl
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.Utah
after adding below commands: git pull --rebase --autostash add this command: git stashLatoyia
P
249

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.

Ponder answered 7/5, 2014 at 12:20 Comment(6)
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/…Allembracing
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 -fLunge
@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.Ponder
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.Lunge
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.Subshrub
When I stash them, all modified files are rebased to the master version, but the newly added files remain. I need to take a copy of the modified files if I need them.Burier
S
71

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

git config --global rebase.autoStash true
Shift answered 26/3, 2020 at 22:40 Comment(2)
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?Heigl
This almost makes git usable. Closer to the sane way that mercurial works anyway.Agincourt
R
66

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 stash@{0} or the simpler git stash pop
Ramberg answered 7/5, 2014 at 12:20 Comment(6)
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/…Allembracing
@nehemiahjacob You can also git stash pop to apply the most recently stashed changes and to avoid memorizing the longer apply stash@{0}.Ramberg
I have to do this all the time. Any easier way?Edmondson
@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'Ramberg
@KostasRousis I have that exact alias, lol. I call it 'sppgit'Jury
Thanks for this. I never bothered to learn about git stash and always just manually committed any changes or put them into a different branch when I wanted to pull.Reiser
C
48

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
Calyx answered 7/5, 2014 at 12:20 Comment(2)
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/…Allembracing
I still get an error: fatal: Not possible to fast-forward, aborting.Potpourri
J
23

This works for me:

git fetch
git rebase --autostash FETCH_HEAD
Jennette answered 9/11, 2017 at 15:14 Comment(3)
Ah autostash, that saves me an extra two commands. This should be the correct answer IMO.Tilton
You can also set it as default in your global git config, see hereShift
after adding below commands: git fetch git rebase --autostash FETCH_HEAD add this command: git stashLatoyia
W
11

You can always do

git fetch && git merge --ff-only origin/master

and you will either get (a) no change if you have uncommitted changes that conflict with upstream changes or (b) the same effect as stash/pull/apply: a rebase to put you on the latest changes from HEAD and your uncommitted changes left as is.

Welloiled answered 23/7, 2014 at 20:35 Comment(0)
B
7

When the unstaged change is because git is attempting to fix eol conventions on a file (as is always my case), no amount of stashing or checking-out or resetting will make it go away.

However, if the intent is really to rebase and ignore unstaged changed, then what I do is delete the branch locally then check it out again.

git checkout -f anyotherbranchthanthisone
git branch -D thebranchineedtorebase
git checkout thebranchineedtorebase

Voila! It hasn't failed me yet.

Breeding answered 26/5, 2017 at 14:9 Comment(0)
S
2

This is because you made some changes on your workspace, but you do not commit

that changed codes into local repository via

git add/rm <file> && git commit -m "Add/Delete file."

Just use git status to check which files have been changed, as you say

I would like to discard any changes I might have made and just get the updated code from the git repository.

I just recommend you to delete your older cloned repository, then clone the latest updates if that works for you.

Or, just use git reset --hard && git pull --no-rebase && git push if this works for you.

Subshrub answered 20/2, 2023 at 11:27 Comment(0)
D
1

Step1 : git stash // It is stash your local changes in a safe place

Step2 : git pull // Pull the latest changes

Step3 : git stash pop // It will move the stash file to your branch

To verify please run : git status

Desmonddesmoulins answered 13/9, 2023 at 14:37 Comment(0)
S
0

Follow the below steps

From feature/branch (enter the below command)

git checkout master

git pull

git checkout feature/branchname

git merge master

Sessile answered 12/10, 2020 at 13:46 Comment(0)
M
0

there you just need to restore the unstagged changes with this command below

for my case i had errors in yarn file for your case it can be another file

git restore --staged yarn.lock

Mindymine answered 22/10, 2021 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.