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 <[email protected]>
Date: Thu Jun 2 13:51:52 2022 0530
added a toggle for viewport only visbility change
commit 4b5ce6478fb2692d03e084eabce636f859650af5
Author: analysis230 <[email protected]>
Date: Thu Jun 2 13:50:17 2022 0530
added a toggle for viewport only visbility change
commit 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f
Author: analysis230 <[email protected]>
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 <[email protected]>
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 <[email protected]>
Date: Thu Jun 2 13:51:52 2022 0530
added a toggle for viewport only visbility change
commit 4b5ce6478fb2692d03e084eabce636f859650af5
Author: analysis230 <[email protected]>
Date: Thu Jun 2 13:50:17 2022 0530
added a toggle for viewport only visbility change
commit 1bc90dd971ea1bbb36cdd280f141cb4cc317ce0f
Author: analysis230 <[email protected]>
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?
CodePudding user response:
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
CodePudding user response:
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)