Quantcast
Channel: How to get default Git branch? - Stack Overflow
Browsing latest articles
Browse All 20 View Live

Answer by jameshfisher for How to get default Git branch?

> git var GIT_DEFAULT_BRANCHmasterFrom the docs for git var:GIT_DEFAULT_BRANCH: The name of the first branch created in newly initialized repositories.This variable was added in git 2.35.

View Article



Answer by mfandrade for How to get default Git branch?

From git 2.28.0 and above:$ git config --get init.defaultBranch || echo master

View Article

Answer by Kankaristo for How to get default Git branch?

I'll add yet another answer, based on the other answers. I didn't like any of the other answers because querying a remote is slow, but I also didn't want a "local-only" solution.I use this (long)...

View Article

Answer by Amory for How to get default Git branch?

As noted by other answerers, the concept of a default branch is a GitHub thing doesn't really make sense (in the lofty sense) of Git (there's a reasonably good (if under-appreciated) comment here gets...

View Article

Answer by pfesenmeier for How to get default Git branch?

This is possible to get with the gh cli tool (tested v2.0.0)gh repo view --json defaultBranchRef --jq .defaultBranchRef.name

View Article


Answer by Henry for How to get default Git branch?

RequirementsYou need refs/remotes/origin/HEAD and it is only set when cloning. Otherwise you need to set it yourself withgit remote set-head origin -aMore infoCommandThe following command will list the...

View Article

Answer by Makusu2 for How to get default Git branch?

All other answers made too many assumptions, this is the only way that worked for me. This works regardless of what branch you're currently on, doesn't assume the origin/HEAD ref exists locally, and...

View Article

Answer by Henrik N for How to get default Git branch?

I just wanted a shell script to know whether the branch is "master" or "main".For that purpose, this seems good enough:[ -f "$(git rev-parse --show-toplevel)/.git/refs/heads/master" ] && echo...

View Article


Answer by Carl Walsh for How to get default Git branch?

If like this question you are trying to get a GitHub default branch--not some other git server:You can get the default branch using the /repos GitHub API. It's the default_branch field of the...

View Article


Answer by Jeffrey Yasskin for How to get default Git branch?

git rev-parse --abbrev-ref origin/HEAD will print origin/<default-branch-name>. The git symbolic-ref answers are doing the same thing but need a longer argument.If the origin repository changes...

View Article

Answer by JoeLinux for How to get default Git branch?

This question is a bit old but in case anyone comes across this more recently...git remote show <remote_name> | awk '/HEAD branch/ {print $NF}'That will also only display the branch name, not...

View Article

Answer by anthony sottile for How to get default Git branch?

There doesn't seem to be an answer that doesn't require cloning so farThis requires git 2.8.0 or newer$ git ls-remote --symref git@github.com:pre-commit/pre-commit.github.io HEADref:...

View Article

Answer by Radon8472 for How to get default Git branch?

I found a way to detect the default-branch if it is not master.git remote show [your_remote] | sed -n '/HEAD branch/s/.*: //p'I tested it with multiple repo from gitlab, and it worked fine.(for the...

View Article


Answer by Christian Hesse for How to get default Git branch?

There is a --short option to git symbolic-ref. So my preferred command:$ basename $(git symbolic-ref --short refs/remotes/origin/HEAD) master

View Article

Answer by danielkza for How to get default Git branch?

Tested with git 2.9.4 (but possibly works in other versions) in a repo cloned from Github:git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'Sample output:masterOr:git...

View Article


Answer by anorm for How to get default Git branch?

Seems like a bit of a workaround solution but this seems to work:$ cat .git/refs/remotes/origin/HEAD ref: refs/remotes/origin/master

View Article

Answer by Tony Spataro for How to get default Git branch?

This works for me with Git 2.1.10, using a repository cloned from GitHub:git branch -r --points-at refs/remotes/origin/HEADA major problem with this approach is that it lists every remote branch...

View Article


Answer by VonC for How to get default Git branch?

Is there a git command available to determine default branch for remote repository?This list the default local repository branch:git rev-parse --abbrev-ref origin/HEADThe GitHub API can show the...

View Article

How to get default Git branch?

My team alternates between usage of dev and master as default branch for several repos and I would like to write a script that checks for the default branch when entering a directory.When pull requests...

View Article

Answer by bric3 for How to get default Git branch?

Adding some context about remote names to the excellent answers of danielkza or Jerrey Yasskin.So the example show it's possible to get the default branch of the remote named origin.$ git rev-parse...

View Article
Browsing latest articles
Browse All 20 View Live


Latest Images