git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: 'git checkout -f' versus 'git reset --hard'

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Apr 5, 2007, 15:43 UTC
Message-ID
<Pine.LNX.4.64.0704050837330.6730@woody.linux-foundation.org>
In-Reply-To
<17940.64329.10165.993967@lisa.zopyra.com>
On Thu, 5 Apr 2007, Bill Lear wrote:
Show 5 quoted lines
>
> After a failed merge, I want to undo things.  I typically use git
> reset --hard, and it works like a charm.  Others have tried to use git
> checkout -f, but I have cautioned that git reset --hard is really the
> way to do it.  Is there a difference here, or are they equivalent?

They are equivalent, but *only* if not given any other arguments (or if given "HEAD", which is obviously the default).

HOWEVER! If you give them an argument, they work very differently. So
	git checkout -f <branchname>
	git reset --hard <branchname>

are fundamentally different operations: the "git checkout -f" thing will *switch* to the named branch (or create a detached HEAD if it's just a random commit SHA1 and not a branchname), while the "git reset --hard" thing will *force* the current branch top commit to the same as for <branchname>.

It just so happens that without any branchname they both default to the "current branch", and then the two end up doing the exact same thing, even though conceptually they are very different operations.

So they really are fundamentally different, it's just that for the common special case they end up doing the same thing.

		Linus
Previous: Shawn O. Pearce
Message 4 of 4 in “'git checkout -f' versus 'git reset --hard'”
  1. Bill LearApr 5, 2007
  2. Jeff KingApr 5, 2007
  3. Shawn O. PearceApr 5, 2007
  4. Linus TorvaldsApr 5, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.