-1

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Martin Müsli
  • 1,031
  • 3
  • 14
  • 26
  • `git revert <1-SHA> <2-SHA> <3-SHA>`? It sounds like you want a commit that undoes the last 3 commits, not the 4th (which is what `git revert <4-SHA>` did). – jonrsharpe Nov 20 '21 at 12:27
  • If the branch in question is published on the repo _and_ shared by others, then `git revert` might be the best option here. – Tim Biegeleisen Nov 20 '21 at 12:27
  • `$git revert <1-SHA> <2-SHA> <3-SHA>` so if I had, say 50 commits I needed to provide all 50 commit shas? @jonrsharpe Is there a shortcut? – Martin Müsli Nov 20 '21 at 12:28
  • If that _is_ what you're trying to do, dupe is https://stackoverflow.com/q/1463340/3001761. – jonrsharpe Nov 20 '21 at 12:29
  • @jonrsharpe I have seen your question as well but the guy asking wants to remove `.e. I want B, C, D, and HEAD to disappear `. I want to keep those changes in history, or do I misunderstand? – Martin Müsli Nov 20 '21 at 12:31
  • They're explicitly asking how to revert rather than rewrite history, and various of the answer use the `git revert` command, so I think you do, yes. – jonrsharpe Nov 20 '21 at 12:32
  • 2
    Note that "closed as duplicate" does not imply that the other question is better, or yours should go away, or anything of the sort: it just means that these are two different ways to ask the same question, in the end. Leaving yours in existence as a closed duplicate will let others find the question using your wording. – torek Nov 20 '21 at 23:41

2 Answers2

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-1

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

Martin Müsli
  • 1,031
  • 3
  • 14
  • 26