-3

I'm working on a project and I wanted to revert back to an earlier commit. This is the git log

commit a342294cbf25550f48452807d33a64f0c0248c34 (origin/master)
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Thu Jun 2 13:51:52 2022 +0530

    added a toggle for viewport only visbility change

commit 4b5ce6478fb2692d03e084eabce636f859650af5
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Thu Jun 2 13:50:17 2022 +0530

    added a toggle for viewport only visbility change

commit 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Wed Jun 1 23:53:21 2022 +0530

    changed git ignore

My head was at commit a342294cbf25550f48452807d33a64f0c0248c34 and I wanted to revert to 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f

I executed git revert HEAD~2 I got the space to write a new message and I wrote a commit message expecting that all the files should now look like they did in commit 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f, but they don't. All the files look like commit a342294cbf25550f48452807d33a64f0c0248c34 but it shows a new commit in the log. Here's the log:

commit 6ee3550ad817839afbebe2b039653c4f00f3c074 (HEAD -> master)
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Thu Jun 2 15:10:43 2022 +0530

    Revert "changed git ignore" Because for some reason the model doesn't load in blender with the new changes.
    Would look over the Coin class

    This reverts commit 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f.

commit a342294cbf25550f48452807d33a64f0c0248c34 (origin/master)
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Thu Jun 2 13:51:52 2022 +0530

    added a toggle for viewport only visbility change

commit 4b5ce6478fb2692d03e084eabce636f859650af5
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Thu Jun 2 13:50:17 2022 +0530

    added a toggle for viewport only visbility change

commit 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f
Author: analysis230 <vipul.s.paul@gmail.com>
Date:   Wed Jun 1 23:53:21 2022 +0530

    changed git ignore

if I do git checkout 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f, I see the version of the file that I expect to see after the revert. Here's an example:

when I checkout 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f, this is what I see in one of my files.

class ParamNames:
    widthOffset = "WidthOffset"
    heightOffset = "HeightOffset"
    precision = "Precision"
    seamAbberations = "Seam Abberations"

    amount = "Amount"
    probability = "Probability"

class Keywords:
    layerNumber = "layerNumber"

when I checkout master branch after the revert i.e. on commit a342294cbf25550f48452807d33a64f0c0248c34, the same file looks like this, which is all the changes I want to remove:

from random import Random

class ParamNames:
    widthOffset = "WidthOffset"
    heightOffset = "HeightOffset"
    precision = "Precision"
    seamAbberations = "Seam Abberations"

    amount = "Amount"
    probability = "Probability"

    seed = "Seed"

class Keywords:
    layerNumber = "layerNumber"

class BiasedCoin:
    def __init__(self, seed):
        self.randGenerator = Random()
        self.randGenerator.seed = seed

    def toss(self, headProb):
        weights = [100-headProb, headProb]
        return self.randGenerator.choices([0,1], weights=weights)[0]

    def uniform(self,a,b):
        return self.randGenerator.uniform(a,b)

Is my understanding of git revert wrong?

Vipul Rajan
  • 494
  • 1
  • 5
  • 16
  • You don't revert _to_ a commit. `git revert ` created a new commit that reverses the changes of just that specified commit. Based on the message I expect you'd see this if you looked in `.gitignore`. You probably wanted to _reset_ back to that commit. – jonrsharpe Jun 02 '22 at 10:03
  • @jonrsharpe wouldn't reset remove the newer commits? I want to keep those, I might reuse some code after debugging – Vipul Rajan Jun 02 '22 at 10:07
  • You can soft reset to keep the changes in your working area, or go and find the old commits in the reflog. It's not entirely clear what result you wanted. – jonrsharpe Jun 02 '22 at 10:08
  • I want a new commit that looks exactly like the commit I'm trying to reset to, all the files should be the same, but I don't want the 'unwanted' or 'reversed' commits to be deleted from my log. – Vipul Rajan Jun 02 '22 at 10:13
  • Then you wanted to revert _multiple_ commits, HEAD and HEAD~1, not the single commit HEAD~2. – jonrsharpe Jun 02 '22 at 10:15
  • Yeah, that's why I finally did. Thanks for your help. – Vipul Rajan Jun 02 '22 at 10:16
  • "Is my understanding of git revert wrong?" Yes, very much so. – matt Jun 02 '22 at 10:37

2 Answers2

0

You've confused revert and reset. You wanted reset. You don't revert to a commit, you reset to a commit.

Since you don't want to lose any work, make a branch to keep it; then reset hard. So, assuming your head is at a342294, say:

git branch failed-experiment 
git reset --hard 1bc90dd
matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You can not use revert as tool to remove something from history. But you can "amend" your commit if you have not pushed it still. (See docs - it is simple from IDE or tool like Sourcetree or from command line)

If you do not want to keep history of your commit and your reverse actions, then most practical way is to create new branch from last correct commit and then do old_branch_delete and new_branch_rename. See docs)