* Re: How to revert to a specific commit?
2024-12-02 11:40 How to revert to a specific commit? tao lv
@ 2024-12-02 12:39 ` Chris Torek
2024-12-03 0:57 ` Junio C Hamano
2024-12-02 12:50 ` Matěj Cepl
1 sibling, 1 reply; 5+ messages in thread
From: Chris Torek @ 2024-12-02 12:39 UTC (permalink / raw)
To: tao lv; +Cc: git
On Mon, Dec 2, 2024 at 4:15 AM tao lv <thebookofunknowable@gmail.com> wrote:
> I want to revert the code to a specific commit. ...
OK, first some background:
* A commit _is_ a full snapshot of every file, as of the state
it had at the time you (or whoever) made that particular
commit. Hence, if you want the particular files from a
particular commit, you simply check out that (historical)
commit.
Now:
> I don't want to revert each individual commit; I just want to restore
> the code to this specific commit while retaining all the current
> history commits.
>
> How can I achieve this? Is there a better way than using the revert function?
Yes.
The mechanics of:
git checkout <hash-ID-or-other-commit-specifier>
are twofold, and mean the same thing as:
git switch --detach <same-hash-ID-etc>
That is, you are asking Git to:
1) switch to the saved snapshot, and
2) "detach HEAD".
Step 2 is your problem here because the jargon phrase
"detach HEAD" means "stop being on any branch at all,
just go to some historical commit". But you seem to want
to subsequently make a *new* commit that contains the
*old snapshot*, as if you had removed all the work you
(or anyone else) had done since that point in time and put
all the files back the way they were in the historical commit,
*while remaining on your branch*, so that you are now
ready to commit all the old versions of the files.
Note that this new commit, if and when you make it,
simply sits atop the history (and / but, since every commit
is a full snapshot of all files, it undoes all the *work* done
since the restored commit -- though, since all commits
are also *permanent*, that undone work can be redone
trivially as well).
So, at this point, you have several options for
achieving this goal, provided I have recapitulated
your goal correctly. The most obvious, I think, is:
git checkout [--detach] <hash> # or git switch --detach <hash>
git reset --soft <branch-name>
The checkout-or-switch does what it does, including
detaching HEAD, and then the `git reset --soft` tells Git
that it should re-attach `HEAD` to the given branch name,
but not alter *either* the index / staging-area, *or* the
working tree, leaving the historical versions of all files
as "ready to commit".
=====
A different, and more direct, option is to use:
git restore --no-overlay -S -W <hash>
The `git checkout` command can be used in place
of this `git restore`, because `git checkout` itself is (in
my opinion) overly complicated and stuffed full of modes,
so that it has a mode in which it *doesn't* affect `HEAD`.
Understanding this requires keeping in mind the way
I described `git checkout` has having a step-1-then-step-2,
even though the two-step `git checkout` actually very
carefully *combines* the two steps (to avoid making any
changes should the second step be about to fail for some
reason). I find that describing checkout as two separate
steps, and literally using an additional `git reset --soft`
step, more explainable (Git is complicated! Nobody learns
all of it, it is always changing over time! Those new to it
find it very mysterious, and this is part of why).
Chris
(PS: the fact that you need `--no-overlay` with the
single-step methods is another one of those picky
little details that make Git tricky.)
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: How to revert to a specific commit?
2024-12-02 11:40 How to revert to a specific commit? tao lv
2024-12-02 12:39 ` Chris Torek
@ 2024-12-02 12:50 ` Matěj Cepl
1 sibling, 0 replies; 5+ messages in thread
From: Matěj Cepl @ 2024-12-02 12:50 UTC (permalink / raw)
To: tao lv, git
[-- Attachment #1.1.1: Type: text/plain, Size: 771 bytes --]
On Mon Dec 2, 2024 at 12:40 PM CET, tao lv wrote:
> I don't want to revert each individual commit; I just want to restore
> the code to this specific commit while retaining all the current
> history commits.
Either git checkout <commit-id> for an inspection or git reset
--hard <commit-id> for change of the current branch to that
commit.
Matěj
--
http://matej.ceplovi.cz/blog/, @mcepl@floss.social
GPG Finger: 3C76 A027 CA45 AD70 98B5 BC1D 7920 5802 880B C9D8
Books aren’t written - they’re rewritten. Including your own. It
is one of the hardest things to accept, especially after the
seventh rewrite hasn’t quite done it.
-- Michael Crichton, alluding to Steele MacKaye (1889) article
where he said this about theater plays.
[-- Attachment #1.2: E09FEF25D96484AC.asc --]
[-- Type: application/pgp-keys, Size: 3102 bytes --]
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQGiBD2g5T0RBACZdnG/9T4JS2mlxsHeFbex1KWweKPuYTpnbu8Fe7rNYMWZ/AKc
9Vm+RuoVErm4HGsb0pL5ZPnncA+m80W8EzQm2rs8PD2mHNsUhDOGnk+0fm+25WSU
6YLzd8lttxPia75A5OqBEAmJ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QoLBBYCAwECHgECF4AACgkQ4J/vJdlkhKwsQQCdGmGXW73O6Q3TB0V0
xP9yLwMjDtEAnjKWDW8PKO90nx8IkPodxr1nCvJbtBpNYXRlaiBDZXBsIDxtY2Vw
bEBzdXNlLmN6PohjBBMRAgAjBQJa+uRPAhsDBwsJCAcDAgEGFQgCCQoLBBYCAwEC
HgECF4AACgkQ4J/vJdlkhKyKtQCdHDpolHg/1qDaw/4CQyUzAfNvHk0AniEYL6BF
rdyonhgQf/ZXzXjnKzSeuQENBD2g5UEQBACfxoz2nmzGJz6ueKHkTeXcQZvK4WzK
TN/uJJhEmSuQmOKymbIkGL6vBQb+W4KxvLl2lAbNlfIgLGDLCs1YAwfSpJ4vS4mt
liPgA2OtZ5j1WSOqpxedQPGVba5gVo7HNSOMUtZKTz7VsCvR94v05comhO1Gok75
ZxHtYyVHuk5V8wADBQP/ft+W4F0tccwslzz8O/c9/Mj8KZDYmfMyNb7ielT2WeQ3
iFF9AxMT6OvOxAQbDJvurfKeYlydcXLs6cy4lKce1hFaJ4i+MOFLVV1ZnZDDChRP
pQ6KrRCHLb+mLY+SYD37O7p0spQA+9gsEE/tmn+5sW7LE8hqSOoPVdf7Y5yUDj6I
RgQYEQIABgUCPaDlQQAKCRDgn+8l2WSErEUSAJ42T1l/2TFykbULBqqAtnbC6kR0
wwCdEnRlCGlvnO78R0FgKXlt3RyzGuE=
=sxoW
-----END PGP PUBLIC KEY BLOCK-----
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 216 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread