Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to revert 2 files that are stubbornly stuck at "Changed but not committed"?

Tags:

git

github

revert

I have a repo that has two files that supposedly I changed locally.

So I'm stuck with this:

$ git status # On branch master # Changed but not updated: #   (use "git add <file>..." to update what will be committed) #   (use "git checkout -- <file>..." to discard changes in working directory) # #       modified:   dir1/foo.aspx #       modified:   dir2/foo.aspx # no changes added to commit (use "git add" and/or "git commit -a") 

Doing git diff says that the entire file contents have changed, even though from eyeballing it that seems untrue (there seem to be common line ranges that diff seems to be failing to see).

Interestingly I don't remember changing these files locally. This repo is used with one remote repo (private, at GitHub.com, FWIW).

No matter what I've tried, I can't discard these local changes. I have tried all of:

$ git checkout -- . $ git checkout -f $ git checkout -- dir1/checkout_receipt.aspx $ git reset --hard HEAD $ git stash save --keep-index && git stash drop $ git checkout-index -a -f 

In other words I've tried everything described in How do I discard unstaged changes in Git? plus more. But the 2 files remain stuck as "changed but not committed".

What the heck would cause two files to be stuck like this and seemingly "un-revert-table"??

P.S. In the list above showing commands I'd already tried, I mistakenly wrote git revert when I meant git checkout. I'm sorry and thank you to those of you who answered that I should try checkout. I edited the question to correct it. I definitely did already try checkout.

like image 687
Greg Hendershott Avatar asked Jun 13 '11 19:06

Greg Hendershott


People also ask

How do you discard a change not committed?

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.

How do I force a git commit revert?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .


2 Answers

I spent hours trying to solve a similar issue - a remote branch that I had checked out, which stubbornly showed four files as 'Changed but not updated', even when deleting all files and running git checkout -f again (or other variations from this post)!

These four files were necessary, but certainly hadn't been modified by me. My final solution - persuade Git that they had not been changed. The following works for all checked out files, showing 'modified' status - make sure you have already committed/stashed any that have really been modified!:

git ls-files -m | xargs -i git update-index --assume-unchanged "{}" 

On Mac OSX, however xargs operates a little bit different (thx Daniel for the comment):

git ls-files -m | xargs -I {} git update-index --assume-unchanged {} 

I've added this as a placeholder for myself for next time, but I hope it helps someone else too.

-Al

like image 103
Alan Forsyth Avatar answered Sep 20 '22 13:09

Alan Forsyth


What are the line endings in the files? I'm betting they're CRLF. If they are, check out this guide: http://help.github.com/line-endings/

In short, you need to make sure git is set to convert the line endings to LF on commit, and then commit those files. Files in the repo should always be LF, files checked out should be the OS's native, assuming you set git correctly.

like image 34
Tekkub Avatar answered Sep 23 '22 13:09

Tekkub