Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't merge origin/master: error: Your local changes to the following files would be overwritten by merge

I make some changes and commits to my project locally but didn't push them, then I change files in GitHub by adding new file and commit.

when I try to push my local commits android studio suggested to merge but when I try to merge it give me that error and whatever I do keep refusing to merge and show me that message.

enter image description here

and when I click merge shows me.

Error message:

enter image description here

My log:

enter image description here

What should I do?

like image 874
humazed Avatar asked Sep 07 '15 21:09

humazed


People also ask

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 you fix error your local changes to the following files would be overwritten by checkout?

The Git “Your local changes to the following files would be overwritten by checkout” error occurs when you make changes on two branches without committing or stashing those changes and try to navigate between the branches. You can fix this issue by either stashing your changes for later or adding them to a commit.

Will git merge overwrite my changes?

With default strategy and default text merge driver, if there is no conflict, git never overwrites anything at merge. If there is a conflict then what "git does" fully depends on how person performing merge resolves the conflicts.


1 Answers

Your local changes will need to be stashed away while you perform the merge. To do that, git provides git stash to save your uncommitted changes to a temporary location, and git stash pop to apply them back to your local code.

This should work:

git stash
git pull origin master
git stash pop

Here's a good website to learn more about git: http://gitready.com/beginner/2009/03/13/smartly-save-stashes.html

But after looking at your screenshot, a merge doesn't seem like the best option for you. Instead a rebase would make more sense.

git stash
git pull --rebase origin master
git stash pop

Here are some resources to understand the difference between a merge and a rebase:

  • http://gitready.com/intermediate/2009/01/31/intro-to-rebase.html
  • https://stackoverflow.com/a/3357174/2651774
like image 188
seanlinsley Avatar answered Oct 23 '22 05:10

seanlinsley