Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout commit and delete newer commits

I have made bad commits in the github and i want to do the following things:

  1. checkout to an old commit from a different branch than master
  2. make this commit my last commit(delete those after this)
  3. make this branch as master branch and delete all the others branches
  4. merge master branch to a new one

Is it possible to do this?

like image 796
user2065529 Avatar asked Feb 27 '13 19:02

user2065529


1 Answers

The key command is:

git checkout -B master aSHA1

If you know where you want to reset your master branch, that is the way to do it in one line.

From git checkout man page:

-B <new_branch> 

Creates the branch <new_branch> and start it at <start_point>;
if it already exists, then reset it to <start_point>. This is equivalent to running "git branch" with "-f";

Then you can force push it:

git push --force

That will reset origin/master (the master branch on the GitHub side) to your old SHA1.

like image 173
VonC Avatar answered Sep 29 '22 18:09

VonC