242

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
  • 9,201
  • 6
  • 26
  • 35

11 Answers11

281

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
mkobit
  • 43,979
  • 12
  • 156
  • 150
240

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.

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • 1
    This seemed to have work but now I am facing another error(started a new questions to not confuse future vistors): http://stackoverflow.com/questions/23518247/error-git-pull-rebase-permission-denied-publickey-fatal-could-not-read-f – user3597950 May 07 '14 at 12:47
  • 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` – Karma Blackshaw Jun 10 '20 at 03:19
  • 1
    @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. – Schleis Jun 10 '20 at 13:27
  • 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. – Karma Blackshaw Jun 10 '20 at 16:04
  • Use "git status" to check your local workspace. And use git use "git checkout -- ..." to discard changes in working directory. Also, "git pull --rebase" will OK. – Vittore Marcas Jan 01 '22 at 23:40
64

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
Kostas Rousis
  • 5,918
  • 1
  • 33
  • 38
  • 1
    This seemed to have work but now I am facing another error(started a new questions to not confuse future vistors): http://stackoverflow.com/questions/23518247/error-git-pull-rebase-permission-denied-publickey-fatal-could-not-read-f – user3597950 May 07 '14 at 12:48
  • 6
    @nehemiahjacob You can also `git stash pop` to apply the most recently stashed changes and to avoid memorizing the longer `apply stash@{0}`. – Kostas Rousis Aug 28 '14 at 08:05
  • I have to do this all the time. Any easier way? – Alper Feb 04 '16 at 13:23
  • @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'` – Kostas Rousis Feb 04 '16 at 14:08
  • @KostasRousis I have that exact alias, lol. I call it 'sppgit' – luizfls Mar 10 '20 at 14:34
  • 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. – thomasrutter Dec 04 '22 at 03:29
60

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

git config --global rebase.autoStash true
agirault
  • 2,509
  • 20
  • 24
  • 5
    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? – Andrew Koster Jun 18 '20 at 23:58
  • This almost makes git usable. Closer to the sane way that mercurial works anyway. – swpalmer Aug 26 '23 at 23:07
45

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
ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91
Igal S.
  • 13,146
  • 5
  • 30
  • 48
  • This seemed to have work but now I am facing another error(started a new questions to not confuse future vistors): http://stackoverflow.com/questions/23518247/error-git-pull-rebase-permission-denied-publickey-fatal-could-not-read-f – user3597950 May 07 '14 at 12:47
  • I still get an error: **fatal: Not possible to fast-forward, aborting.** – IgorGanapolsky Sep 14 '22 at 16:51
23

This works for me:

git fetch
git rebase --autostash FETCH_HEAD
CppChase
  • 901
  • 12
  • 25
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.

Jared Updike
  • 7,165
  • 8
  • 46
  • 72
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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
tggagne
  • 2,864
  • 1
  • 21
  • 15
0

Follow the below steps

From feature/branch (enter the below command)

git checkout master

git pull

git checkout feature/branchname

git merge master

Thiru
  • 25
  • 1
  • 8
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

Bukenya KizzaRoland
  • 721
  • 1
  • 7
  • 13
0

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.

Vittore Marcas
  • 1,007
  • 8
  • 11