Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would my local changes in Git be overwritten by checkout in this circumstance?

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?

like image 910
Aaron Avatar asked Aug 20 '12 20:08

Aaron


People also ask

Does git checkout overwrite local changes?

Checkout old commitsSince this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.

How do you fix git error your local changes to the following files will be overwritten by merge?

The “Your local changes to the following files would be overwritten by merge” error occurs when you try to pull a remote repository to your local machine whose contents conflict with the contents of your local version of the repository. To fix this error, either stash your changes away for later or commit your changes.

How do I get my local changes back in git?

If you have committed changes to a file (i.e. you have run both git add and git commit ), and want to undo those changes, then you can use git reset HEAD~ to undo your commit.

What is the impact of git checkout?

git Checkout: The git checkout is navigator command that helps to switch branches. This option prepares for you to work on a particular working branch. It Updates files in the working tree to match the version in the index or the specified tree.


1 Answers

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.

like image 171
Greg Hewgill Avatar answered Oct 16 '22 20:10

Greg Hewgill