Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete files in the repository while keeping (and ignoring) them locally?

Tags:

git

I erroneously added some local project files to a git repository and committed/pushed them.

I'd like to delete these files from the remote repository, keep them locally, and ignore them for future commits/pushes.

What's the best way to go about this?

like image 488
Wells Avatar asked Jul 01 '10 18:07

Wells


3 Answers

The cleanest solution is the following:

  • git rm --cached the extra files locally (note the --cached option to keep those files in your working directory),
  • add them to your .gitignore file and git commit -A -m "..." after that,
  • push your branch (no history rewritten, but previous history will keep references to those files).

If you think not too many people have pulled from your remote repo (ideally, none), you could:

  • fix your commit history locally
    (git rebase --interactive first-commit-with-files^: the '^' referencing the parent commit of the first one where you did introduce the bad files.
    git rm --cached the files, then replay the other commmits unless some of them have also made modifications to the same files.
    Other solutions here -- git filter-branch or git rebase),
  • push --force your branch,
    (but then, be prepared to point out people to the RECOVERING FROM UPSTREAM REBASE section of the git rebase man page).
    (see definition of upstream here)
like image 110
VonC Avatar answered Sep 18 '22 06:09

VonC


  1. Create a new branch which has the commit with the files.
  2. Reset the original branch back to where it used to be.
  3. Do a push -f to forcefully revert the remote backwards (WARNING: This will "break" repositories which have those commits already pulled - its undoable, but manual drudgery).
like image 39
Yann Ramin Avatar answered Sep 18 '22 06:09

Yann Ramin


Well add them to .gitignore (line separated filenames in the main directory of the repo and then follow this guide: http://help.github.com/removing-sensitive-data/. Finally git push -f to forcibly overwrite the remote repo.

like image 27
fbstj Avatar answered Sep 19 '22 06:09

fbstj