Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you git push but someone else has already committed something you don't have on your local repo?

Sorry, I'm quite a newbie at Git, and it is hard to find answers for such hard-to-phrase questions (at least for me) on Google.

Say my name is Joe, and I'm working on a software program with my colleague Bob.

We have the upstream Repo A and we have clones of Repo A on our local machines, where we code, commit, and push periodically to the same branch, say master. I know this is not technically the best way to do it, but for example's sake.

Now, what happens if we are both working on one file. Bob's local file that he did not yet push has the contents

// awesome program
[some edits by Bob]

My local file that I did not yet push has the contents

// awesome program
[some edits I made]

Bob pushes, a second later I push. What happens now?

Is the file on remote going to be

// awesome program
[some edits I made]

or

// awesome program
[some edits Bob made]

or perhaps, if there is a way to do it, I would like it to be

// awesome program
[some edits Bob made]
[some edits I made]

Sorry for the noobishness :(

like image 770
Joseph Avatar asked Jul 08 '16 16:07

Joseph


2 Answers

Git will refuse your push. It will tell you there are new changes on the server, and make you do a pull before you can push. So, you'll end up with

// awesome program
[some edits Bob made]
[some edits I made]

on your local machine. Then you'll be able to push that out to the remote.

like image 187
mwarsco Avatar answered Oct 25 '22 22:10

mwarsco


First: you can't push in a normal manner with a regular git push because when you were on local master your remote master has changed. so git propose you to do a force push with -f option. i.e. git push -f But Its not recommended to do force push on a shared branch unless you have a fair reason like interactive rebasing!

Second: By this message of git you are informed that something is happend to your remote repository so you do a git status and yes it tells that "Your branch and 'origin/master' have diverged, and have 1 and 1 different commit(s) each, respectively.". you should update your local repo; so you are doing a git pull or in this situation a better command, git pull --rebase and Surprisingly! Git also refuse your pull and tells that you have conflicts in example file. that's because both of you and Bob has editing the same file and the same lines!

Finally: See this link and resolve the conflict. you can keep your changes or Bob's or Both. then commit out the changes and git push to also update your remote repo. and that's it !!!

like image 24
dNitro Avatar answered Oct 25 '22 23:10

dNitro