Home > other >  Git recreate previous state
Git recreate previous state

Time:11-20

I have a git repository on GitHub. My last three commits are changes, that I now want to rollback, but keep them in the git history.

Master-Branch:

- HEAD: 3. Change; removed one of added files, added new line in main.js
- -1  : 2. Change; removed line in one of added files
- -2  : 1. Change; Added two files
- -3  : Stuff <-- This is the status of the project that I want
  • Now, I tried $ git revert <COMMIT-SHA> but this only resets changes for that specific commit and ignores all the other commits that happend in that time.

  • $git reset <COMMIT-SHA> throws away all the changes that I made.

Is there a simple way to recreate the state of a specific commit, push it as a new commit and effectively get your previous state without going through all changes manually?


Note:

I would like to keep this question, since the wording of "previous state" is more descriptive for git beginners than "revert"

CodePudding user response:

As I explain in "Revert a range of commits in git", what you need is

git revert commit3..HEAD

It will revert everything after commit 3, up to HEAD commit, making one new commit reverting the previous commits (except commit3)

So this is not so about reverting multiple commits, than it is about reverting a range of commits.

CodePudding user response:

Kinda works:

$ git revert --no-commit <COMMIT SHA>..

puts me in git revert mode, change by change, so I can have a commit for each change that happened

  • Related