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

git-checkout silently throws away the dirty status of index without a warning?

From
TLTzu-Jung Lee <roylee17@gmail.com>
Date
Sep 1, 2011, 15:47 UTC
Message-ID
<CAEvN+1h+mY+f3dzK7LFOwkqokOZSS-LosCzBqtYGbyjz=Dg7Zw@mail.gmail.com>
Hi guys,
Correct me if I'm wrong:
    git-checkout saves the changes to index and working-tree, and
tries to apply them to the destined commit.
    If the changes are applicable, then git-checkout the destined
commit and apply the changes.
    Otherwise, git-checkout fails with warnings and leaves the current
status untouched.

If the above correct. Please help me clarify if the following corner case an intended or unexpected behavior.

Setup and git repo with two commits to illustrate the scenario:
    $ git init
    $ echo aaa >> aaa.txt
    $ echo bbb >> bbb.txt
    $ git add .
    $ git commit -a -m commit1
    $ echo bbb >> bbb.txt
    $ echo aaa >> aaa.txt
    $ git commit -a -m commit2

Forge a unclean index with changes that are subset of the destined commit we are about to switching to.

    $ git checkout -b br1
    $ git reset HEAD^
    Unstaged changes after reset:
    M       aaa.txt
    M       bbb.txt
    $ git checkout HEAD aaa.txt
    $ git status --short
    M bbb.txt
    $ git add bbb.txt
    $ git status
    # On branch br1
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       modified:   bbb.txt
    #
    $ git checkout master
    Switched to branch 'master'

git silently switch to master without warning against the index are "RESTORE/RESET" to clean.

    $ git checkout br1
    $ git status
    # On branch br1
    nothing to commit (working directory clean)

Is this an intended behavior? Though the status and changes can be safely restore from the database with some manipulation. But it may become difficult and tedious if the number of involved files are large.

Regards, Roy

Next: Jeff King
Message 1 of 6 in “git-checkout silently throws away the dirty status of index without a warning?”
  1. Tzu-Jung LeeSep 1, 2011
  2. Jeff KingSep 1, 2011
  3. Tzu-Jung LeeSep 1, 2011
  4. Junio C HamanoSep 1, 2011
  5. Tzu-Jung LeeSep 1, 2011
  6. Andrew ArdillSep 2, 2011

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.