Remove working files from git repository, to save space

一个人想着一个人 提交于 2019-12-13 07:03:59

问题


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:

  1. Check that all your files are safely committed into a branch (e.g. "master" or "develop" branch).
  2. Create an empty branch. I will call it "empty".

    git checkout --orphan empty
    
  3. But that didn't remove the files. Do that manually:

    git reset --hard
    
  4. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!