2

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?

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110

2 Answers2

4

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!
joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
  • You can also [convert your repo to a bare repository](http://stackoverflow.com/questions/7380741/how-to-remove-working-tree-from-a-git-repository) but then it won't be so trivial to checkout the files and get working as you were before. – joeytwiddle Feb 12 '16 at 06:01
  • There are a number of [other tips to reduce disk space here](http://stackoverflow.com/questions/1398919/make-git-consume-less-disk-space), including [creating a shallow clone with no history](http://stackoverflow.com/questions/1398919/make-git-consume-less-disk-space#35356887). – joeytwiddle Feb 12 '16 at 07:22
1

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.

LeGEC
  • 46,477
  • 5
  • 57
  • 104