How to nuke a commit before last commit

Suppose I have 2 branches master and dev.

The dev branch is ahead of master by 2 commits, and neither of those commits is merged or pushed to any remote.

  • how do i know if a submodule has changed in git?
  • On git pull, “unable to resolve reference” and “Undefined error: 0”
  • Git error Permission denied (publickey), couldn't paste publickey
  • How to manually trigger DLTK indexing in Eclipse PDT for a PHP project
  • Overwriting folders with git
  • Visual Studio 2013 and Github account authentication not working
  • The dev branch looks like this:

    dev A - B - C - D - E

    Whereas, A is oldest, and E newest commit.

    Id like to get to this state:

    dev A - B - C - E

    Basically I want to nuke the commit before the last commit, as if it never happened. There should be no conflicts either (E does not depend on D’s changes).

    Could I use interactive rebase?

  • Check that git repository exists
  • Ran git rebase origin/develop and now I have a (develop|REBASE 1/2) - branch
  • How can I review or secure remote branch deletions?
  • git-commit gives no output and nothing is committed
  • Is there a way to use a Mercurial repository as Git submodule?
  • In a git hook is the current working directory guaranteed to be within the git repository?
  • One Solution collect form web for “How to nuke a commit before last commit”

    I would use the itneractive form of rebase. While on dev (you need to have clean working copy):

    git rebase -i HEAD~~

    In your favourite text editor, you get a list of last 2 commits. Just comment/delete the line for the unwanted commit.

    If HEAD depends on changes introduced by HEAD~, you could get merge conflicts and after you resolve them, you will loose your previous history (still available through the reflog, of course). I usually prefer this method.

    If you want to conserve your previous history (and the unneeded commit), you should do

    git checkout HEAD~~         # go to last good commit
    git checkout -b new_branch  # create a new branch and forget about the original
    git cherry_pick dev         # copy the commit on top of `dev` to `new_branch`

    You will still get a conflict, but you won’t be modifying the history of dev.

    On the other hand, if the unwanted commit was on master and you didn’t want to break anybody’s build, you would do git revert HEAD~

    Git Baby is a git and github fan, let's start git clone.