5

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?

1
  • We do already have a question about ignoring on SO, don't we? O_o
    – P Shved
    Commented Jul 1, 2010 at 18:53

3 Answers 3

5

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)
1
  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).
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.