After git rebase: I found another detached master branch

Viewed 45

Recently i wanted to do some changes to a file in the FIRST COMMIT(c37a5b9) (browse down to see the git log)

So i did:

git rebase -i --root

It opened a vim editor showing all the commits.Then I chose the FIRST COMMIT(c37a5b9) to edit and closed it. then edited some file and then did

git add -A

then did

git rebase --continue

I had some conflicts with the SEVENTH COMMIT. I manually resolved it by editing the conflicts.

Then did

git add -A

then again

git rebase --continue

And after this i found this tree.

* da348ec - Sat, 12 Oct 2019 07:55:40 +0530 (4 hours ago) (HEAD -> master)
|           NINTH COMMIT 
* e507db5 - Thu, 12 Sep 2019 13:46:42 -0400 (4 weeks ago)
|           EIGHT COMMIT
* 541e199 - Mon, 9 Sep 2019 12:46:00 -0400 (5 weeks ago)
|           SEVENTH COMMIT
* dbe3979 - Thu, 11 Jul 2019 06:04:58 -0400 (3 months ago)
|           SIXTH COMMIT
* b98d381 - Mon, 22 Apr 2019 15:42:42 +0530 (6 months ago)
|           FIFTH COMMIT
* b24c5f0 - Fri, 12 Apr 2019 12:29:31 +0530 (6 months ago)
|           FOURTH COMMIT
* 1c9341d - Fri, 12 Apr 2019 00:23:24 +0530 (6 months ago)
|           Third Commit
* cc193e6 - Tue, 9 Apr 2019 00:37:25 +0530 (6 months ago)
|           Second Commit
* 6592e2c - Mon, 8 Apr 2019 23:59:24 +0530 (6 months ago)
            First Commit

* 2229bec - Tue, 24 Sep 2019 07:12:44 -0400 (3 weeks ago) (origin/backup_of_login_with_database, backup_of_login_with_database)
|           NINTH COMMIT (not used procedure)
| *-.   e708887 - Tue, 24 Sep 2019 13:47:56 -0400 (3 weeks ago) (refs/stash)
| |\ \            WIP on master
|/ / /  
| | * 4cd7f19 - Tue, 24 Sep 2019 13:47:56 -0400 (3 weeks ago)
| |             untracked files on master
| * 159e63a - Tue, 24 Sep 2019 13:47:56 -0400 (3 weeks ago)
|/            index on master
* 9ad7f9a - Thu, 12 Sep 2019 13:46:42 -0400 (4 weeks ago) (origin/master, origin/HEAD)
|           EIGHT COMMIT
* 9a50dde - Mon, 9 Sep 2019 12:46:00 -0400 (5 weeks ago)
|           SEVENTH COMMIT
* 5c786a8 - Thu, 11 Jul 2019 06:04:58 -0400 (3 months ago)
|           SIXTH COMMIT
* c7ca76f - Mon, 22 Apr 2019 15:42:42 +0530 (6 months ago)
|           FIFTH COMMIT
* eb1ca3c - Fri, 12 Apr 2019 12:29:31 +0530 (6 months ago)
|           FOURTH COMMIT
* a652e6e - Fri, 12 Apr 2019 00:23:24 +0530 (6 months ago)
|           Third Commit
* 27cc974 - Tue, 9 Apr 2019 00:37:25 +0530 (6 months ago)
|           Second Commit
* c37a5b9 - Mon, 8 Apr 2019 23:59:24 +0530 (6 months ago)
            First Commit

6592e2c and c37a5b9 are two FIRST COMMITS i see. I see two master branches.

I was confused.

The below is what i had before i started the rebase. So how can i change a file of FIRST COMMIT with the below git log.

* 54a7170 - Sat, 12 Oct 2019 07:55:40 +0530 (4 hours ago) (HEAD -> master)
|           NINTH COMMIT 
* aa6dfe6 - Thu, 12 Sep 2019 13:46:42 -0400 (4 weeks ago)
|           EIGHT COMMIT
| * 2229bec - Tue, 24 Sep 2019 07:12:44 -0400 (3 weeks ago) (origin/backup_of_login_with_database, backup_of_login_with_database)
| |           NINTH COMMIT (not used procedure)
| | *-.   e708887 - Tue, 24 Sep 2019 13:47:56 -0400 (3 weeks ago) (refs/stash)
| | |\ \            WIP on master
| |/ / /  
| | | * 4cd7f19 - Tue, 24 Sep 2019 13:47:56 -0400 (3 weeks ago)
| | |             untracked files on master
| | * 159e63a - Tue, 24 Sep 2019 13:47:56 -0400 (3 weeks ago)
| |/            index on master
| * 9ad7f9a - Thu, 12 Sep 2019 13:46:42 -0400 (4 weeks ago) (origin/master, origin/HEAD)
|/            EIGHT COMMIT
* 9a50dde - Mon, 9 Sep 2019 12:46:00 -0400 (5 weeks ago)
|           SEVENTH COMMIT
* 5c786a8 - Thu, 11 Jul 2019 06:04:58 -0400 (3 months ago)
|           SIXTH COMMIT
* c7ca76f - Mon, 22 Apr 2019 15:42:42 +0530 (6 months ago)
|           FIFTH COMMIT
* eb1ca3c - Fri, 12 Apr 2019 12:29:31 +0530 (6 months ago)
|           FOURTH COMMIT
* a652e6e - Fri, 12 Apr 2019 00:23:24 +0530 (6 months ago)
|           Third Commit
* 27cc974 - Tue, 9 Apr 2019 00:37:25 +0530 (6 months ago)
|           Second Commit
* c37a5b9 - Mon, 8 Apr 2019 23:59:24 +0530 (6 months ago)
            First Commit

I will later do push -f

2 Answers

When using git rebase -i --root, choose the first commit would rewrite the whole commit history of local master. So, after the rebase, you got the local master branch started with 6592e2c and remote master with the original c37a5b9.

The following are all keeping your old version of master alive.

  • The remote-tracking branch origin/master
  • The local branch backup_of_login_with_database
  • The remote-tracking branch origin/backup_of_login_with_database
  • Work in progress stashed against the old master

When you git push --force origin master, origin/master will reflect the rewritten history.

You can throw away the stashed commit with git stash drop or resume the WIP with git stash apply.

Transplant backup_of_login_with_database to the new history with

git rebase --onto master~ backup_of_login_with_database~ backup_of_login_with_database

or, using SHA1 object names that are specific to the history you showed

git rebase --onto e507db5 9ad7f9a backup_of_login_with_database

Also force-push that branch with

git push --force origin backup_of_login_with_database

All that said, consider whether all the above trouble is really worth it versus merely making the change on top of the master and backup_of_login_with_database branches.

Related