-
-
Save lucasdinonolte/4016361 to your computer and use it in GitHub Desktop.
git branch | grep "*" | awk '{ print $2 }' | pbcopy |
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.
@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
@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
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.
I used this variant:
alias gitb="git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' | pbcopy"
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.
As of Git 2.22 just:
git branch --show-current | pbcopy
Looks like that last one still needs the newline removed
git branch --show-current | tr -d '\n' | pbcopy
@smithtimmytim that one removed any "n" chars from my copied branch name.
@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 😅 )
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
I found the below worked better for an alias: