0

I have 2 files that are in my working directory. I want to completely discard these files so I can switch branches.

There are many helpful guides online, and I have tried every answer I can find to remove these files, yet they still persist.

user@machine:~/Code/foo$ git status
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:   src/test/foo.c
    modified:   src/test/bar.c

Commands I have tried based on other stackoverflow questions.

 git reset 

 git reset --hard HEAD^

 git stash save --keep-index
 git stash drop

 git checkout HEAD -- $(git ls-files -m)

 git clean -f
 git clean -dfx

 git checkout -- .

 git checkout -- *

 git checkout 123456

 git reset --hard 123456

 git reset HEAD --hard

Amazingly, after every command, I find the files are still present!

user@machine:~/Code/foo$ git status
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:   src/test/foo.c
    modified:   src/test/bar.c

Since there are only 2 files, I know I could just recheck them out one by one. However if this happened again and I had 100 files, I want to know the procedure to blow all of them away in one sweep.

 git checkout src/test/foo.c
 git checkout src/test/bar.c

Update

Aparently not even a git checkout works

git checkout src/test/foo.c
echo $?
0
git checkout src/test/bar.c
echo $?
0

user@machine:~/Code/foo$ git status
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:   src/test/foo.c
    modified:   src/test/bar.c

These files just wont go away.

Update2

I've tried these command as well with no luck.

git reflog
git checkout HEAD@{17}

git init
git reset --hard HEAD^

Update3

Stashing does nothing.

git checkout master
git stash
git stash pop
git stash drop

Deleting the files does not work either.

user@machine:~/Code/foo$ git rm --cached $(git ls-files -m)
rm 'src/test/foo.c'
rm 'src/test/bar.c'

user@machine:~/Code/foo$ git status
On branch master
Your branch is behind 'origin/master' by 162 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

    deleted:   src/test/foo.c
    deleted:   src/test/bar.c
spuder
  • 17,437
  • 19
  • 87
  • 153
  • `git checkout -f yourCurrentBranch` – Jonathan de M. Apr 14 '14 at 15:39
  • possible duplicate of [How do I clear my local working directory in git?](http://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git) – CodeManX Apr 14 '14 at 15:39
  • Yes, I saw that question, and I've tried every answer I could find in it. No luck – spuder Apr 14 '14 at 15:44
  • possible duplicate of [How do I revert all local changes in a GIT managed project to previous state?](http://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-a-git-managed-project-to-previous-state) – jthill Apr 14 '14 at 15:47
  • Yes, I've read that question too, and I've tried all the answers suggested there with no luck. – spuder Apr 14 '14 at 15:49
  • It almost sounds like some other application that's running has those files open and resaves them to disk when a change in the file is detected. I know it's kind of extreme, but have you tried closing all applications and doing a full shutdown and restart of your computer? – Greg Burghardt Apr 14 '14 at 16:42
  • Good idea @GregBurghardt I'll try a restart. – spuder Apr 14 '14 at 16:48
  • @GregBurghardt restarted, yet the issue continues. This is mildly infuriating. – spuder Apr 14 '14 at 19:05

3 Answers3

1

For all unstaged files use (note the period at the end). This works for me (as of git v 1.8.4.5)

git checkout -- .

alternatively you can put everything in stash;

git stash

that way you can still come back to the way you were, or you can even apply those changes on another branch:

git stash pop (this command applies the latest stash)

or if you don't need the changes anymore:

git stash drop
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
1

This sounds very much like line-ending troubles, you've told git to do automatic conversions on checkin and the result of those conversions won't match the committed state. Check .git/info/attributes and .gitattributes for text attributes:

text

This attribute enables and controls end-of-line normalization.When a text file is normalized, its line endings are converted to LF in the repository.

If someone has checked in files containing CRLFs, I understand text=auto is a good way to, erm, find them.

jthill
  • 55,082
  • 5
  • 77
  • 137
0

Figured it out.

In git diff, it showed that the file name was lowercase foo.bar, however when I actually inspected the file it was Foo.bar.

I am on a mac which has a case insensitive file system.

I should be able to fix it by manually moving the files

Case sensitivity in Git

Community
  • 1
  • 1
spuder
  • 17,437
  • 19
  • 87
  • 153