1

I am working on an app with my partner and we are using github to sync our projects, but neither of us have used github before, and I am presented with a problem. My partner and I were synced from the last push, but we then worked on different things separately, and we now need to figure out how to merge our two versions. So, it looks a little like this:

    ---- B ----|
A --|          |--- D
    ---- C ----|

State A was when the github repository had code that we had in common.

Then, both of us pulled and worked on different parts of the code, creating two distinct states B and C.

Now, we need to combine our two states into D, which has both parts that we have worked on.
State B is pushed, which is the not what I have, but I do know everything from B that I need to have on C before pushing.

Any help is appreciated.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
acn3
  • 338
  • 3
  • 16
  • While there is another answer related to resolving this specific issue, I'd like to also say that if you are working on different things separately, you might want to look at using branches. Branches can be used to develop separate sets of changes (say, multiple commits on a feature) with the same parent, and then merge them cleanly later. – Gareth Pulham Apr 17 '17 at 00:35
  • Thank you very much @GarethPulham. I will look into this and talk to my partner as well. – acn3 Apr 17 '17 at 00:38

1 Answers1

0

One of you will be able to push without any issue.

The other will have an error message inviting him/her to pull first.

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.

I would recommend:

git pull --rebase
git push

That will replay your local commits on top of the update upstream master branch.

Actually, since git 2.9 or more, you can setup (once):

git config --global pull.rebase true
git config --global rebase.autoStash true

Then you can replace git up by a simple classic git pull.

This workflow is good when you are both working on the same development effort (so same branch) and need to keep your common work up-to-date.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This ended up overwriting State B, which was already pushed, without first getting anything from B. – acn3 Apr 17 '17 at 01:16
  • @acn3 strange, considering pull first fetches: B is is origin/commonBranch. Then you replay your commit on top of origin/commonBranch. Assuming you have both a common branch to which you are pushing to. – VonC Apr 17 '17 at 01:20
  • We do have a common branch. I did git add [files] and git commit before I did the git pull --rebase. Should I not have done this? – acn3 Apr 17 '17 at 01:22
  • @acn3 you can try `git push` first (in case you are first to push). But if not, the `git pull --rebase` is the good practice to follow, after committing locally. If you don't find all your files, check `git status` to see if the isn't a local merge conflict to resolve. – VonC Apr 17 '17 at 01:25