Delete a file from my local changes (GIT)

I made some changes in my project. When i type git log i got the next changes:

The question is: How to delete from this list of changes the file demo.js? What command should i use? I want to do this, because i don't want to add all files in my remote repository using the command git add ..

Use this command

git checkout -- <file-path>

<file-path> can be replaced by . if all files are to be removed


To remove all changes from demo.js :

git checkout -- demo.js

This post will show you how to discard all local changes in your git repository. If you have got stuck with this problem, this post is for you. To do this, let’s follow the following 2 steps: 1. Remove untracked directories in addition to untracked files git clean -fd This git clean command will remove all new directories. 2.

If you want git not to track those files, add them to .gitignore:

echo demo.js >> .gitignore
echo test.html >> .gitignore

You can place multiple .gitignore files anywhere in the repo.

A file can be removed easily from Git index and working directory by Git rmcommand. For example: git rm some_file. After adding a file in Git, you may require removing it for different reasons and at various levels.

Since it is an untracked file, you can delete it with your regular "file deletion command" of your shell. In case you want to still keep the file locally on your computer, you are better of by putting it into your .gitignore file.

git checkout master 02 Change hello.html . It happens that you modify a file in your local working directory and sometimes wish just to discard the committed changes. Here is when the checkout command will help you. Make changes to the hello.html file in the form of an unwanted comment. File: hello.html

Also, if you want to get rid of untracked files, you can use the following git command: git clean -f -d. Warning: The above command will delete all untracked files from your workspace. Conclusion. Using the above technique to force overwrite your current workspace can save your time. However, use this technique with caution. If you make it a regular habit, you might mistakenly use it and lose your local work unintentionally. Looking for help with GIT?

This will overwrite all the local changes done on your computer a duplicate copy of the version in the repository will appear. Execute the following commands in your IDE: git reset -- hard git pull. This will instantly destroy all your local changes so make sure that you know what you are doing and don’t need your local changes.

# add files git add . # diff all the changes to a file git diff --staged > ~/mijn-fix.diff # remove local changes git reset && git checkout . # (later you can re-apply the diff:) git apply ~/mijn-fix.diff [edit] as commented, it ís possible to name stashes. Well, use this if you want to share your stash ;)

Comments
  • i tried but i got: error: pathspec 'demo.js' did not match any file(s) known to git
  • this means that demo.js isn't being tracked by git (i.e, it has never been committed). So you can delete the whole file if not required else you can remove unwanted code manually and then add/commit the file. Further, if you want to keep the file locally and not commit it, you can add it to .gitignore file