0

I am trying to do this the easy way and do less coding. I have the following commits in git

A->B->C->D

I want to basically remove commit C and keep commit D - and I am struggling to find a way how

I tried

git checkout -b myfix B
git cherry-pick D
git checkout master
git merge myfix

But of course commit C is still there since I merged.

Is there a way to say replace master with myfix branch?

Or even another approach to get

A->B->D->[new commit]
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • 1
    `git revert` is probably what you're looking for. See [this excellent explanation](https://www.atlassian.com/git/tutorials/undoing-changes/git-revert/). – sjagr Jan 20 '15 at 18:20
  • Revert worked perfectly - can you post that as an answer - @Sanova did mention it but rebase was not what I was looking for – Adrian Cornish Jan 20 '15 at 18:57

2 Answers2

3

Interactive Rebase

$ git rebase -i HEAD~5

where -i is the interactive flag, and HEAD~5 means include the last 5 commits in the interactive rebase.

A prompt will show then, delete the lines that reference the commits you want to be rid of, save and close.

also

if you just want to go back on local comits, just use git revert

Sanova
  • 551
  • 3
  • 10
  • Revert is what I was looking for - thanks - but @sjagr mentioned it first so even though you did to be fair I'd accept his answer if he posts one. The rebase would have actually required me to manually merge all the code which I was trying to avoid (because I am reverting another dev's code change and I really don't want to spend time on whose change is what) – Adrian Cornish Jan 20 '15 at 18:59
1

git revert is a very simple tool that will achieve exactly what you want. This writeup is a nice explanation on how it works and how to use it, however it's just a matter of using:

git revert <commit>

Where <commit> is the commit ID, or simply in your case, HEAD~1. Resulting command would be:

git revert HEAD~1

Then just git push your changes and you're set.

sjagr
  • 15,983
  • 5
  • 40
  • 67