Can't seem to discard changes in Git

After seeing the following from the command line:

# On branch RB_3.0.10
# 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:   index.htm

I am trying to discard my changes by typing the command:

  • How to make git svn clone on an SVN tag fetch the complete history?
  • Synchronizing copied repository with GIT repository
  • How can I start a clean branch with no ancestry, then commit files progressively?
  • How to display the tag name and branch name using git log --graph
  • git: where do the conflict commits go when rebasing
  • git rebase fatal: empty ident name not allowed
  • git checkout -- index.htm
    

    but when I re-run git status, it looks exactly the same. The checkout doesn’t seem to be working. Am I doing something wrong? I am using GIT 1.6.1.2 on windows/cygwin.

    # On branch RB_3.0.10
    # 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:   index.htm
    

  • Can git ignore specific lines matching a pattern?
  • Git Auto-complete not working in bash
  • Git Use Existing Files
  • Problems in rebasing in git
  • Git worktree: two worktrees point in the same location, cannot be prune
  • Escape comment character (#) in git commit message
  • 10 Solutions collect form web for “Can't seem to discard changes in Git”

    What changes does git diff show on the file? On windows, I’ve seen issues with line-endings causing issues like this. In that case, look at what settings you have for git config core.autocrlf and git config core.safecrlf. There is some documentation for these settings here.

    I would say, if you are using git svn for integration with subversion, then do make sure autocrlf is turned off. From what I can tell it is just broken in this configuration and it makes most of the tools think files have been changed, when you have done a checkout to revert any changes.

    If you are seeing a problem where you do git checkout, and then git status shows the file is still modified, and git diff shows the file is modified on every line in the file, then this is the problem you are seeing.

    core.autocrlf

    If true, makes git convert CRLF at the end of lines in text files to LF
    when reading from the filesystem, and
    convert in reverse when writing to the
    filesystem. The variable can be set to
    input, in which case the conversion
    happens only while reading from the
    filesystem but files are written out
    with LF at the end of lines.
    Currently, which paths to consider
    “text” (i.e. be subjected to the
    autocrlf mechanism) is decided purely
    based on the contents.

    core.safecrlf

    If true, makes git check if converting CRLF as controlled by
    core.autocrlf is reversible. Git will
    verify if a command modifies a file in
    the work tree either directly or
    indirectly. For example, committing a
    file followed by checking out the same
    file should yield the original file in
    the work tree. If this is not the case
    for the current setting of
    core.autocrlf, git will reject the
    file. The variable can be set to
    “warn”, in which case git will only
    warn about an irreversible conversion
    but continue the operation.

    Here is my experience, set following variables in .git/config:

    [core]
        autocrlf = false
        safecrlf = false
        eol = crlf
    

    then run $ git checkout HEAD ., and it works. but $ git checkout -- . not, strange!

    * git version 1.9.3

    This has been bothering me for a while, almost every repo I’d check out had changes that I couldn’t discard. Long story short, I tried all of the above, nothing worked. This is what I did to get things back to normal (on a Mac):

    Completely remove the autocrlf & safecrlf settings from ~/.gitconfig
    Completely remove the autocrlf & safecrlf settings from your repo's local config ./.git/config
    git rm --cached -r .
    git reset --hard
    

    It might be line endings, as @1800-information suggests, but another possibility is that the difference (that’s preventing your from reverting these files with a checkout command) is one of file mode. This is what happened to me. On my version of git you can discover this by using

    git diff index.htm

    And it will show you file mode changes. It still won’t let you revert them, though, using checkout, even with the -f option. For that use either

    git config core.filemode false

    or change your git .config in your text editor by adding

    [core]

    filemode = false
    

    After you do this, you can use

    git reset HEAD index.htm

    and the file should disappear.

    (I got all of this from the answers to How do I make git ignore mode changes (chmod)? and updating-file-permissions-only-in-git)

    I think you need to pass -f

    From the man page (man git-checkout, GIT-CHECKOUT(1)):

    -f, –force
    Proceed even if the index or the working tree differs from HEAD.
    This is used to throw away local changes.

    For instance, discard changes on the current branch and switch to a different branch:

    git checkout -f master
    

    Are you on OSX or Windows? If so, the problem probably is having two files of the same name, with different case. eg. index.htm and Index.htm

    Windows, and by default OSX, uses a case insensitive file system, which conflicts with the case sensitive git.

    I had this issue and after trying all of the above, nothing worked.

    What worked for me was to delete the directory that the file was in, then did git status and made sure that all the files in that dir are now marked as deleted. After that I simply did git checkout -f and everything was back to normal.

    I’ve had a similar issue, where it wouldn’t allow me to discard files which either does not exist or has been changed. I use Visual Studio at work, and I found that this happens when switching branches while the app is running.

    git checkout and trying to discard did not help. It wouldn’t work or it would just tell me that I do not have permission.

    Solution that worked:

    1. Go into Safe Mode
    2. Discard files

    Restarting is a pain, but this worked faster than trying out 100 things.

    There is a easy solution. If this happens (normally from unexpected windows shutdown or memory dump) and you cannot discard your changes and even switch between branches (Git says you don’t have enough permission); in Windows environment show all hidden files and folders from folder options. Go to your GIT directory (should be start with .git) and delete the "index.lock" file. Then Git should let you do whatever you want to do.

    I ended up doing a git stash followed by a git stash clean to get rid of some. Didn’t see any auto cr/lf configurations in .git/ or ~/.git stuff.

    Git Baby is a git and github fan, let's start git clone.