15

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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
humazed
  • 74,687
  • 32
  • 99
  • 138
  • Can you show us the rest of the error message? What is the state of your working directory and your origin/master branch? You can use `git log` to fetch this information. – Adam Arold Sep 07 '15 at 21:07
  • 1
    Possible duplicate of [How to ignore error on git pull about my local changes would be overwritten by merge?](http://stackoverflow.com/questions/14318234/how-to-ignore-error-on-git-pull-about-my-local-changes-would-be-overwritten-by-m) – kenorb Mar 16 '16 at 12:43
  • 1
    Possible duplicate of [How do I resolve git saying "Commit your changes or stash them before you can merge"?](http://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me) – kenorb Jul 24 '16 at 12:22

1 Answers1

30

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:

Community
  • 1
  • 1
seanlinsley
  • 3,165
  • 2
  • 25
  • 26
  • thanks a lot man, the rebase method worked very well, however I didn't fully understand what happened but I will spend more time on learning git. – humazed Sep 08 '15 at 01:01
  • and Please if you can explain why did this problem occurs in the first place, so I didn't fall in it again. – humazed Sep 08 '15 at 01:15
  • 2
    The problem occurred in the first place because you had changes locally that hadn't yet been committed. You can see those by running `git status`. – seanlinsley Sep 08 '15 at 16:50
  • 3
    Both merges and rebases can't be run if there are uncommitted changes locally. – seanlinsley Sep 08 '15 at 16:50
  • @seanlinsley This answer is one of the greatest answers on this site. Thanks a lot! – mertyildiran Jan 23 '17 at 02:09