问题
I have a number of GitHub projects on my disk, but many of them are not in active use.
I would like to remove the working files for the time being, because they are stored in the commit history anyway, so I can retrieve them at a later time.
The easiest way to remove the files is rm -rf *
but it has a number of drawbacks:
- It doesn't remove dotfiles such as
.gitignore
- If I come back later,
git status
complains about lots of removed files. I have to work out whether I should commit these removals, or if I had just removed them to temporarily save space.
What is a quick and easy way to remove the working files? And is there a way to do it cleanly?
回答1:
Creating an empty branch seems to work quite nicely:
- Check that all your files are safely committed into a branch (e.g. "master" or "develop" branch).
Create an empty branch. I will call it "empty".
git checkout --orphan empty
But that didn't remove the files. Do that manually:
git reset --hard
The branch doesn't really exist yet. Make an initial commit to confirm it:
git commit --allow-empty -m 'Empty commit'
Now all your files are gone, and you are on a branch that embodies that situation.
Good things about this process:
- It uses git's own mechanisms. There is no confusing dirty status.
- It is clear what is going on when you see the branch name.
- You can easily switch back to master with
git checkout master
- You can easily switch back to your empty branch again with
git checkout empty
Disadvantages:
- The initial process requires 3 commands.
- If you are not familiar with branches, or seeing which branch you are on, when you return to the folder you might wonder where all your files have gone!
回答2:
If you use git 2.5.1 or higher, you can use bare repositories, and use git worktree add /some/checkout/path branch
if you want to inspect or use its content.
The worktree will work exactly as a standard git clone, expect that all modifications you run there (commits, new branches, tags, ...) are applied to the bare clone.
来源:https://stackoverflow.com/questions/35355810/remove-working-files-from-git-repository-to-save-space