Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't "git reset HEAD" undo my uncommitted, unstaged changes?

Tags:

git

I've previously been able to undo changes through SourceTree by performing the "Discard" function, which under the hood produces this command:

git -c diff.mnemonicprefix=false -c core.quotepath=false reset -q HEAD -- myproj.csproj 
git -c diff.mnemonicprefix=false -c core.quotepath=false checkout HEAD -- myproj.csproj

Suddenly this doesn't work. I do the Discard, no error occurs, refreesh the view, but the files are still "modified". I've then tried to do the same in the command line with the following, same result:

c:\myproject> git reset HEAD

Unstaged changes after reset:
M       myproj.csproj

Why is it still listed as an unstaged change?

I've verified that the file is indeed writable (no process is holding a lock)

update

git checkout didn't work either:

C:\myproject>git checkout myproj.csproj

C:\myproject>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:   myproj.csproj
#
no changes added to commit (use "git add" and/or "git commit -a")

Update 2 Also tried:

  • git checkout --
  • git checkout -- .
  • git checkout HEAD

,none of which solves my problem

update 3 - huge step closer:

Turns out when I do the checkout, the .csproj is indeed reverted to the correct version, but the checked out version uses a different line feed encoding. While the checked-in version has CR-LF (0D-0A) for line feed, the checked-out has only LF (0A). Hence git belives the file to be different on every single line. Why this?

update 4: added the second line of git-commands issued by SourceTree. I didn't notice that the first time around, that's why I thought git reset HEAD would do anything. This doesn't change the fact that the underlying problem still is CR/LF-related (I think)

summary I never found a solution to the issue, but I "solved" it by checking in the file. My original question didn't contain information that SourceTree indeed issued the correct commands to rollback what I wanted, so most answers here address that issue. The real issue is still unclear, but my main theory is that it was CR/LF related.

like image 325
Nilzor Avatar asked Jan 23 '14 11:01

Nilzor


People also ask

Does git reset remove uncommitted 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. Use Git reset --hard <commit id> to point the repo to a previous commit.

How do I get my changes back after resetting git hard?

We can use the command git fsck to recover the files after a hard reset.

How do I fix unstaged changes in git?

For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.

Does git reset hard head remove untracked files?

git reset --hard resets your index and reverts the tracked files back to state as they are in HEAD. It leaves untracked files alone.


1 Answers

If you need to remove local changes then run following command

git checkout -- file_name

git reset HEAD does not remove local changes.

like image 153
Thushan Avatar answered Oct 28 '22 23:10

Thushan