Reputation: 864
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.
git add *
git commit -m "my commit"
git push -u origin my_branch_name
git checkout master
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.
pull
after the merged step and then push (in this stage if i get a pull, what will happen.) or git stash
, then push
and something like that.hope your help with this. thank your very much.
Upvotes: 1
Views: 121
Reputation: 24204
You need to Pull remote/master
(to get all remote changes), then you are able to push your local changes.
Say, When you pulled remote/master
there were 2 commits (A, B)
remote/master: A -> B
local/master: A -> B
Then other developer pushed a commit P
to master
remote/master: A -> B -> P
local/master: A -> B
Then you've committed X
in your branch (say, feature
)
remote/master: A -> B -> P
local/master: A -> B
local/feature: A -> B -> X
Now Merged your feature
with local/master
remote/master: A -> B -> P
local/master: A -> B -> X
local/feature: A -> B -> X
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
Push your local/master to remote
remote/master: A -> B -> P -> X
local/master: A -> B -> P -> X
local/feature: A -> B -> X
Upvotes: 1
Reputation: 1436
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.
Upvotes: 0