primee
primee

Reputation: 90

git ended up completely diverged branch

After trying to merge my branches, I realized they ended up into unrelated history.

So I'm wondering how is that possible in git!, I didn't used --orphan to create the branch and it's only used as a local repository(no remotes used), although I used some Hooks to dump the database into working directory by git status that made hooks to run git add db.sql; git commit --amend --no-edit many times.

Edit: diff result between first commits of each branch (e51b4a2 and 6cf7d37) lies in db.sql!.

commit's log:

* commit 75c0c57 (HEAD -> database)
| Date:   3 days ago
|
|     somechanges
|
* commit b4e667d
| Date:   3 days ago
|
|     initial database
|
* commit 6cf7d37
  Date:   3 days ago

      initial

* commit 6507785 (master)
| Date:   3 days ago
|
|     update module
|
* commit e51b4a2
  Date:   3 days ago

      initial

git reflog:

75c0c57 HEAD@{0}: checkout: moving from master to database
6507785 HEAD@{1}: checkout: moving from database to master
75c0c57 HEAD@{2}: commit: some changes
b4e667d HEAD@{3}: commit (amend): initial database
202717a HEAD@{4}: commit (amend): initial database
5db604f HEAD@{5}: commit (amend): initial database
6d2dcbd HEAD@{6}: checkout: moving from master to database
6507785 HEAD@{7}: commit: update module
e51b4a2 HEAD@{8}: checkout: moving from database to master
6d2dcbd HEAD@{9}: checkout: moving from database to database
6d2dcbd HEAD@{10}: checkout: moving from database to database
6d2dcbd HEAD@{11}: checkout: moving from 6d2dcbd47ce6f57a5a5a767f97b301db78ba11c2 to database
6d2dcbd HEAD@{12}: checkout: moving from database to head
6d2dcbd HEAD@{13}: commit (amend): initial database
7ea51da HEAD@{14}: commit (amend): initial database
0bce495 HEAD@{15}: commit (amend): initial database
8401193 HEAD@{16}: commit: initial database
6cf7d37 HEAD@{17}: commit (amend): initial
e51b4a2 HEAD@{18}: checkout: moving from master to database
e51b4a2 HEAD@{19}: commit (amend): initial
3f30042 HEAD@{20}: commit (amend): initial
8c6ef22 HEAD@{21}: checkout: moving from database to master
c2f1f28 HEAD@{22}: commit (amend): initial database
c9a3aa6 HEAD@{23}: commit (amend): initial database
13997d1 HEAD@{24}: commit: initial database
8c6ef22 HEAD@{25}: checkout: moving from master to database
8c6ef22 HEAD@{26}: commit (amend): initial
181978f HEAD@{27}: commit (initial): initial

Upvotes: 0

Views: 61

Answers (2)

Useless
Useless

Reputation: 67822

Here's your exact problem:

3f30042 HEAD@{20}: commit (amend): initial
8c6ef22 HEAD@{21}: checkout: moving from database to master

At this point, you've created a copy of your initial commit on master, which is no longer related to the initial commit on database.

We can even show the exact state before and after this, thanks to your reflog.

Up to HEAD@{22} (before switching back to master):

A  181978f "initial" <HEAD@{27}
A' 8c6ef22 "initial" <HEAD@{26} <master
 \
  B  13997d1 "initial database" <HEAD@{24}
  B' c9a3aa6 "initial database" <HEAD@{23}
  B" c2f1f28 "initial database" <HEAD@{22} <database

Note all the orphaned duplicates causes by your amends - they're not a problem, but they do exist.

After switching back to master, and doing the final amend, you get:

A  181978f "initial" <HEAD@{27}
A' 8c6ef22 "initial" <HEAD@{26}
 \
  B  13997d1 "initial database" <HEAD@{24}
  B' c9a3aa6 "initial database" <HEAD@{23}
  B" c2f1f28 "initial database" <HEAD@{22} <database
A" 3f30042 "initial" <HEAD@{20} <master

See that A" is unrelated to A' just as that was unrelated to A. For any but the first commit, they'd be siblings sharing a common ancestor. Compare B, B' and B", which are all siblings with the same parent (A').


So I'm wondering how is that possible in git!

Because you explicitly told git to do that. Commit A' was shared between master and database. Amending a commit shared between branches is just as problematic as amending one shared between local and remote repos - the point is you're changing something that was already shared.

If you're not sure about this stuff, don't use --amend without a specific reason. It's not a good default, which is why it isn't the default.

Your simplest fix is probably something like:

git rebase --onto 3f30042 8c6ef22 database

(assuming you want A" to be the parent of database).

Upvotes: 2

hspandher
hspandher

Reputation: 16763

If you have already pushed the branch to remote or have merged it into other branch, then git commit --amend is always going to cause a problem.

You see amend actually rewrites the history. amend command actually doesn't changes the same commit, instead a new commit is created with a different hash id. Technically keeping the same commit id is not feasible. Commit Id are created by hashing the commit message and root tree, which is always going to be different for an amend.

As a rule of thumb, never amend or rewrite the history that you have already shared with others.

Upvotes: 0

Related Questions