git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Sitaram Chamarty <sitaramc@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: "git reset --hard" == "git checkout HEAD" == "git reset --hard HEAD" ???
Date: Sat, 03 Jan 2009 02:15:37 -0800	[thread overview]
Message-ID: <7vprj4ae5y.fsf@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <slrngltvrd.63u.sitaramc@sitaramc.homelinux.net> (Sitaram Chamarty's message of "Sat, 3 Jan 2009 06:02:21 +0000 (UTC)")

Sitaram Chamarty <sitaramc@gmail.com> writes:

>>> It seems we have 2 ways to blow away work we haven't
>>> checked in yet then right?
>>
>> Wrong.
>
> Strictly as asked, yes, but what if he adds a "-f" to the
> middle command, making it "git checkout -f HEAD"?  Wouldn't
> that be the same as the others then?

Yeah, but comparing reset and checkout misses a whole _dimension_ in the
revision space continuum.

"git checkout <branch>" is primarily about checking out a different
branch, aka "switching branches".

When you switch branches, you generally do not want to lose your pending
changes, but would want to take them with you.  A typical scenario is that
you start looking at one issue, you fiddle a few lines here and twiddle a
few lines there, and as you dig deeper, you realize that whatever the
final shape of the change you are going to make will be either (1) big
enough to deserve its own branch created anew, or (2) better done as an
extension to an existing branch.  You realize that you are in the latter
situation by noticing that the modification you were making will be helped
by something you have implemented in the other branch but not yet
available in the current one (typically the latter is 'master').

In such a case, you will "git checkout <the-appropriate-topic>" to switch
to the branch, and you would want to take the change you already made to
your work tree files when you do so.

On the other hand, "git checkout -f <branch>" blows away your changes, but
it still _is_ about switching to a different branch.

Whether you use -f or not, you are allowed to ask to switch to the current
branch by (1) naming the branch explicitly, i.e. "git checkout -f master",
(2) using HEAD to mean the current one instead, or (3) omitting <branch>
altogether.  But that is there merely for consistency and, even though
there may not make much sense to do so (because it is largely no-op except
that you would get the "you are ahead by N" notice), there is no strong
reason to forbid asking for a no-op.

For that reason, "git checkout -f HEAD" is "blow away my changes".  But it
is merely a degenerated case of "switching to the current branch while
blowing away my changes."

"git reset --hard <commit>" is different.  "reset" is primarily about
pointing the tip of the current branch to somewhere else.  While "git
checkout <branch>" never changes what commit sits at the tip of any
branch, "git reset <commit>" modifies it for the current branch (--hard
variant matches the work tree files to the contents recorded by the
resetted-to commit while at it).

Again, you are allowed to ask to reset to the current HEAD by saying
"reset --hard HEAD".  That is a degenerated case of "resetting the tip of
the current branch, while blowing away my changes".  More general case
would be "reset --hard <some-commit>" and it won't just blow away your
changes (relative to the commit you started out with), but also blows away
the history leading to the commit the branch tip used to point at.

"checkout -f" and "reset --hard" work on different dimensions, and what
they do intersect when (and only when) the <branch>/<commit> argument
happen to be HEAD.  "checkout -f <another>" and "reset --hard <another>"
will do quite different things.

  reply	other threads:[~2009-01-03 10:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-02 19:57 "git reset --hard" == "git checkout HEAD" == "git reset --hard HEAD" ??? chris
2009-01-02 22:36 ` Junio C Hamano
2009-01-03  6:02   ` Sitaram Chamarty
2009-01-03 10:15     ` Junio C Hamano [this message]
2009-01-03 12:27       ` Sitaram Chamarty

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=7vprj4ae5y.fsf@gitster.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=sitaramc@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).