I encountered an interesting problem after rebasing a branch on another branch; git rebase -i HEAD~n
shows not only the wrong commit history (the old branch) but also the incorrect amount of commits.
What I want to do
I want to be able to do a git rebase -i HEAD~n
on the correct commit history in order to squash a leftover commit from the old branch I based my branch on.
What I did to cause the issue
# On another Feature branch
git branch -b NewFeautureBranch
# Develop my commit
git add editedfile.extension
git commit -m "Commit message"
# I squashed and merged the feature branch into the development branch, as well as some other feature branches
git fetch --all
git checkout DevelopmentBranch
git pull
git checkout NewFeatureBranch
git rebase DevelopmentBranch
git push -f
Running git log
and gitk
will show the history as it should be, which is my current commit and an older commit from the feature branch it was originally based on, as expected. However, if I then run git rebase -i HEAD~4
I get the old branch's history as well as 11 commits instead of the 4 I asked for:
# Git rebase -i HEAD~4 opens up in VIM like this:
pick f79316dc Commit on old Branch base
pick ba742f2f Commit on old Branch base
pick 7577ea7a Commit on old Branch base
pick 91c4088c Commit on old Branch base
pick 98feed6d Commit on old Branch base
pick e8a73d78 Commit on old Branch base
pick 198f79e7 Commit on old Branch base
pick 10bb699c Commit on old Branch base
pick 1d15a926 Commit on old Branch base
pick 0cf569bb Previous commit I want to squash with
pick 470de8d9 Current FeatureBranch commit
But git log
prints this:
# As it should be when seeing git log and gitk
commit 470de8d92bb490dd14c31d5741b7edec82ca7597 (HEAD -> FeatureBranch, origin/FeatureBranch)
Author: evFox <[email protected]>
Date: Wed Jan 3 16:08:24 2018 +0100
Feature commit message
commit 0cf569bba43b5747831a28d6cb42209dab1c2ffa
Author: evFox <[email protected]>
Date: Wed Jan 3 12:09:48 2018 +0100
Feature from old branch still relevant to this; what I want to merge with the later commit.
commit 982c30d9c3b46539340fe48c241192e377d3e136 (origin/Development, Development)
Merge: ab6e7c9d 1d15a926
Author: evFox <[email protected]>
Date: Tue Jan 9 10:51:06 2018 +0000
Merged PR XX: Merge OldFeatureBranch to Development
What I've tried
I've tried to delete the branch locally and pull a fresh one off of the remote repository:
git checkout DevelopmentBranch
git branch -D NewFeatureBranch
git checkout -b NewFeatureBranch origin/NewFeatureBranch
I have tried to get more history by increasing the commits in the git rebase -i
and I can clearly see that the commits are old, belonging to the OldFeatureBranch as it was before it was updated and merged:
# Git rebase -i HEAD~20 opens up in VIM like this:
pick bt67f432 Commit on old version DevelopmentBranch
pick 5g67f33s Commit on old version DevelopmentBranch
pick rt53d563 Commit on old version DevelopmentBranch
pick ew5r45fg Commit on old version DevelopmentBranch
pick 9gy3f74f Commit on old version DevelopmentBranch
pick 58u5hh63 Commit on old version DevelopmentBranch
pick 34fdg5d5 Commit on old version DevelopmentBranch
pick n678hcn7 Commit on old version DevelopmentBranch
pick mh7y88dr PR merge of DevelopmentBranch where old Branch base were branched out originally, but not where DevelopmentBranch was when old Branch Base was squashed and merged.
pick f79316dc Commit on old Branch base
pick ba742f2f Commit on old Branch base
pick 7577ea7a Commit on old Branch base
pick 91c4088c Commit on old Branch base
pick 98feed6d Commit on old Branch base
pick e8a73d78 Commit on old Branch base
pick 198f79e7 Commit on old Branch base
pick 10bb699c Commit on old Branch base
pick 1d15a926 Commit on old Branch base
pick 0cf569bb Previous commit I want to squash with
pick 470de8d9 Current FeatureBranch commit
But the issue remains.
Possible work-around
I suppose I could try to do a soft reset and amend to the commit I want to squash into:
git reset --soft HEAD~1
But that doesn't fix my current issue of having the git rebase -i HEAD~n
history messed up.
See Question&Answers more detail:
os