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

"git reset" and newly created files.

From
Junio C Hamano <junkio@cox.net>
Date
Aug 21, 2005, 19:31 UTC
Message-ID
<7vbr3rccjc.fsf@assigned-by-dhcp.cox.net>

When you made a mistake and committed a set of incomplete changes, the "git reset" command comes handy.

    ... Edit, compile, and test.
    $ git commit -s -m 'The perfect change.'
    ... Test again, OOPS it fails --- it was not perfect.
    $ git reset HEAD^
    foo: needs update
    bar: needs update

The commit object name for the botched commit is kept in the $GIT_DIR/ORIG_HEAD file, the HEAD commit and the index file are "reset" to the commit before ORIG_HEAD. The files in the working tree are not touched and they are the same as what are in ORIG_HEAD. The command tells which files in the working tree are different from the HEAD commit.

So you would naturally be tempted to do this:
    ... Re-edit, compile, and test.  This time it is perfect.
    $ git commit -a -C ORIG_HEAD

Well, not really. You can lose any file newly created in ORIG_HEAD this way. Instead, you need to do this:

    ... Re-edit, compile, and test.  This time it is perfect.
    $ git add <whatever file you have changed>
    $ git commit -a -C ORIG_HEAD
Do people find this a big problem?

Technically, what "git reset" does is correct, because its definition says the index file is reset to what was in HEAD, which did not know about the files added by the ORIG_HEAD, and there is no way for it to keep an eye on the file you wanted to add in the botched ORIG_HEAD commit.

But it does not _feel_ right.

This relates to the fact that there currently is no way to record "intent to add" (aka "please keep an eye on") in the index file. I think we could solve this with something like this:

    $ git-update-cache --add-maybe foo
    $ git status
    # Changed but not updated:
    #   (use git-update-cache to mark for commit)
    #
    #       new file: foo
    $ git-update-cache --refresh
    foo: needs update
    $ git-update-cache foo
    $ git status
    # Updated but not checked in:
    #   (will commit)
    #
    #       new file: foo

The "git reset" command should be able to detect the paths found in ORIG_HEAD but lacking in HEAD, reset the index to HEAD and say "git-update-cache --add-maybe" for such paths.

Hmm?
Next: Sam Ravnborg
Message 1 of 3 in “"git reset" and newly created files.”
  1. Junio C HamanoAug 21, 2005
  2. Sam RavnborgAug 21, 2005
  3. Junio C HamanoAug 22, 2005

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.