1

So we finally have git set up but I'm really confused how to handle multiple people merging.

I have a branch, test, that I merged a feature in to earlier today. The other developer has now merged his new feature into test, and in the process - since his new feature was branched off before I merged mine in - it deleted the files that mine added.

1) How do we get around this?

2) How can I force git to not remove or overwrite files without asking?

Edit: Corollary: When should we pull, and I guess when we pull (I'm guessing before a merge?) we should merge the pulled copy into the branched feature?

Andrew
  • 5,095
  • 6
  • 42
  • 48

2 Answers2

2

There's something very very wrong there. Files do not just get deleted from a branch, Git will always tell you if there are conflicts and that happens by default. If they were committed correctly they will still be recoverable.

You need to make sure the other developer knows what he's doing when he's performing a merge. If things are exactly like you describe, it sounds like he put considerable effort into merging his feature at the cost of not having to deal with a merge correctly, meaning he chose to remove your earlier changes.

When you pull Git merges automatically and warns of any merge conflicts. If you want to avoid merging automatically, perform a git fetch and then rebase your branch.

sciritai
  • 3,688
  • 1
  • 17
  • 22
  • Neither of us really know what we're doing. :) The way I understand it... 1) We have the test branch. 2) I made a branch off it. 3) He made a branch off it. 4) I merged mine in. 5) He pulled. 6) He merged his in; this deleted the new files that were pulled. Was some of this done out of order? should he have pulled into his new branch as well? – Andrew Nov 03 '11 at 22:09
  • That workflow sounds fine. However the result of that workflow being that the files were deleted is impossible unless someone chose to do it. Sounds like you need to do more reading about Git :) – sciritai Nov 03 '11 at 22:13
  • Agreed. The workflow seems fine. It sounds like your coworker manually screwed up his merge. – Phil Miller Nov 03 '11 at 23:45
1

Consider the following timeline (sequence of events):

  1. Oct 1 - test is created and pushed to the shared repository. We'll refer to it as origin/test
  2. Oct 2 - You pull the test branch to your local repository
  3. Oct 2 - Developer-2 pulls the test branch to *his* local repository
  4. Oct 4 - You merge (or simply push) your changes in the test branch to origin/test. This should be smooth sailing. I am keeping it simple here assuming that no one else has made any changes to test in the interim
  5. Oct 6 - Developer-2 attempts to push his code changes to origin/test

Right at step-5, Git will complain. Why? Because the state of origin/test has changed since developer-2 pulled from it. Developer-2 must

  1. pull from the shared repository. In so doing he will probably encounter merge conflicts if you and developer-2 were working in the same area of the code base
  2. Developer-2 must resolve any conflicts locally
  3. push his changes to shared repository

The only way developer-2 could clobber your changes in the shared repository is if he used the --force flag when pushing his changes without first pulling.

Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
  • Well maybe I'm misreading the messages then; when he merged it said, among other things, "Removing [file]". Is this simply something that should be resolved in the merging process? – Andrew Nov 04 '11 at 13:34
  • This is impossible to diagnose without having the full context. Like I said, Developer-2 **must** do a `pull` before doing a `push`. Part of the `pull` operation is to do a `merge`. What is merging? The remote copy of the branch named `test` and his _local_ copy of the branch named `test`. – Sri Sankaran Nov 04 '11 at 15:33
  • It seems like this was a hiccup caused by us shoehorning in our existing development environment into our new git system. Some skillful merging and everything now seems hunky dory. Sorry for confusing people and causing y'all to try to figure out the solution to our problem! :( – Andrew Nov 04 '11 at 19:52