joeytwiddle
joeytwiddle

Reputation: 31305

Remove working files from git repository, to save space

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:

What is a quick and easy way to remove the working files? And is there a way to do it cleanly?

Upvotes: 2

Views: 409

Answers (2)

LeGEC
LeGEC

Reputation: 52226

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.

Upvotes: 1

joeytwiddle
joeytwiddle

Reputation: 31305

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!

Upvotes: 4

Related Questions