Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a file from a Git repository without deleting it from the local filesystem

My initial commit contained some log files. I've added *log to my .gitignore, and now I want to remove the log files from my repository.

git rm mylogfile.log 

will remove a file from the repository, but will also remove it from the local file system.

How can I remove this file from the repo without deleting my local copy of the file?

like image 420
mveerman Avatar asked Jul 17 '09 14:07

mveerman


People also ask

How do I remove files from git remote and keep local?

Execute the following command: git rm --cached path/to/file . Git will list the files it has deleted. The --cached flag should be used if you want to keep the local copy but remove it from the repository.

How do I remove a file from a git repository?

The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.

How to delete a file in Git repository?

Delete Files using git rm. The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “ git rm ” command, the file will also be deleted from the filesystem. Also, you will have to commit your changes, “git rm” does not remove the file from ...

What to do when files are removed from local Git cache?

Now that the files have been removed from the local git cache, you should probably either commit the changes you’ve made, or add an entry to your local .gitignore file and then commit the changes. This will make sure that it does not happen again. Loading... AWS CodeCommit is a git code repository service by Amazon Web Services.

How to delete a file from git history?

In order to delete file from Git history, you have to use the “git filter-branch” command and specify the command to be executed on all the branches of your Git history. Finally, you want to specify the revision to execute the changes from : we are going to choose HEAD (as a reminder, HEAD is the last commit of your repository).

What happens to the Files I remove from Git when I push?

It's worth noting that the most upvoted answer is dangerous for some. If you are using a remote repo than when you push your local then pull elsewhere those files you removed from git only WILL BE DELETED. This is mentioned in one of the replies but not commented upon. to do it the right way: stackoverflow.com/questions/57418769/…


2 Answers

From the man file:

When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

So, for a single file:

git rm --cached mylogfile.log 

and for a single directory:

git rm --cached -r mydirectory 
like image 174
bdonlan Avatar answered Oct 13 '22 18:10

bdonlan


To remove an entire folder from the repo (like Resharper files), do this:

git rm -r --cached folderName 

I had committed some resharper files, and did not want those to persist for other project users.

like image 44
Sam Tyson Avatar answered Oct 13 '22 18:10

Sam Tyson