How do I revert my main branch to two previous commits?

0
Hi,   How do I revert my main branch to two previous commits? Could I? However, I think using the revert a commit option in Studio Pro won't fully accomplish what I want. Because reverting a commit doesn't revert to the previous revision, but rather undoes a specific commit. Isn't that right?
asked
2 answers
0

Hi Ayberk,

Not sure if I understand this correctly but here are my two cents. 

 

1. You can use Reverse Merge Changes option under Version Control to undo the changes done in specific commits and then commit it. Basically this will ensure whatever changes that were done in the two commits will be reverted. Note that you should choose lower revision first and then the higher when selecting the revisions to revert. 

 

2. You can create a new branch with the Revision you desire so that that branch will not have any changes from the two commits you want to eliminate. 

 

Regards,

Sharad R K

answered
0

Hello AYBERK ;) 

 

Try this and let me know if it works for you! 

 

The strategy is to create a new branch, apply the "undo" commits to it, and then merge that branch into main.

 

First, ensure your local copy of main is up-to-date. Then, create a new branch from it. Let's call the branch revert-feature-xyz.

 

# Get the latest version of main
git checkout main
git pull

# Create a new branch to do the revert work
git checkout -b revert-feature-xyz

You are now on the revert-feature-xyz branch, which is currently identical to main.

 

2. Find the Commit Hashes:

Find the hashes of the commits you want to undo on the main branch.

 

# This shows the history of main, even though you are on a different branch
git log main --oneline -n 3

Your output will look something like this:

a1b2c3d (origin/main, main) The most recent commit message
e4f5g6h The second most recent commit message
i7j8k9l The commit you want to be the new HEAD

3. Revert the Commits on Your New Branch:

 

Now, run the git revert commands. This will create new "revert" commits on your revert-feature-xyz branch.

Bash

 

# Revert the most recent commit
git revert a1b2c3d

# Revert the second most recent commit
git revert e4f5g6h

Your revert-feature-xyz branch now contains two new commits that undo the work from the two commits you targeted.

 

4. Push Your New Branch to the Remote:

Push this new branch up to your remote repository 

 

git push --set-upstream origin revert-feature-xyz

 

5. Create a Pull Request:

Now, go to your repository hosting service in your web browser. You should see a prompt to "Create a Pull Request" from the revert-feature-xyz branch.

 

answered