You don't have to revert. You can use git rebase
in interactive mode to edit your history. For example, on my current topic branch, if I run git rebase -i master
I get this view in VIM:
pick 23299aa6 improve unit test messages
pick cf1c3c0c move sagas tests to where they'll get run
pick f7ff7fa7 use docker-compose up to rebuild the ui image more quickly
pick 544902ec split definitions file into helpers and given, when, then files
pick 55e94e7b remove canary test
pick 8a4b0862 move expectTitleToBe function to helper file
# Rebase 72b74f26..8a4b0862 onto 72b74f26 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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
From this view I can remove lines I don't want to commit, if I didn't actually want to remove the canary test I could delete or comment out that line or use the drop command. I can stop the rebase at a commit and change what will be committed with edit (you could commit partially here by removing changes) and so on for the other commands listed:
edit 23299aa6 improve unit test messages
pick cf1c3c0c move sagas tests to where they'll get run
pick f7ff7fa7 use docker-compose up to rebuild the ui image more quickly
pick 544902ec split definitions file into helpers and given, when, then files
drop 55e94e7b remove canary test
pick 8a4b0862 move expectTitleToBe function to helper file
# Rebase 72b74f26..8a4b0862 onto 72b74f26 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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
For your situation and rebasing against yourself and not another branch, you can look back in the git log
to find the commit hash of the commit you want to revert to. This is usually going to be one commit before the work that you started: git rebase -i <where to "revert">
.