How can I merge remote branch to local branch?

Our project has a develop branch. I created a bug-fix branch based on the develop branch, and changed one file a.java. And I know another people changed the a.java and merged to develop branch when I was fixing the bug.

Now I have fixed the bug, I want to merge my code to develop branch, but before I do that, I want to pull the changes(since other guy changed the a.java as well) to my local branch to test if it works, am I right? How can I do it?

Firstly I pulled all the code by 'git pull', and I am in my bug-fix branch. then run 'git merge develop', it output empty. Then I 'git merge origin/develop'.It output below errors.

Updating 46f689b..3011608
error: Your local changes to the following files would be overwritten by merge:
        projectA/a.java
Please commit your changes or stash them before you merge.
Aborting

How can I merge the develop branch to my working directory?Thanks.

4 answers

  • answered 2018-07-12 02:14 scharette

    Normally in this situation here is what I like to do,

    git stash # Stash your local changes
    git pull  # Update code
    git stash apply # Merge your local changes
    

    Now, for clarification, you can switch branch before your git pull if you need to synch another branch. From there you can come back on your bug-fixes branch and run git stash apply to then merge with your develop branch.

  • answered 2018-07-12 02:15 Vuong

    Because you have some changes on that file

    Please commit your changes or stash them before you merge.
    

    Just follow this:

    # save current work state
    git stash
    # merge from develop
    git merge origin/develop
    # recover current work state (get the last item of stash)
    git stash pop
    

  • answered 2018-07-12 02:16 Steve Mulvihill

    You need to commit or stash your changes to projectA/a.java before merging in other changes.

    Commit Example:

    $ git add projectA/a.java
    $ git commit -m "my change message"
    $ git pull origin/develop
    

    Stash Example:

    $ git stash
    $ git pull origin/develop
    $ git stash apply
    

  • answered 2018-07-12 04:32 VonC

    Since you haven't push your local commits on the bug branch, you need to rebase them on top of the updated (by the other developer) origin/bug branch.

    For that, make sure you have set first the configuration:

    git --global config pull.rebase true
    git --global config rebase.autoStash true
    

    (to be done only once, from any folder)

    Then, while you are in your current branch, you can include contribution of other with a simple:

    git pull.
    

    Now, make sure everything is committed, and:

    git checkout develop
    git pull
    

    Finally, merge develop

    git checkout bug
    git merge develop