Cisplatin
Cisplatin

Reputation: 2998

Is it possible to git reset as a new commit?

I'd like to reset a branch to a certain commit, but still keep its history. For example, given this branch:

A -> B -> C

I'd like to create a new commit A* such that the branch at A and the branch at A* are exactly the same, and the history looks like:

A -> B -> C -> A*

Is this possible in git?

Upvotes: 1

Views: 41

Answers (2)

Chris Martin
Chris Martin

Reputation: 30756

I can think of two approaches using git revert.

1 - Revert, then squash

Revert the last two commits. This creates two new commits. I'm using the --no-edit flag here so it won't prompt you for a commit message for each revert.

git revert --no-edit HEAD^^..HEAD

Then, since you wanted one commit instead of two, do an interactive rebase to squash the two revert commits you just created.

git rebase -i HEAD^^

2 - Revert with --no-commit, then commit

With the --no-commit flag, git revert does not commit anything, but just makes changes to the working tree and index.

git revert --no-commit HEAD^^..HEAD

Then you can commit the change.

git commit

Upvotes: 4

choroba
choroba

Reputation: 242323

Use git revert for that. Read a nice tutorial by Atlassian.

Upvotes: 3

Related Questions