Skip to content

Instantly share code, notes, and snippets.

@lucasdinonolte
Created November 5, 2012 09:45
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucasdinonolte/4016361 to your computer and use it in GitHub Desktop.
Save lucasdinonolte/4016361 to your computer and use it in GitHub Desktop.
Copy current git branch to clipboard
git branch | grep "*" | awk '{ print $2 }' | pbcopy
@charlieegan3
Copy link

I found the below worked better for an alias:

alias gitb="git branch | grep '^\*' | cut -d' ' -f2 | pbcopy"

@azizbekian
Copy link

For those, who don't know how to make this work: create a file, e.g. git_branch_name_to_clipboard.sh in your user directory with following content:

git branch | grep "*" | awk '{ print $2 }' | pbcopy

Then in .gitconfig file, which is located in your user folder, add these lines under [alias]:

cp = "!sh $HOME/git_branch_name_to_clipboard.sh"

Now performing git cp will copy your current branch name to clipboard.

@fengshuo
Copy link

fengshuo commented Mar 15, 2017

@lnolte @azizbekian thanks for the tip, I found it very helpful to paste the branch name when I am editing commit messages.
Although there is a new line character at the end of the copied content, I can change it to not move to a new line on Terminal, but when I am using IntelliJ's embed terminal, it doesn't have an option to not move to a new line.
So here is a modified version of the sh file without the new line character in case someone else want to use it:
git branch | grep "*" | awk '{ print $2 }' | tr -d '\n' | pbcopy

@anthonyLukes
Copy link

@charlieegan3's version works better for me as well, but I also prefer copying without the new line character. I applied @fengshuo's changes and came up with this:
git branch | grep '^\*' | cut -d' ' -f2 | tr -d '\n' | pbcopy

@martisj
Copy link

martisj commented Nov 1, 2018

I reckon you could use this as well (ref: https://stackoverflow.com/a/12142066/433509)
git rev-parse --abbrev-ref HEAD | tr -d '\n' | pbcopy

Tip: remove tr -d '\n' to keep the newline at the end.

@yan-kisen
Copy link

I used this variant:

alias gitb="git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' | pbcopy"

@GeorgeWL
Copy link

GeorgeWL commented Nov 4, 2019

just for anyone using bash for Windows, here's their equivalent command:
git branch | grep '^\*' | cut -d' ' -f2 | tr -d '\n' | clip
Bash for windows has some but not all of the Linux commands installed, and pbcopy is one of those which it doesn't have.

@Pezmc
Copy link

Pezmc commented Oct 21, 2020

As of Git 2.22 just:

git branch --show-current | pbcopy

@xavibenjamin
Copy link

Looks like that last one still needs the newline removed

git branch --show-current | tr -d '\n' | pbcopy

@MSPigl
Copy link

MSPigl commented Jun 9, 2021

@smithtimmytim that one removed any "n" chars from my copied branch name.

@Hoijof
Copy link

Hoijof commented Oct 22, 2021

@smithtimmytim that one removed any "n" chars from my copied branch name.

@MSPigl You probably did tr -d 'n' instead of tr -d '\n'. (Probably he updated his comment fixing that 😅 )

@JoA-MoS
Copy link

JoA-MoS commented May 4, 2022

for those who end up here after today, there is a command built into git to show the current branch

git branch --show-current

Add these get aliases to your .gitconfig and you have the same functionality

[alias]
	scb = branch --show-current
        ccb = ! git scb | tr -d '\n' | pbcopy

git scb - shows (prints) current branch
git ccb - copies the current branch to clipboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment