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
Share:
108,185
Rodrigo Souza
Author by

Rodrigo Souza

Calm down. It's only ones and zeros

Updated on August 05, 2022

Comments

  • Rodrigo Souza
    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
    grossvogel over 13 years
    And then add the path to .gitignore so git doesn't try to make you add it later.
  • bstpierre
    bstpierre over 13 years
    Will this result in (files in) the directory being removed when he pulls from the remote?
  • Adam Smith
    Adam Smith over 13 years
    Not 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
    ioleo over 9 years
    this is not a good solution - what if you have a very large directory?
  • Bobby Jack
    Bobby Jack over 9 years
    What 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
    theUtherSide over 8 years
    probably need to use git rm -r
  • Борис Чиликин
    Борис Чиликин over 8 years
    For single files, you can do git rm --cached path/to/file
  • Jantar88
    Jantar88 about 7 years
    @BobbyJack, files will be removed from the third remote upon git pull there. (It worked this way for me.)
  • 648trindade
    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