I tend to use Tower as my git client here you can see me directly reverting an older commit.
Without rewriting history:
First revert the thing you want to get rid of locally:
git revert sha-of-2 -m X
Where X is the parent number (likely 1 in this case). See here for details.
Resolve any merge issues that come from that. Resolving these potential conflicts will make sure that 1
is applied correctly without having to revert it first. then commit and push.
The result will be:
-- 4 -- 2 -- 1 -- '2 <- master
/
-- 3 --/
where '2
is the inverse of 2
.
With rewriting history:
WARNING: force pushing to master after rewriting history may have severe negative impact on other users of the same branch.
Do an interactive rebase:
git rebase -i sha-of-2
drop the merge commit, keep commit 1, then force push master.
The result will be:
-- 4 -- '1 <- master
-- 3
Where '1
is a new commit with the original changes of 1
plus any conflicts you had resolved.
Rewriting history using a new branch
WARNING: force pushing to master after rewriting history may have severe negative impact on other users of the same branch.
This may be the easier to understand. It does the exact same thing as the rebase example able, but instead of applying rebase-fairy-dust, you do the hard work yourself.
Instead of rebasing you could re-do these changes yourself. Reset to 4
, create a new branch, cherry pick 1
, reset master to '1
and force push.
/- '1 <- master
/
-- 4 -- 2 -- 1
/
-- 3 --/
git reset --hard sha-of-4
git checkout -B newmaster
git cherry-pick sha-of-1
# fix conflicts if any
git checkout master
git reset master --hard newmaster
git push --force
Where '1
is the cherry-picked version of 1
.