Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MerryChristmas's avatar

How to make old commit a new commit using GIT?

I have commits A->B->C->D.

I want B to be a newest version.

So I would end up with this A->B->C->D->B

Is it possible? To make older version of code newest version. But not losing any commits? Also code is already in Gihub and on server.

0 likes
6 replies
tykus's avatar

B is already in the commit history - what is the thought behind this???

tisuchi's avatar
tisuchi
Best Answer
Level 70

@merrychristmas To make commit B the newest version, you can use the following commands:

  • Checkout the commit that you want to be the new head (in this case, commit B)
git checkout <commit-hash-of-B>
  • Create a new branch from this commit
git branch new-branch
  • Checkout to your master branch
git checkout master
  • Revert all commits that come after B.
git revert <commit-hash-of-C>..HEAD

This will create new commits that undo the changes made in commits C, D, and any other commits that come after B. This will effectively make B the newest version, while preserving the entire commit history.

1 like
MerryChristmas's avatar

@tisuchi

git revert c1e0757bec32dbca1b103c58087d1af39334c757..HEAD
error: commit 2004ef02689d9145abdabdd1aa2f750b051f79b8 is a merge but no -m option was given.
fatal: revert failed
tisuchi's avatar

@MerryChristmas The hash of the commit that you want to use as the new commit should be used. In the example provided, if you want to make commit B the new commit, then you would use the hash of commit B. If you want to make commit C the new commit, then you would use the hash of commit C.

1 like

Please or to participate in this conversation.