5

Say I have a branch A, and from that I branch B. I make a bunch of changes on A, then checkout B and do a git pull. Now I make a change on B but realize that it should've been in A. If I now try to git checkout A, I get "Your local changes to the following files would be overwritten by checkout" to the file I touched.

Why would my change be overwritten if I just did a git pull in B and haven't touched that file in A since?

1 Answer 1

9

The reason you get that message is that the underlying file (before your uncommitted modifications) is different between branch A and branch B. If the files were the same, Git would switch branches and keep the same uncommitted modifications after switching to branch A.

One way to bring these changes across is to stash them:

(on branch B)$ git stash git checkout A git stash pop 

If there are conflicting changes, you may have to resolve the conflict at this point. If there are changes but they don't conflict, then this will succeed.

Sign up to request clarification or add additional context in comments.

8 Comments

But how could it be different if I just did a git pull in branch B and a git status in branch A doesn't report any changes to that file?
You said that you made a bunch of changes on A after making branch B. It sounds like the committed changes on A are different from the committed changes on B. git status won't' tell you about those changes.
I made changes on A, then committed those changes to A, then checked out B, then did a git pull, then made uncommitted changes to B, then tried to checkout A again. I don't see how the committed changes could differ in that case :(
I stashed and popped for now, but what's the best way to debug this next time it comes up? In other words, how can I say "show me how git believes the commits to this file have diverged"?
You can use git diff A B file.txt to see what Git believes the differences are between file.txt on branch A and file.txt on branch B.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.