How can I delete files in the repository while keeping (and ignoring) them locally?
I erroneously adde开发者_Go百科d 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?
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 andgit 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
orgit rebase
), - push --force your branch,
(but then, be prepared to point out people to the RECOVERING FROM UPSTREAM REBASE section of thegit rebase
man page).
(see definition of upstream here)
- Create a new branch which has the commit with the files.
- Reset the original branch back to where it used to be.
- 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).
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.
精彩评论