0

I'm using git on Mac OS X. How do I revert changes to a file? The changes aren't committed yet. I tried

localhost:myproject davea$ git checkout -- .gitignore
error: pathspec '.gitignore' did not match any file(s) known to git.

The above error doesn't make sense because when I try and pull from the remote repository, it complains taht I have the file it can't overwrite ...

localhost:myproject davea$ git pull origin master
Username for 'https://github.com': myuser
Password for 'https://myuser@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/JBSinc/myproject.git/'
localhost:myproject davea$ git pull origin master
Username for 'https://github.com': myuser
Password for 'https://myuser@github.com': 
From https://github.com/JBSinc/myproject
 * branch            master     -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
    .gitignore
Please move or remove them before you can merge.
Aborting
Zoe
  • 27,060
  • 21
  • 118
  • 148
Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

0

Let's start out by assuming that by "not commited yet", you mean that the changes are just in the working tree, and not staged in the index. Files staged in the index are also "not committed yet".

To revert changes to a file, a good way is:

git checkout --patch <filename>

this will interactively walk you though the "diff hunks" so that you can selectively discard some changes while keeping others. Without the --patch, the whole file is restored noninteractively. Of course, multiple files can be specified. If you don't give any file names, then all files with changes are processed.

The file is restored to the version that is in the index. So that is to say, if you have made some changes to the file and then staged it with git add <file>, and then made subsequent changes which you are now reverting, they will be reverted against the staged version of the file.

To revert the staged changes, an easy way is to first unstage them with git reset:

git reset <file>

Now all the changes in <file> are in the tree; none are staged, and we can go through the git checkout exercise above.

Working copy changes discarded with git checkout are permanently gone; if you think you might change your mind, keep backups or stash the changes with git stash instead.

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • Is there any advantage to `git checkout -- ` over `git restore `? Seems like they both work. – Woodchuck Aug 22 '22 at 16:38
  • 1
    @Woodchuck My answer was written in 2018. The [commit](https://github.com/git/git/commit/46e91b663badd99b3807ab34decfd32f3cbf15e7) which added the `restore` command was in 2019. `git checkout` will work on older git installations that haven't picked up that feature. It's an old command that "everyone" understands. – Kaz Aug 23 '22 at 05:15
0

'.gitignore' is the one file that prevents your git commands from affecting whatever files or directories defined in it. if it's not useful to you, you may remove it with rm as suggested above. your git commands will not affect it. also, in other cases when you have untracked files you would like to get rid of, you can always use:

git clean --force

this will silently remove anything not committed and your directory will be clean.

Oren_C
  • 565
  • 7
  • 22