Using git log on a bare git repository with remote name in revision

84 Views Asked by At

I've been experimenting with git worktree and found some weird issue. Maybe it has to do cloning the repository as --bare.

$ cd ~
$ git clone --bare https://github.com/actions/checkout.git

The checkout repository has a remote called origin:

$ git remote -v
origin  https://github.com/actions/checkout.git (fetch)
origin  https://github.com/actions/checkout.git (push)

In upstream projects I usually have multiple remotes and I like to look at the logs of each remote by doing git log origin/main for example.

Let's do that in the bare repository:

$ cd ~/checkout.git
$ git log origin/main
fatal: ambiguous argument 'origin/main': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Why is that?

Even when I create a worktree for a hotfix I get the same error:

$ cd ~/checkout.git
$ git worktree add -b hotfix ~/checkout.git/hotfix main
$ cd ~/checkout.git/hotfix
$ git log origin/main
fatal: ambiguous argument 'origin/main': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Adding the -- separator from the help also doesn't help:

$ git log origin/main --
fatal: bad revision 'origin/main'
2

There are 2 best solutions below

0
Konrad Kleine On

Thank you @matt for you comments. They were helpful.

I don't know if this is the way one is supposed to be using worktrees but if I want to create a worktree for a hotfix based on origin/main and use the really remote branch notation origin/main I can do it like this.

$ git clone https://github.com/actions/checkout.git
$ cd checkout
$ git worktree add -b hotfix ../checkout-hotfix origin/main
$ git worktree list 
$ git worktree list
/home/kwk/checkout         72f2cec [main]
/home/kwk/checkout-hotfix  72f2cec [hotfix]

This is solving my problem.

0
Guildenstern On

You can't do this (at least not in any obvious way):

  1. Clone a bare repository
  2. Use multiple remotes

Because (as matt explained) a bare repository doesn't have remotes. So there isn't a way to reference different remote-tracking branches.

(As a side-note though you can have (1). Using worktrees with a bare repository works fine. But you can't both of these.)

You don't explain why you want a bare repository. I'll just assume that you don't want to waste disk space on using a worktree in a directory which contains the repository when you won't be using it.

First of all you will need a non-bare repository. But the working for that repository (for the main worktree) doesn't need to take up any space. You can do that by creating a branch which has an empty tree:

git clone <name>
cd <name>
# Use a branch name that you won't need
git switch --orphan unborn-in-the-usa
git clean --force -xd
git commit --allow-empty --message=Init
# Add more remotes
git remote add <name2>
git remote add <name3>
# Start making worktrees
git worktree add -b hotfix ~/checkout.git/hotfix main

This setup will only use a tiny amount of more disk space compare to a bare repository since the main worktree will have an empty worktree.