2

Here is my situation, there is a GIT repository, I am working on master while someone else is working on a branch, say subway, with the following history (say for one single file):

edit:sorry the formatting is not user-friendly, I've updated it as below:

Person A and B are both working on the same file, Person A is working on master with commits: Time1A, Time2A and Time3A changes: added 1, 3, 5 respectively

Person B is working on branch subway with commits: Time1B, Time2B and Time3B changes: added 2, 4, 6 respectively

When PersonB merges from subway to master, the commits history will look like:

Time1A, Time1B, Time2A, Time2B, Time3A, Time3B

and in the file I got: 1,2,3,4,5,6.

If I need to get a clean master before the merge, can I just checkout the commit for Time3A? Does it include stuff merged from subway before Time3?

Thanks

hzxu
  • 5,753
  • 11
  • 60
  • 95

2 Answers2

3

Making a merge in Git only adds to history. It does not modify the past — if you check out a past commit after a merge, that commit will be exactly as it was when that commit was made.

Incidentally, your timeline is not really correct: the two branches are not retroactively commingled like that. A better depiction of the commit history would be:

master -> ... Time1A -- Time2A -- Time3A
                                        \
                                         >-- Merge
                                        /
subway -> ... Time1B -- Time2B -- Time3B

In this history, the commit marked "Merge" has two parents (Time3A and Time3B).

  • But if Person B merges to master, then in the master's commit history, do I get all commits from Person A, followed by commits from Person B, or mixture of their commits? – hzxu Apr 18 '13 at 00:42
  • You'd get all the parent commits, sorted by the time they were made. So, probably a mixture. –  Apr 18 '13 at 00:52
  • Yes, so I need to checkout a commit in master with only Person A's commits, that's what my question is asking, sorry for the misleading. – hzxu Apr 18 '13 at 01:02
  • Checking out any one of those intermediate commits will still give you the contents that were originally part of that commit. Like I said, merging does not alter the past. –  Apr 18 '13 at 01:19
  • Yes, but the actual time line looks like A1, B1, A2, B2, B* are just in another branch before the merge. – hzxu Apr 18 '13 at 01:29
  • It's still possible to tease them apart. `git log` will display them all together, but other tools will display something resembling the structure shown in my answer. –  Apr 18 '13 at 01:32
2

One better solution would be for B to rebase his/her work on A

(master) -> ... Time1A -- Time2A -- Time3A
                                        \
                                         >--  Time1B' -- Time2B' -- Time3B' (subway)

Then the merge to master is a fast-forward one:

(master) -> ... Time1A -- Time2A -- Time3A --  Time1B' -- Time2B' -- Time3B' (master,subway)

See "git rebase vs git merge".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250