Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout -- <files> doesn't discard changes?

I have changes in my working directory that I'm trying to discard (reset to the current indexed version of the files), however, git checkout -- <file> will not discard the changes. I've attempted to manually remove the files (rm -r files) then run git checkout -- ., which displays the files as modified again.

$ git checkout -- .
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   files/Hulk.png
#   modified:   files/Hulk_2.png
#
no changes added to commit (use "git add" and/or "git commit -a")

Running git diff shows the files are modified...

diff --git a/files/Hulk.png b/files/Hulk.png
index 1c256cb..1d37fe0 100644
Binary files a/files/Hulk.png and b/files/Hulk.png differ
diff --git a/files/Hulk_2.png b/files/Hulk_2.png
index 1c256cb..0717199 100644
Binary files a/files/Hulk_2.png and b/files/Hulk_2.png differ

NOTE: Some people have said to run git checkout ., however this will achieve the same result as git checkout -- .. The -- is just a notation used in the git checkout command to differentiate treeish/commit points from files/paths.

OS: OSX 10.6 Git: 1.7.10.2

like image 227
Highway of Life Avatar asked Jul 12 '12 19:07

Highway of Life


People also ask

Does git checkout discard changes?

Git stash lets you discard changes and save them for later reuse. Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes.

How do you discard changes in git?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.

Does git checkout update files?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.


3 Answers

The cause for this was due to multiple files with the same name but different cases. In OSX, which is case-insensitive, doesn't like multiple files with the same name but different cases. It views them as the same file. To fix this, I ran git mv (or just mv) to a temporary filename, added the temp files, which allowed git to remove the old/incorrectly named versions, then a second commit to name them back. This could also be corrected on a filesystem which allows different files with the same name to be different cases.

like image 152
Highway of Life Avatar answered Oct 19 '22 05:10

Highway of Life


Did you try

git config --global core.autocrlf false

or

git config --global core.filemode false
like image 9
vinboxx Avatar answered Oct 19 '22 07:10

vinboxx


Based on your comments, you must configure your repository to be case sensitive:

git config core.ignorecase false

This allows git to track both files (although the file system only shows one, which is enormously confusing). Here are replication steps to demonstrate what's happening, when git is correctly tracking case sensitivity:

git init /tmp/test && cd /tmp/test
git config core.ignorecase false
echo test>test && git add test && git commit -m "lowercase t"
mv test Test

Now git status shows no differences to test:

git status -s
 ?? Test

Commit Test and use git ls-files to see what we're now tracking:

git add Test && git commit -m "uppercase T"
git ls-files
 Test
 test

What does ls report? Why, just 'Test', naturally:

ls
 Test

Finally, what happens when we modify Test?

echo garbage>Test
git status -s
 M Test
 M test

What a mess.

like image 6
Christopher Avatar answered Oct 19 '22 07:10

Christopher