0

When I run .git/refs/heads, it shows only the branches that I have checked out locally. How can I create refs/heads to all my branches? Or how can I create refs/heads to all the origin/?

I am working with Azure DevOps. I am using their predefined variables that will ONLY return the branch ref of the source/target of the Pull request. However, those variables will return, for example, refs/heads/feature. So after I am getting the refs, I am using git rev-list $branch-ref in one of my devops tasks. But it giving me the ambiguous argument 'refs/heads/feature': unknown revision ERROR. I think that is happening because the git checkout that is happening does not have those references. However, when I use origin/feature it works fine. So I am trying to find a way that I turn all origin/ into refs/heads/

MSOUFAN
  • 101
  • 2
  • 12
  • You shouldn't need to make local branches for all of origin. You can reference `origin/branch_name` if you just need to read the branch. What are you trying to do? – Schwern Jul 02 '20 at 00:43
  • Any update for this issue? Have you resolved this issue? If not, would you please let me know the latest information about this issue? – Leo Liu Jul 06 '20 at 05:59

2 Answers2

1

Inside the .git/refs folder, you find:

  • heads folder, containing all the refs of local branches
  • remotes folder, containing all the remotes (ex. origin)
  • remotes/origin folder, containing all the refs of the origin remote branches
  • tags folder, containing all the refs of tags

In case the repository has been packed, which may occur automatically, you'll find the references into the file .git/packed-refs.

Depending on your needs, the correct way of extracting the references is through git itself.
Accessing the raw .git files should be avoided.

Yusef Maali
  • 2,201
  • 2
  • 23
  • 29
  • 1
    They do not contain all the references. Some will be in [packfiles](https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery). – Schwern Jul 02 '20 at 00:48
  • As soon as the repo won't be packed, they do contain all the references. Anyway I agree with you that accessing the raw .git files should be avoided – Yusef Maali Jul 02 '20 at 01:02
  • I have edited the question to make it clearer. I appreciate your answer, but I think now you will have a better idea on what I want. – MSOUFAN Jul 02 '20 at 14:15
1

.git/refs/heads will not contain a list of all your local branches. Some will be in .git/packed-refs.

To see all your local branches, use git branch.

How can I create refs/heads to all my branches? Or how can I create refs/heads to all the origin/?

You shouldn't make a local branch unless you intend to make modifications. You can instead refer to the remote tracking branch. origin/foo refers to the branch foo on origin.

If you really want to, write a little script using git branch -r to get a list of all your remote branches. Here's an example in Ruby.

remotes = `git branch -r`.split("\n").map(&:strip)
locals = remotes.map { |r| r.sub(%r{^.?+/}, ''); }
remotes.zip(locals) { |r,l| system("git branch #{l} #{r}") }
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • I have edited the question to make it clearer. I appreciate your answer, but I think now you will have a better idea on what I want. – MSOUFAN Jul 02 '20 at 14:15
  • @MSOUFAN If you want the source and target of a PR, that will be your remote tracking branches. If it says refs/heads/feature that is the remote's feature branch which is your origin/feature, not your local feature. Again, there's no need to make a local branch unless you plan on making commits you're going to keep. – Schwern Jul 02 '20 at 16:15