user8920367
user8920367

Reputation: 105

How can I merge two files into the same github repository?

We have colleagueA and colleagueB. Both download code from a GitHub repository and start to modify it, lets say app.py. Each one modifies different parts of the code in his own computer.

What is good git practice when merging both app.py into the final one to push it to the original repository?

I'm new to GitHub, I'd like simple answers if possible.

Upvotes: 1

Views: 685

Answers (1)

VonC
VonC

Reputation: 1323953

If you are both pushing to the same branch (for example master), then the second one to push (say developerB) will fail, as in here, with:

hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and merge the remote changes
hint: (e.g. 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

In that case, I recommend a git stash + git pull --rebase which will:

  • stash any current modification (not yet committed)
  • fetch the updated origin/master branch (with developerA app.py modifications
  • replay developerB local commits (which are not pushed, since the push just failed) on top of origin/master

(you can automate that git stash + git rebase process with a bit of configuration: see "Can “git pull” automatically stash and pop pending changes?")

If there are any merge conflicts, they will be resolved locally, on developerB's workstation, with local compilation and tests to validate that developerB's changes are working with what developerA has pushed.

Once the rebase is completed, a simple git push form developerB will work (assuming that developerA has not pushed again on the common branch)

You can avoid that of course by pushing to a dedicated branch for each developer, but you will need to reconcile those parallel development at some point.
At least, with the workflow I describe above, if both developers are participating to the same development effort, that reconciliation will be sooner rathen than later.

Upvotes: 1

Related Questions