1

This question is about the apparent inconsistency in git behavior. It is not about how to solve "Cannot pull with rebase" -- that I already know.

We've all been in this situation: make some local changes, and when preparing to commit realize that we're in the wrong branch. So we need to switch branch first:

git checkout master
git pull

My question is this: sometimes I get the message "error: Your local changes to the following files would be overwritten by checkout:" right when doing the checkout; some other times, checkout works, I see some merging being done on the modified files, but then the message "Cannot pull with rebase: You have unstaged changes"is given when doing the pull. Why is the error not always given on checkout, or always on pull?

memo
  • 1,868
  • 11
  • 19
  • `git fetch && git rebase --autostash`? – Biffen Aug 17 '18 at 09:25
  • 1
    Possible duplicate of [Error: Cannot pull with rebase: You have unstaged changes](https://stackoverflow.com/questions/23517464/error-cannot-pull-with-rebase-you-have-unstaged-changes) – phd Aug 17 '18 at 13:26
  • https://stackoverflow.com/questions/52704/how-do-i-discard-unstaged-changes-in-git – phd Aug 17 '18 at 13:27
  • 1
    Not quite a duplicate (focus is specifically on checkout) but see also https://stackoverflow.com/q/22053757/1256452 – torek Aug 17 '18 at 15:18

1 Answers1

0

The error error: Your local changes to the following files would be overwritten by checkout is not always given on checkout because the checkout would not always overwrite your local changes, even though you have local changes.

In other words, git does not forbid checking out new changes just because you have local changes, unless that would include changes to a specific file for which you have local changes.

So when the chekcout would clobber a file, you get the first error. When it wouldn't, the checkout is fine but then you get the second error.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52