1

I use git worktree concept to create multiple working directory. One for each feature and/or bug fix. So I will create a git clone of a repository once, and then I start creating worktree as needed.

When I clone a git repository, by default a working directory is checked out - main worktree. Which we dont use and unnecessarily takes up space. So I am looking for a way to create a git repo with a working directory being checked out at the beginning.

I tried git clone --bare <url> ./test-git

which actually created a git repo without a working directory but I was not able to create a git worktree out of it. I use to throw the following error -

bash-4.4 [/user/darshan/test-git] $git worktree add -b test-git-worktree.repo ../test-git-worktree origin/repo
Preparing worktree (new branch 'test-git-worktree.repo')
fatal: Not a valid object name: 'origin/repo'.
  1. What am I doing wrong?

I went inside the .git directory created from git clone bare command and found that there was index file and lfs directory missing. And these two items will be present in the .git directory created from the full git clone.

  1. What do these two item indicate?
  2. On running what git command will I get these items?
Darshan L
  • 824
  • 8
  • 30
  • 1
    I am looking at the help from `git worktree` and this is what I see for `git worktree add`: `git worktree add [] []`. So you should have the path and the treeish.... but you have 3 params. I wonder what you mean with them. – eftshift0 Oct 21 '20 at 06:37
  • Also.... not that I am complaining about your workflow.... it's technically possible.... but why do you create a worktree per feature/bug? It sounds like trying to set up fireworks with an atomic bomb. That is _normally_ carried out using separate branches on a single worktree.... and switching between them. – eftshift0 Oct 21 '20 at 06:40
  • I personally use this kind of setup alot. But in my use case I just create a playground worktree elsewhere from a normal git repo, not from a bare git repo like OP. Reason is simply I like to go messy in that playground worktree, and if my boss pop-in to let me do a urgent bugfix, I don't have to first cleanup the playground then check out the master. I just open another folder, that's why. – hackape Oct 21 '20 at 06:48
  • @eftshift0 so that format of the command that I have used is - `git worktree add -b ` This format closely resembles the one in the git website - `$ git worktree add --track -b /` In fact the command that I am using works when I use it with a full git clone command. – Darshan L Oct 21 '20 at 06:59
  • @eftshift0 The reason I take this workflow is - we can continue working on multiple bug fixes and features simultaneously. Say, you have to run some test suite which takes hours to complete, or you trigger a build which again takes long time. So with worktree you can continue working on another issue meanwhile. This is the fundamental use case of worktree inception afaik. – Darshan L Oct 21 '20 at 07:02
  • @hackape At the moment even I am creating worktree from the full git clone (not from bare git repo). The thought that came to my mind is when I have created `n` worktrees for my bugs/features, there are `n+1` worktrees present in my storage (additional one is the main/default worktree). since our codebase is too huge, even an additional worktree cost a huge space. So was thinking of a way to eliminate it when I am not really using it. – Darshan L Oct 21 '20 at 07:05
  • I see your cwd is `/user/darshan/test-git`, is it currently a bare repo? – hackape Oct 21 '20 at 07:06
  • yes. it contains all the git administrative files - `branches, config, description, FETCH_HEAD, HEAD, objects, refs` etc – Darshan L Oct 21 '20 at 07:08
  • OK, I believe the problem is the last argument `origin/repo`, the `origin/` is excessive. Remove it you should be fine. – hackape Oct 21 '20 at 07:10
  • @hackape Do you mean to remove `origin/` in `git worktree` command? If you meant that - then the same command is working when I am using it with full git clone. – Darshan L Oct 21 '20 at 07:23
  • I just experimented and found my earlier explanation was utterly wrong. So just forget what I've said. But yes, I mean that removing `origin/` should work. – hackape Oct 21 '20 at 07:25
  • okay, I think using `--mirror` instead of `--bare` would work. Looking at the definition from the link - https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone I am going to try this out and update. – Darshan L Oct 21 '20 at 07:30
  • Yeah, I must've confused `--mirror` with `--bare`. – hackape Oct 21 '20 at 07:31
  • Just another idea FYI. If the `n+1` worktree disk usage is the only concern, you can also go with normal git clone, then in your local repo, you do `git checkout --orphan empty_branch`. A bit hacky but solves problem. – hackape Oct 21 '20 at 07:34
  • Okay, I am not sure how this command works. I will explore on it. Thanks. – Darshan L Oct 21 '20 at 07:41
  • The explanation is [here](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---bare). I quote "Also the branch heads at the remote are copied directly to corresponding local branch heads, without mapping them to `refs/remotes/origin/`". That's why `origin/` should be removed. – hackape Oct 21 '20 at 07:48
  • And the `--orphan` option explained [here](https://stackoverflow.com/questions/15034390/how-to-create-a-new-and-empty-root-branch) – hackape Oct 21 '20 at 07:52
  • An update - using `--mirror` essentially helped me partially achieve what I wanted. ie, I can create a worktree out of it and it doesnt have the default working tree being created. However, I see some side effects like - when I execute the command `git branch`, it executes all the list of remote branch and not the local one. So looks like it is a mirrored copy of the remote repo. I might have to still understand in depth on how it works. – Darshan L Oct 21 '20 at 08:06

1 Answers1

1

We can create a git repository without a working copy using -

git clone -n <url>

This just creates a directory (with repository name) with .git directory inside it.

Note: Furthermore, I hope using git clone --bare should also provide similar functionality. But have not been able to figure that out yet.

Darshan L
  • 824
  • 8
  • 30