Remove file from the repository but keep it locally
108,185
Solution 1
git rm --cached -r somedir
Will stage the deletion of the directory, but doesn't touch anything on disk. This works also for a file, like:
git rm --cached somefile.ext
Afterwards you may want to add somedir/
or somefile.ext
to your .gitignore
file so that git doesn't try to add it back.
Solution 2
I would just:
- Move the folder out of your working tree
git rm
the folder, commit the change- Add to
.gitignore
(or.git/info/excludes
), commit the change - Move the folder back
Comments
-
Rodrigo Souza over 1 year
I have a folder which I'd like to remove in my remote repository. I'd like to delete it, but keep the folder in my computer
-
grossvogel over 13 yearsAnd then add the path to .gitignore so git doesn't try to make you add it later.
-
bstpierre over 13 yearsWill this result in (files in) the directory being removed when he pulls from the remote?
-
Adam Smith over 13 yearsNot when he pulls; the files will stay removed locally during the pull's automatic merge process. After that, a push will cause the files to be removed server-side.
-
ioleo over 9 yearsthis is not a good solution - what if you have a very large directory?
-
Bobby Jack over 9 yearsWhat happens if I have a third remote? Will the corresponding files get removed in a future pull? I ask because I feel this is quite a common use case, i.e. check some files into the repo, realise at a later date that they diverge between remotes, for good reason, and should never have been in the repo in the first place, want to resolve that by keeping all local checkouts exactly as they are, but removing the files from the repo.
-
theUtherSide over 8 yearsprobably need to use
git rm -r
-
Борис Чиликин over 8 yearsFor single files, you can do
git rm --cached path/to/file
-
Jantar88 about 7 years@BobbyJack, files will be removed from the third remote upon
git pull
there. (It worked this way for me.) -
648trindade about 6 years@loostro just don't move it to another block device. The most file systems will just update the file's inode, and not literally move the entire directory