8

Example:

  • I cloned the Github repository, created a new branch and started working on a feature.

  • My co-worker clones the same exact Github repository, creates his own branch on his local machine (git checkout -b mylocalbranch), and works on his own feature.

  • My co-worker finishes his feature before I do and pushes it to production and back to Github.

  • I finish my feature 30 minutes later and want to push my work without causing any conflicts.

What is the best way to push my work to production and back to Github without causing any conflicts?

Smooth
  • 956
  • 1
  • 15
  • 37
  • A quick explanation or a link to a relevant post would be greatly appreciated. – Smooth Dec 19 '12 at 19:28
  • When you say "creates his own branch", do you mean specifically he creates a new local branch on his computer (git branch MyNewBranch), or just that his act of cloning the repo creates a new "branch" on his system, but he's still working in the "master" branch? – Mark Hildreth Dec 19 '12 at 19:31
  • Creates a new branch on his local machine with git checkout -b "mynewbranch" – Smooth Dec 19 '12 at 20:36
  • To simplify things, let's call his branch "new_feature". In that case, has he merged "new_feature" into master before pushing master back to Git (git checkout master; git merge new_feature; git push)? Or did he create a new branch on github (git checkout new_feature; git push -u new_feature)? – Mark Hildreth Dec 19 '12 at 20:59
  • He has merged "new_feature" into master and then pushed master back to Git. – Smooth Dec 19 '12 at 21:04
  • In that case, it really doesn't matter that he created the new branch, it's just making the entire process more difficult to follow. I'd suggest changing the question to just say that he made changes in master, then push master to github. In the end, I don't think it would change the answer to your question. – Mark Hildreth Dec 19 '12 at 21:19
  • Mark, it seems like you understood my question best. Which answer do you recommend? – Smooth Dec 19 '12 at 21:37
  • Jon: They're all correct. If I were you, I would actually test out this exact scenario with some fake repositories, either with your co-worker or playing his role on another computer. Give the answer to the one that you feel helped you understand what you were doing the most. – Mark Hildreth Dec 19 '12 at 21:47

4 Answers4

17

Lets say your co-worker merged his code to production branch.

Now the branch yourfeature you created from earlier production is outdated a bit and has few commits by you on top of it.

What you have to do now is :

  • switch to production branch
  • Pull it (so you have latest changes including changes by ur co-worker)
  • go to your yourfeature branch
  • Rebase your branch with production : This will replay your commits of this branch over the latest production.
  • If any conflicts arise: They are due to the same line changes made by your co-worker where you are also updating something.

Read more on rebasing here

Rahul garg
  • 9,202
  • 5
  • 34
  • 67
1

Well, you could checkout the branch you wish to merge into,

git checkout master

then merge your code

git merge [current_branch]

There is always the possibility of conflicts and in that case you simply have to deal with them and resolve them.

This article should help: http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging

Foggzie
  • 9,691
  • 1
  • 31
  • 48
0

Quick way is, make sure your changes are committed, pull the changes your teammate made by using git pull, merge the branch into yours with git merge branchname, merge your branch into master by git checkout master git merge yourbranchname, then git push the changes back up to the remote repo.

Other relevant links:

Merging between forks in GitHub

Merge changes from remote github repository to your local repository

Community
  • 1
  • 1
Chalise
  • 3,656
  • 1
  • 24
  • 37
0

This blog post is very helpful:

Conclusions

Avoid the merge workflow, especially if you have many committers or you have less-trained committers.

Understand how the distributed nature of git changes the game.

Turn on system receive.denyNonFastForwards on your authoritative repository

From: http://randyfay.com/content/avoiding-git-disasters-gory-story. The comments as well as the post are gold.

Kprof
  • 742
  • 1
  • 8
  • 16