Reputation: 19320
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://[email protected]':
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://[email protected]':
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
Upvotes: 0
Views: 3562
Reputation: 761
'.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.
Upvotes: 0
Reputation: 58637
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.
Upvotes: 0