Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current branch name for use in git command

Tags:

I sometimes want to use the current branch name to use in git commands. For example

git push origin feature/really-long-branch-name 

Is there a git command that will give just the branch name so I can do something like the following?

git push origin current_branch 

There is git rev-parse --abbrev-ref HEAD but that's exactly useful in this case. Setting a default branch isn't that helpful either since the branch name changes often. Changing the default behavior of git push isn't what I'm looking for either since it still means having to type in the full branch name the first time I push.

Edit:

Moderators, this question is not a dupe so please do not close for that reason. Please read the bolded part of my question carefully.

like image 875
Dty Avatar asked Oct 12 '13 05:10

Dty


People also ask

How do I find my current branch branch in github?

You can use git branch --contains to list all the branches descended from the tip of develop , then use grep to make sure feature is among them. If it is among them, it will print " feature" to standard output and have a return code of 0.

How do you get specific branch from remote?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

Which command rename current branch branch?

The git branch command lets you rename a branch. To rename a branch, run git branch -m <old> <new>. “old” is the name of the branch you want to rename and “new” is the new name for the branch.


1 Answers

Since June 2019, Use Built-In --show-current Flag

While this answer from 2013 has stood the test of time, Git learned a new git-branch flag back in 2019 that makes this much easier. In commit 3710f60a80, git-branch learned a new flag for showing the current branch without requiring users to parse the list of branches or refs themselves. You can invoke it like so:

$ git branch --show-current main 

The other methods below continue to work, but this should now be the go-to solution for Git release versions >= v2.22.0.

Read the Symbolic Ref of HEAD

There are a number of ways to get the name of the current branch. The most canonical is to read the symbolic ref for HEAD using git-symbolic-ref(1). For example, assuming you are on the master branch:

$ git symbolic-ref HEAD | sed 's!refs\/heads\/!!' master 

Using the Ref

However you parse it, you can use the symbolic name in another command by invoking your shell's command substitution. For example, in Bash:

$ git log -n1 $(git rev-parse --abbrev-ref HEAD) 

There's no reason you couldn't use this trick with push or other commands, if you choose.

Push.Default

If you're only interested in pushing the current branch to a remote branch with the same name, and aren't parsing the refs for some other reason, then you'd be better off using Git's push.default option described here and here. For example:

git config push.default current 
like image 92
Todd A. Jacobs Avatar answered Sep 22 '22 13:09

Todd A. Jacobs