If you have not yet pushed anything, you can modify your history in multiple ways. If you have pushed your state, you need to consider the impact it will have!
You'll be causing trouble for any collaborators that have created commits on top of branch1
but not yet pushed them. If nobody other than you has ever used branch1
, you should be fine.
I find the most intuitive way to use
git checkout branch1
git rebase -i master
This opens a text file like this. Change the commit you want to get rid of to drop
as I did below. Then save the file and exit.
drop 1a432d7 revertB
pick 2657446 c
pick 8d15847 d
# Rebase 1219262..8d15847 onto 1219262 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Since I modified always the same line in my testing repository for this answer, I got a merge conflict. In commit b, I wrote b
. In commit c, I wrote c
. In commit revertB I wrote revertB
.
<<<<<<< HEAD
b
=======
c
>>>>>>> 2657446... c
You'll have to fix your merge conflicts if you encounter any. In my case, I replaced the file contents with c
.
Continue the rebase after staging the fixed file:
git add myfile
git rebase --continue
You're given the option to modify the commit message of commit c. Once you confirmed it, revertB
is gone from branch1
.