Stream-saving version:npx bcy
foreword
In work and collaboration scenarios, it is unavoidable to tell colleagues about your current development branch, which usually requires 2 steps
Step1 View the current branch
git branch
There is another case here. If there are many branches, you need to turn the page to find the current branch.
To get it accurately, you need to add --show-current
parameters
git branch --show-current
Step2 right mouse button to copy the current branch
This article will introduce 2 ways to achieve 1 line of code to directly copy the branch
- Shell
- Node CLI
core steps
get branch name
This is introduced above, and can be git branch --show-current
obtained by
In shell
, you can directly store the result in a variable
# bcp.sh
branch=$(git branch --show-current)
echo $branch
Node.js
Here, child_process
commands can be executed through the module
const { execSync } = require('child_process');
const branch = execSync('git branch --show-current').toString().trim();
console.log(branch);
copy to clipboard
This part is to call the system instruction to execute, different operating systems are different
I have used the node-copy-paste library before
Briefly introduce the commands used by each operating system: MacOS (pbcopy), Windows (clip), Linux (xclip)
Here the author uses is MacOS
, so directly use pbcopy
the command
| pbcopy
Just add on top of the above
branch=$(git branch --show-current)
echo $branch | pbcopy
# 或
git branch --show-current | pbcopy
After running, it is found that git branch --show-current
the obtained content will have a newline character, which can be tr
removed through the command
git branch --show-current | tr -d '\n' | pbcopy
In Node.js, just .trim()
add after.replace(/\n/g, '')
execSync('git branch --show-current').toString().trim().replace(/\n/g, '');
But in reality, we need to encapsulate such long instructions for easy use
Anyone who has nothing to knock so much can manually CV many times
package implementation
Shell
Just use alias
the command
alias bcy='git branch --show-current | tr -d "\n" | pbcopy'
Then add this command to ~/.zshrc
or ~/.bashrc
(you can use echo $0
the default shell executor of your own terminal)
You can run the following shell script to complete the automatic addition
echo 'alias bcy="git branch --show-current | tr -d \"\\n\" | pbcopy"' >> ~/.zshrc
重启终端,或者使用source ~/.zshrc
使其生效
source ~/.zshrc
当然我们也可以打印一些提示信息
alias bcy='branch=$(git branch --show-current); echo "当前分支:$branch"; echo $branch | tr -d "\n" | pbcopy'
对应的安装脚本如下
echo "alias bcy='branch=\$(git branch --show-current); echo \"当前分支:\$branch\"; echo \$branch | tr -d \"\\\\n\" | pbcopy'" >> ~/.zshrc
Node CLI
上面介绍的是shell
里的实现,这里介绍下Node.js
里的实现
方便有 Node 环境,但不熟悉Shell的同学使用
脚本也很简单
#!/usr/bin/env node
const { execSync } = require('child_process')
const ncp = require('copy-paste')
// 获取当前仓库分支
const branch = execSync('git branch --show-current')
.toString()
.trim()
.replace(/\n/g, '')
console.log('当前分支:', branch)
ncp.copy(branch)
这个CLI通过npm包发布了,可以直接npx bcy
使用
或者 npm i bcy -g
全局安装
包名实在是难取,简单语义化一点的都被占用了,让GPT 辅助了一下
最后用了搜了一圈不重复的只有bcy
总结
本文简单介绍了如何通过Shell
和Node.js
实现复制当前分支名到剪贴板
如果你有更好的实现方式,欢迎留言讨论
源码地址:bcy