2

I've just finished a git clone here on a new computer, and I now have the repo in an interesting state:

There is no branch checked out. The working directory is empty, but the repo is all here, compressed in the .git metadata.

Of course, the missing step here is to do a git checkout to get the working copy onto a branch, but this state got me thinking. This is a fairly large repo, one which I don't expect to need access to very frequently either, so I'm now wondering, is there a way to return to this 'clean' state, where no branches are checked out, and the files exist only as metadata?

It would go a long way in saving up some disk space.

I thought about deleting all local files once I'm done, but that would then show up as uncommitted removals on that branch. I'd like to find out if there is a way to simply 'clear out' the working directory, disconnecting it from all branches on the repo, and leaving only the metadata in the repo directory.

One possible solution here might be to create a branch where all files are deleted, but that doesn't sound very elegant. Maybe a more direct approach exists?

As always, thanks in advance!

Cheers

yano
  • 4,095
  • 3
  • 35
  • 68
HarvesteR
  • 175
  • 1
  • 8
  • Possible duplicate of [How do I clear my local working directory in git?](http://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git) – msc Jul 20 '16 at 04:39
  • Not quite. I read that one, and it seems to be about clearing out untracked files from the working copy. My question here was about wiping the entire working copy and leaving only the metadata on disk. – HarvesteR Jul 21 '16 at 16:01

2 Answers2

3

Well, I believe I've found my own answer here. It is possible!

Git has the concept of 'orphan' branches. This is a branch that has no history relationship to any other commits on the repo history. This allows us to check out to 'no commit' by doing:

git checkout --orphan none

and then

git rm -rf .

That effectively gets rid of all files, and because the 'none' branch has no ancestry, the deletions don't appear as removals. The metadata folder stays put, so to return to a valid branch, you just do another checkout.

More info here: https://coderwall.com/p/0n3soa/create-a-disconnected-git-branch

Gotta love git!

Cheers

HarvesteR
  • 175
  • 1
  • 8
  • 1
    Yes. A shorter / faster, albeit trickier, way is to make a commit that has the empty tree as its `tree` object, then make a name (branch or tag name) that points to this empty commit. See http://stackoverflow.com/q/9765453/1256452 and look at `git commit-tree` and `git update-ref`. – torek Jul 20 '16 at 03:40
-2

I've just finished a git clone here on a new computer, and I now have the repo in an interesting state: There is no branch checked out. The working directory is empty, but the repo is all here, compressed in the .git metadata.

By default, git clone will check out master branch for you. If the master branch is empty(usually, I like to create a more meaningful branch name to start, not the default one, i.e. master), you will not see any file in working directory.

git checkout --orphan none

and then

git rm -rf .

I would like to recommend you not do this way. git rm will mark files as to be be delete. It will destroy your working directory.

Community
  • 1
  • 1
gzh
  • 3,507
  • 2
  • 19
  • 23