Caden Albaugh
Caden Albaugh

Reputation: 213

How do I revert back to a former Git commit and then save it in a separate branch?

I have over-engineered myself into a corner. The two things that need to happen are:

  1. How do I revert back to an earlier working version of my code?...and simultaneously,

  2. How do I then create a separate branch to save that code while still having a saved version of my new code to draw from?

Any help is greatly appreciated

Upvotes: 3

Views: 50

Answers (3)

janos
janos

Reputation: 124646

Before switching branches, if you have pending changes, commit them.

If you know the commit SHA you want to go back to, you can create a new branch from it and switch to it:

git checkout -b <new_branch_name> SHA

Upvotes: 2

Vishal
Vishal

Reputation: 589

Note down the SHA of the last commit (say e123asd)

To remove last commit from branch 1

git reset --hard HEAD^

Switch to branch 2

git cherry-pick e123asd

This will bring the last commit to this new branch.

Upvotes: 1

Makoto
Makoto

Reputation: 106430

If you know the commit SHA you want to go back to:

git reset <SHA>
git checkout -b <new_branch_name>

Upvotes: 2

Related Questions