All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Chris Torek <chris.torek@gmail.com>
Cc: tao lv <thebookofunknowable@gmail.com>,  git@vger.kernel.org
Subject: Re: How to revert to a specific commit?
Date: Tue, 03 Dec 2024 09:57:29 +0900	[thread overview]
Message-ID: <xmqqser5h9c6.fsf@gitster.g> (raw)
In-Reply-To: <CAPx1Gve_dCRux9_cF7NTspomS4K=Hp_y74d94S-Hm0amuovUqQ@mail.gmail.com> (Chris Torek's message of "Mon, 2 Dec 2024 04:39:13 -0800")

Chris Torek <chris.torek@gmail.com> writes:

> 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?
>
>     git checkout [--detach] <hash>   # or git switch --detach <hash>
>     git reset --soft <branch-name>
>
>     git restore --no-overlay -S -W <hash>

While that would _work_ in the sense that you can revert the effect
of what the discarded commits did, I would not recommend any of the
above since it does not leave any record of what you discarded.

Imagine that I regret doing everything since we tagged, say, v2.47.0
release and want to discard everythig on 'master' newer than that
commit.  If I want to "keep" the history of failed commits that I
regret having made since v2.47.0, here is a way to do it:

 $ git checkout master
 $ git reset --hard v2.47.0
 $ git merge -s ours --no-ff -m 'Discard everything since v2.47.0' master@{1}

The first two steps just discard the unwanted commits.  The third
step is a trick to

 - Keep the tree contents of the current commit (i.e. -s ours) as
   the result,

 - Make sure the result is recorded as a merge commit (i.e.
   --no-ff), not "fast-forward" to the tip of the history I am
   discarding, and

 - Record the discarded history (i.e. master@{1}) as the side branch
   of the resulting history.

The last part would help if I later need to merge changes that were
based on 'master' I am discarding.  The presense of the side branch
makes sure that only the effects from the new work done on the
discarded history, and not from the commits such a new work was
based on (which are the commits I regret having made and I am
discarding), are taken into account when computing further merges.

  reply	other threads:[~2024-12-03  0:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-12-03  2:45     ` Chris Torek
2024-12-02 12:50 ` Matěj Cepl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqser5h9c6.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=chris.torek@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=thebookofunknowable@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.