What I have:
---A----B-----C-----D--------*-----E-------> (master)
\ /
1----2 (foo)
What I need:
---A---------------D--------*-----E-------> (master)
\ /
1----2 (foo)
A while ago I made two commits I would like to remove from my git repo. I tried a variety of different rebasing "tutorials" and all of them ended in weird git histories, so I created an example repo, and the result is not what I expected. Can anyone help me to understand what I am missing?
I have two branches, master
and foo
. I made a commit B with a single file I would like to remove, and commit C where I modified this file. Along the other commits, I never touched this file ever again.
Commit IDs:
A: f0e0796
B: 5ccb371
C: a46df1c
D: 8eb025b
E: b763a46
1: f5b0116
2: 175e01f
So I use rebase -i f0e0796
and remove B 5ccb371
and and C a46df1c
, correct? If I interpret the result correctly, this is what gitk
shows me for my repo, although git branches
still lists the second branch.
---A-----1----2---E------> (master)
Can anyone tell me what happened here?
Edit: This is how to recreate the repo from the first graph:
git init foo
cd foo
touch A
git add A
git commit -m "add A"
touch B
git add B
git commit -m "add B"
echo "modify" > B
git add B
git commit -m "modify B"
touch C
git add C
git commit -m "add C"
git checkout -b foo
touch 1
git add 1
git commit -m "add 1"
touch 2
git add 2
git commit -m "add 2"
git switch master
git merge foo --no-ff
touch E
git add E
git commit -m "add E"
While what I am proposing will give you a clean, linear history; that's what rebase is supposed to do essentially. However, am hoping this gives you a way to remove B and B' from the commit history. Here goes the explanation:
Repo recreation output:
---A----B-----B'-----C--------D-------> (master)
\ /
1----2 (foo)
git log --graph --all --oneline --decorate #initial view the git commit graph
* dfa0f63 (HEAD -> master) add E
* 843612e Merge branch 'foo'
|\
| * 3fd261f (foo) add 2
| * ed338bb add 1
|/
* bf79650 add C
* ff94039 modify B
* 583110a add B
* cd8f6cd add A
git rebase -i HEAD~5 #here you drop 583110a/add B and ff94039/modify B from
foo branch.
git log --graph --all --oneline --decorate
$ git rebase -i HEAD~5
* 701d9e7 (HEAD -> master) add E
* 5a4be4f add 2
* 75b43d5 add 1
* 151742d add C
| * 3fd261f (foo) add 2
| * ed338bb add 1
| * bf79650 add C
| * ff94039 modify B
| * 583110a add B
|/
* cd8f6cd add A
$ git rebase -i master foo #drop 583110a/add B and ff94039/modify B again
$ git log --graph --all --oneline --decorate #view the git commit graph
* 701d9e7 (HEAD -> foo, master) add E
* 5a4be4f add 2
* 75b43d5 add 1
* 151742d add C
* cd8f6cd add A
Lastly, the final out might not be in the order you'd expected A--C--1---2---E. However, you can re-arrange the order within the interactive mode again. Try git rebase -i HEAD~n.
Note: It's best to avoid changing commit/publishing history. I am a newbie and exploring git, hopefully the above solution should stick. That said am sure there are tonnes of other easier solutions available online. I found this article quite helpful, for future reference for all.