Remove a folder from git tracking
Asked Answered
M

8

725

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.

Motivity answered 18/6, 2014 at 16:10 Comment(3)
Deleted doesn't means git is going to delete your files. However, if you'll use reset --hard, it will.Caesarean
As @Caesarean mentions, committing will not cause your files to be deleted. It will commit their deletion from Git, but your working copy will still contain them. But if you pull that commit somewhere else those files will be deleted from the new system.Insubstantial
to see a list of tracked files git ls-filesLovellalovelock
D
1495

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.

Dourine answered 20/5, 2015 at 21:46 Comment(10)
Quick question, does the folder path have to be the full path? or just the path with the git repository acting as the base?Singh
I believe it is the full path, but don't remember for sure.Dourine
"and people pulling from this repo will get the files removed from their trees" Can I prevent this?Janot
this correctly removed my files from tracking on a branch, but deleted them after a merge!Morten
Before step 2 it's nessessary to change a current directory to repository root folder, like this: cd path_to_root_pero_folderSuspend
for me still remains. Also - should I unselect those files when commiting which are in the folder? I tried both ways. If unselecting, it does not allow to push, but then I changed one char in another file which should be tracked. Still does not helpPlausible
I think only modifying the .gitignore file is enough.Woodsum
in addition to this answer you can add file extension in .gitignore of you don't want those files to be updated every time, like uploaded mediaExperimentation
on Windows the paths might be case sensitive. In my case the committed Path was Test and the currently existing path was test. Removal only worked with the same casing as was committed.Mayda
Until recently, the --cached parameter has been the most overlooked and powerful git command I've not been using.Bankable
J
101

This works for me:

git rm -r --cached --ignore-unmatch folder_name

--ignore-unmatch is important here, without that option git will exit with error on the first file not in the index.

Jandy answered 18/8, 2017 at 11:57 Comment(2)
unknown option 'ignore unmatched'Respect
very useful if you want to just remove certain file extensions from a directory too: git rm -r --cached --ignore-unmatch .idea/*.xmlOriginate
R
53

I know this is an old thread but I just wanted to add a little as the marked solution didn't solve the problem for me (although I tried many times).

The only way I could actually stop git form tracking the folder was to do the following:

  1. Make a backup of the local folder and put in a safe place.
  2. Delete the folder from your local repo
  3. Make sure cache is cleared git rm -r --cached your_folder/
  4. Add your_folder/ to .gitignore
  5. Commit changes
  6. Add the backup back into your repo

You should now see that the folder is no longer tracked.

Don't ask me why just clearing the cache didn't work for me, I am not a Git super wizard but this is how I solved the issue.

Rattigan answered 1/2, 2019 at 14:7 Comment(1)
Thank you!!! Git was driving me crazy not ignoring the eclipse .metadata folder. Followed your steps with the eclipse .metadata folder, and it finally worked.Brachiate
S
29

To forget directory recursively add /*/* to the path:

git update-index --assume-unchanged wordpress/wp-content/uploads/*/*

Using git rm --cached is not good for collaboration. More details here: How to stop tracking and ignore changes to a file in Git?

Sheritasherj answered 3/10, 2017 at 15:44 Comment(1)
This is a very elegant solution! the recursive path trick saved my day. Thanks!Iced
N
9

From the git documentation:

Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore. This is particularly useful if you forgot to add something to your .gitignore file and accidentally staged it, like a large log file or a bunch of .a compiled files. To do this, use the --cached option:

$ git rm --cached readme.txt

So maybe don't include the "-r"?

Numbles answered 18/6, 2014 at 16:23 Comment(2)
The OP already tried --cached. The -r option is for "recursive", which should be included in this instance.Insubstantial
I found this useful. I was searching for how to do it on a folder, and then see the way to do it on a file. I'm up-marking this comment.Estrus
T
7

if file is committed and pushed to github then you should run

git rm --fileName

git ls-files to make sure that the file is removed or untracked

git commit -m "UntrackChanges"

git push

Trent answered 9/8, 2020 at 20:49 Comment(0)
S
1

I am also a new learner and here is my solution. It should be easy to solve the problem:

  1. In your terminal, make sure you are in the correct folder. For me, it should like: my-MacBook:~ myUserName$
  2. Input: git status
  3. It should give you a list of folders and files that git tracking now. Copy them.
  4. Input: code .gitignore
  5. Your default code editor will open this file, or you can find it and open manually.
  6. Paste the list of folders and files name from step 3. Make sure there is no tab or space at beginning of each line.
  7. Save and close this file (.gitignore)
  8. Go back to the terminal and input: git status
  9. If you see the untracked files only have: .gitignore, it means it works now.
  10. Input: git add .gitignore
  11. Input: git commit -m "what ever message you want to left here"
  12. Now you should be all set!
Spondaic answered 9/9, 2022 at 4:9 Comment(0)
Q
0

Remove the hidden .git directory for each folder which act as a git repository. Make sure you switched to the branch you want on each repository so that it's in the state you want, because you'll lose all the branches.

Quincentenary answered 12/3 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.