0

So the repo I'm working in is a selective checkout of a big repo. The server I'm working on has also seen better days. If I wanted to know what branch of a repo I was on, I'd normally do git branch -v, but last time I did that it took 3 minutes and 58 seconds.

Any way I can have just the name of the current branch a bit quicker than that? Surely it's stored in that .git folder somewhere?

Clumsy cat
  • 289
  • 1
  • 12
  • 1
    git status also displays the current branch. – ewokx Aug 30 '22 at 07:27
  • @RomainValeri so taking away verbosity (`git branch`) doesn't actually seem to speed things up, I'm still measuring almost 4 mins for that. `git rev-parse HEAD` is quick, but it gives me the commit hash not the branch. Is there a quick way to get a branch name from a commit hash? – Clumsy cat Aug 30 '22 at 07:35
  • @ewong this does actually appear to be much faster (10s), thanks! If you want to make an answer of that I would accept it. – Clumsy cat Aug 30 '22 at 07:37
  • `git rev-parse --symbolic-full-name HEAD` and `git rev-parse --abbrev-ref HEAD` should work and not be so slow, but `git branch -v` should also work and not be so slow - it might be interesting to find out what the heck is wrong with this repository and/or Git installation. – torek Aug 30 '22 at 08:06
  • @torek You are probably correct that there is something odd about the git install. I don't think it's the repo itself, on my own computer it takes much less time. Also takes less time if I use a virtual env with a different git. It may be related to the "git atlas" tools used on the server. https://atlassoftwaredocs.web.cern.ch/gittutorial/git-clone/ Unfortunately I do need them, and I don't have permission to tamper with the git install, but ewong's work around is great. – Clumsy cat Aug 30 '22 at 08:15

2 Answers2

1

Another option is to use git status, which does show which branch is the current branch.

ewokx
  • 2,204
  • 3
  • 14
  • 27
  • The question shows zero research, the solution doesn't adress the real underlying problem nor actually answers the question, and there are duplicates. Where to start... – Romain Valeri Aug 30 '22 at 07:44
  • The question asks how to find the current branch. The answer gives an option which shows the current branch which is what the OP wanted. The question didn't ask why the ```git branch -v``` takes eons to spit out the answer. Had the OP asked "Why is it taking so long?", then the answer would be not the right answer. That said, if there are duplicates, I apologize. – ewokx Aug 30 '22 at 07:47
  • No, you went for the pragmatic way, and it's probably a good thing also. I didn't want to sound harsh, sorry for this. – Romain Valeri Aug 30 '22 at 07:50
  • @RomainValeri No, no need to be sorry. You were correct in that I should've checked for duplicates. Thanks for the reminder. – ewokx Aug 30 '22 at 08:01
1

Check if git branch --show-current (since Git 2.22, Q3 2018) would be faster.

That way, you would not have to post-process git branch -v.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250