Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a folder from git tracking

Tags:

git

git-rm

I need to exclude a folder (name uploads) from tracking. I tried to run

git rm -r --cached wordpress/wp-content/uploads 

and after that I added the path to .gitignore

/wordpress/wp-content/uploads 

but when I ran git status they show up as deleted. If I try to commit the changes, the files will be deleted, not only removed from tracking.

What am I doing wrong?

I have also tried

git update-index --assume-unchanged <file> 

but this seems to untrack only files. But I need to remove an entire folder (including subfolders) from tracking.

like image 788
Stefan Avatar asked Jun 18 '14 16:06

Stefan


People also ask

How do I remove files from being tracked by Git?

To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The git rm command does that, and also removes the file from your working directory so you don't see it as an untracked file the next time around.

How do I stop a Git repository from a folder?

Remove Folder from Local Tracking Run 'git rm' command to remove the folder from being tracked in your git repository.


1 Answers

I came across this question while Googling for "git remove folder from tracking". The OP's question lead me to the answer. I am summarizing it here for future generations.

Question

How do I remove a folder from my git repository without deleting it from my local machine (i.e., development environment)?

Answer

Step 1. Add the folder path to your repo's root .gitignore file.

path_to_your_folder/ 

Step 2. Remove the folder from your local git tracking, but keep it on your disk.

git rm -r --cached path_to_your_folder/ 

Step 3. Push your changes to your git repo.

The folder will be considered "deleted" from Git's point of view (i.e. they are in past history, but not in the latest commit, and people pulling from this repo will get the files removed from their trees), but stay on your working directory because you've used --cached.

like image 189
Tod Birdsall Avatar answered Sep 22 '22 08:09

Tod Birdsall