0

I recently made some unnecessary changes to my existing code and accidentally deleted my controller (Using rails). At this point I was on my master branch since I just had to make a small change. Hence I had committed the changes to my repository.

To revert this, I use git reset --hard HEAD to get back to my old code. What I want to do now is remove the 2 commits ahead of this version and want to start working only on the head I just moved to. How do I go about this?

Zooly
  • 4,420
  • 4
  • 29
  • 49
Rohan Mayya
  • 162
  • 1
  • 2
  • 11
  • 1
    Possible duplicate of [Delete commits from a branch in Git](http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – 1615903 May 18 '17 at 06:37

2 Answers2

1

You can remove your commits by push force on HEAD.

git push origin HEAD -f
Zooly
  • 4,420
  • 4
  • 29
  • 49
  • Glad it helps you :) – Zooly May 18 '17 at 08:25
  • 1
    Rather than `-f` (or equivalent `--force`), I recommend using `--force-with-lease` so that you don't accidentally overwrite any commits that someone else may have pushed since you last fetched. – Scott Weldon May 18 '17 at 17:38
0

There are many ways to do this. As per me, the best way is as below:

git rebase -i HEAD~3

It will open a rebase-to-do list file. Remove the commits you want to delete. Then save and close the file.

Pang
  • 8,605
  • 144
  • 77
  • 113
love
  • 2,823
  • 2
  • 14
  • 32
  • This also works fine. I just figured it out. I'd use this for a kind of situation where I'd want to remove a patch in between versions. But push force on HEAD seemed to do what I required just fine. Thank you :) – Rohan Mayya May 18 '17 at 08:26