Remove a commit and related changes from a branch

git log 

will list all the commits on the current branch, suppose I got three commits, and I want to remove the one in the middle, you may suggest the following command:

git reset --hard <sha1-commit-id>

However, I don’t want to revert to that commit id, I want just to remove it from the branch so next time when I do git log it will not figure in the list. Thanx in advance.

  • Git syncs master changes to all other branches
  • WARNING: this script is deprecated, please see git-completion.zsh
  • See “real” commit date in github (hour/day)
  • Showing git branch in shell prompt?
  • Combined command for git tag verification and git checkout?
  • Composer - SensioDistributionBundle asking for a password
  • EDIT, sorry for my miss explanation, when I said removing the commit id, I meant removing all related changes as well.

  • How to undo “add to Git repository” after creating an Xcode project?
  • Is git ls-files really character agnostic? If not, what is?
  • How to switch submodule branch based on superproject branch
  • Git refs/remotes/origin/master does not point to a valid object
  • How to clone a specific Git branch?
  • showing git branch name in command prompt
  • 3 Solutions collect form web for “Remove a commit and related changes from a branch”

    To effectively undo the changes in that commit and remove it from history, you could do something like this:

    git rebase --onto HEAD^^ HEAD $BRANCH
    

    Where $BRANCH is the name of the branch you are currently on.

    Alternatively, you could check out the older commit and then cherry-pick the newer one:

    TIP=`git rev-parse HEAD`
    git reset --hard HEAD^^
    git cherry-pick $TIP
    

    Keep in mind that both operations change history, and this will require a git push --force operation if you have already published these commits.

    Is the commit also on a remote branch? Depending on the answer to this will determine what you should do.

    If it isn’t, do git rebase -i <sha before the one you want to remove> This will allow you to delete the commit.

    If it is on a remote, you don’t want to remove it. You will end up causing problems with pushing and pulling. Rewriting history after you have pushed is a bad thing. So for that case you want to git revert <sha that you want to remove>. This creates a new commit that undoes the changes.

    Use command

    git rebase -i HEAD~N

    Suppose you dont want 2nd commit, N will be 2.. once you run this command with value of N=2 git will prompt you to take action.. just remove the line for the commit you dont want , it will get deleted…

    And to delete tag use

    git tag -d [ tag name ]

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