what will happen when merge, if both master and branch has changed in bitbucket

130 views Asked by At

I have a branch from master in my local machine. I know how to merge something to master repo. but question is this.

Think some other developer has changed the master repository by pushing changes to it and at the same time I'm going to merge changes to the master repository from by branch.

What will happen at this situation.

What should I do in this type of situation. I tried to do following from my branch.

  • added my changes with -> git add *
  • then committed with -> git commit -m "my commit"
  • then push to my branch with -> git push -u origin my_branch_name
  • then changed the repository to master with -> git checkout master
  • then merged branch to master with -> git merge my_branch_name

up to this stage it was successful. then I tried to push with following command (before few minutes ago, another developer has pushed to master)

  • git push origin master

then it says followings.

! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:abcdef/cups.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

at this stage, what should I do.

  1. should I get a pull after the merged step and then push (in this stage if i get a pull, what will happen.) or
  2. do I have to do something like git stash, then push and something like that.

hope your help with this. thank your very much.

2

There are 2 answers

0
Sajib Khan On BEST ANSWER

You need to Pull remote/master (to get all remote changes), then you are able to push your local changes.

  1. Say, When you pulled remote/master there were 2 commits (A, B)

    remote/master: A -> B
    local/master:  A -> B
    
  2. Then other developer pushed a commit P to master

    remote/master: A -> B -> P
    local/master:  A -> B
    
  3. Then you've committed X in your branch (say, feature)

    remote/master: A -> B -> P
    local/master:  A -> B
    local/feature: A -> B -> X
    
  4. Now Merged your feature with local/master

    remote/master: A -> B -> P
    local/master:  A -> B -> X
    local/feature: A -> B -> X
    
  5. Now you've to pull your remote/master to fetch all commits of remote/master into local/master.

    remote/master: A -> B -> P
    local/master:  A -> B -> P -> X    # now local/master has P (sync with remote/master)
    local/feature: A -> B -> X  
    
  6. Push your local/master to remote

    remote/master: A -> B -> P -> X
    local/master:  A -> B -> P -> X
    local/feature: A -> B -> X
    
0
iclman On

You need to retrieve the work that has been pushed by the other person and integrate it locally before being able to push. You can either perform a "git pull" directly, or better, perform a "git fetch", followed by either a "git merge" or a "git rebase". The advantage of the fetch is to allow you to see what commit has been pushed by the other user. You can then decide to do a merge or a rebase. The rebase has the advantage of resulting in a "cleaner" tree.

If you have some ongoing work (files that you have staged), you need to decide if you want to integrate that in your next push. You then need to decide to discard them, stash them or integrate them to your next commit.