Michael Gruber
Michael Gruber

Reputation: 601

Undoing a bunch of Git mess

I want to revert back to a specific commit ID. Restore a file I "git rm"-ed. Blow away all commits made since then. And then commit that repo. How would I do this?

Upvotes: 0

Views: 114

Answers (3)

mipadi
mipadi

Reputation: 410602

$ git reset --hard <commit ID>    # Will reset to a specific commit
$ git push -f <remote>            # Push branch into remote, with force

Note that anyone who has cloned your repo may experience minor problems if they simply pull in your changes (since you forced the branch backwards in the commit history); they should do this to get your changes:

$ git fetch <remote>
$ git reset --hard <remote>/<branch>

Note that this will also blow away any of their changes in the current branch. However, if they have any branches made from their current branch, those branches will still have the commits you "blew away".

Upvotes: 2

tonino.j
tonino.j

Reputation: 3871

You can use git revert my_commit_id command.

If it is one or two previous commits, you do it with git revert HEAD and git revert HEAD^ respectively.

Instead of HEAD use your commit id if this doesnt solve your case.

This will also give you an option to edit commit messg again and create new commit. `

Upvotes: 0

Perhaps you are looking for git reset --soft {ID}.

   git reset --<mode> [<commit>]
       This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>, which must be one of the
       following:

       --soft
           Does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git
           status would put it.

Not sure what 'blow" really means, that why --hard mode could be useful too.

Upvotes: 0

Related Questions