finnahuss
finnahuss

Reputation: 307

What happens to a modified file when using "git checkout"?

I was simply trying out a few things on a project and wanted to delete some modified files I was working with but no longer needed.

I got careless and typed git checkout file instead of git checkout -- file. So essentially I was just checking out this modified file and I have no clue where that modified file went off to.

I've tried using git update-index --fresh just to try and see where this modified file could have gone but it seems like it may have been deleted in the end?

    modified:   test/ssc_test/cmod_tcsdirect_steam_test.cpp
    modified:   test/ssc_test/cmod_tcsfresnel_molten_salt_test.cpp
    modified:   test/ssc_test/cmod_tcsmolten_salt_test.cpp

These were the files I was trying to get rid of but forgot to add the -- to get rid of one of the edited files.

After checking out this file, instead of deleting it, it gave me the message, Updated 1 path from the index and it was no longer displayed along with the other modified files when using git status.

I'm pretty clueless with what happened exactly. Was it was deleted or hidden somewhere in the index, or something else?

Upvotes: 9

Views: 6892

Answers (3)

VonC
VonC

Reputation: 1324606

In order to avoid any ambiguity between branch and file, you can use the new git restore command. See git restore.
It is available with Git 2.23, still experimental for now.

git restore -- afile

It will restore working tree files from any source you want.
By default, HEAD, but you can specify any treeish you want.

Using or not -- won't change anything.

Upvotes: 3

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

I got careless and typed 'git checkout file' instead of 'git checkout -- file'

Those commands do the same thing, unless for some reason teh actual name of file could be confused with a non-path argument to checkout.

Or to say that differently - the only thing -- does is to separate paths from other options in case it's ambiguous

Upvotes: 10

Julie Nielsen
Julie Nielsen

Reputation: 91

Git will set the file to the working head/index.

To test and illustrate:

mkdir test
cd test
git init
touch testfile
git add --all
git commit -m "Commit"
echo hi > testfile
cat testfile # hi
git checkout testfile
# Testfile is now empty

Upvotes: 2

Related Questions