34

I have a master branch like this..

A -- B -- C -- D -- E -- HEAD

Is there any command that remove one of a old commit and retain the others, say commit C?

finally it becomes like this

A -- B -- D -- E -- HEAD

I know that we can use a reverse patch and apply a new commit with reverse patch to remove commit C, but the tree structure will not be so clear and looks bulky, i.e.

A -- B -- C -- D -- E -- C(apply reverse patch) -- HEAD

Anyone knows?

0

2 Answers 2

45

Use interactive rebase. For example, to go back 5 commits:

git rebase -i HEAD~5

Then in the editor which pops up, delete the line containing the commit you want to remove.

Sign up to request clarification or add additional context in comments.

6 Comments

With customary warning of don't do this if you have pushed this elsewhere.
With customary exception that you're certain you're the only one using the remote you pushed it to.
With customary addendum that it's also possible if you notify all the other devs and they are able to fix it on their end
@Graham: I found it works. But in the editor, there are different command, how can i apply those command? i.e., squash, pick etc apart from just deleting that commit line?
@Kit: just read the help text which appears at the bottom of your editor window. Replace the word "pick" with "edit" or "squash" as needed for each commit, or delete the lines you don't want.
|
8

Interactive rebase works, but to do it with just one command:

git rebase --onto B C

Still see the comments on the "interactive rebase" answer. They apply here, too.

3 Comments

when executing this command and ending up a with a conflict what does it mean? can you provide some practical examples please?
Where B and C are commit hashes and not branch names?
@logicbloke Either one. A branch name is just a friendly alias for a commit. For most any general git command, giving a branch name or a commit hash are interchangeable ways of identifying a commit. See git help revisions for many other ways of identifying a specific commit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.