Reputation: 163
Dears,
I want to delete a very old commit on git repo, but after execute the rebase process and making a checkout to the last branch of my repository the proccess is being undone. So, this is what I'm doing:
git log
abcdef - Last repo commit (master)
...
... # many many commits
...
222333 - A
111222 - B <-- I want to delete this
000111 - C
Change the HEAD, just for readability:
git checkout 222333
git rebase -i 000111
At this point, I edit the todo shown by rebase, changing the text from pick to edit.
pick 222333 - A
pick 111222 - B (change to edit)
pick 000111 - C
Now I'm making modifications on file content inside the commit. After this:
git add .
git commit --amend
git rebase --continue
After all, when inspecting using git log
or gitk
everything is ok. But when I return to master git checkout master
, all the proccess is undone, when inspecting again! Does anyone know why?
Environment:
Upvotes: 1
Views: 47
Reputation: 97718
The problem is this line, which you say you're doing "just for readability":
git checkout 222333
This puts you in "detached head" state: your working copy is on that revision, and not on any branch.
While in that state, you rebase some commits, which creates a new history, made up of new commits. Normally, this would also move the current branch pointer to point at those new commits, but you don't have a current branch, so no pointers are moved.
Finally, you run git checkout master
which switches to the branch called "master". A branch is just a pointer, and this one doesn't point at the new commits which git rebase
generated, it points wherever it pointed before. The newly created commits are left "orphaned" with no branches pointing at them.
There's another problem, too: commits in git are immutable, so any edit to history has to create new commits from that point forward. The "many many commits" you show in your log summary all need to be rewritten, so they all need to be listed in the interactive rebase todo list.
(If you've watched enough sci-fi films, "changing history" in git is like the form of time travel where you don't change your own past, you create a parallel universe with a different past, and a correspondingly different future.)
So forget about making it "readable", and just run this:
git checkout master
git rebase -i 000111
All the many many lines labelled "pick" are the ones that are going to be re-created; just scroll past them and delete the one which you want to not re-create.
Upvotes: 3