git分支可以用数字列出吗?
我想知道是否有人构建了一个脚本,或者有没有办法用数字列出git分支,这样就可以代替它(最好是在bash中) 我可以得到这样的列表(或类似列表) 那么签出一个分支就很容易了git分支可以用数字列出吗?,git,branch,Git,Branch,我想知道是否有人构建了一个脚本,或者有没有办法用数字列出git分支,这样就可以代替它(最好是在bash中) 我可以得到这样的列表(或类似列表) 那么签出一个分支就很容易了 git checkout #4 这听起来很容易创建。简单的实现可以使用两个单行脚本/别名 吉特lbr(listbranch): git coid(checkoutid) 但这是一个简单的实现,对于缺少输入/非数字输入/超出范围的分支ID的情况没有错误处理 使用示例: $ git lbr 1 branch_42
git checkout #4
这听起来很容易创建。简单的实现可以使用两个单行脚本/别名 吉特lbr(listbranch): git coid(checkoutid) 但这是一个简单的实现,对于缺少输入/非数字输入/超出范围的分支ID的情况没有错误处理 使用示例:
$ git lbr
1 branch_42
2 feature/super-feature
3 foo/bar
4 * master
$ git coid 1
Switched to branch 'branch_42'
Your branch is up-to-date with 'master'.
上面的答案不适用于我,并给出了一个语法错误:/。也许不同操作系统(我使用mac)的外壳之间存在差异。下面是我在.bashrc文件中添加的内容
# List branches by id
alias gbls='git branch --no-color | cat -n'
# Checkout branch by id
function gcb() {
local index=$1
local branches=$(git branch | grep "[^* ]+" -Eo)
local counter=0
echo "$branches" | while IFS= read -r branch ; do
counter=$((counter+1))
if [ "$index" -eq "$counter" ]; then
git checkout $branch
fi
done
}
工作原理如下
对于Windows上的Git Bash,我在用户目录中编辑了.gitconfig文件,以包含以下部分:
[alias]
lbn = !git branch | cat -n
cobn = "!f() { branch_name=$(git branch --format='%(refname:short)' | head -n $1 | tail -n 1); git checkout $branch_name; }; f"
这可能不是最复杂的方法,但对我来说很有效:
$ git lbn
1 bugfix/1234_CrashInAboutScreen
2 feature/feature1
3 feature/feature2
4 * master
$ git cobn 2
Switched to branch 'feature/feature1'
就像,我的也是一个没有输入验证或错误处理的简单实现。刚才我突然想到了这个想法。
$ git lbr
1 branch_42
2 feature/super-feature
3 foo/bar
4 * master
$ git coid 1
Switched to branch 'branch_42'
Your branch is up-to-date with 'master'.
# List branches by id
alias gbls='git branch --no-color | cat -n'
# Checkout branch by id
function gcb() {
local index=$1
local branches=$(git branch | grep "[^* ]+" -Eo)
local counter=0
echo "$branches" | while IFS= read -r branch ; do
counter=$((counter+1))
if [ "$index" -eq "$counter" ]; then
git checkout $branch
fi
done
}
[alias]
lbn = !git branch | cat -n
cobn = "!f() { branch_name=$(git branch --format='%(refname:short)' | head -n $1 | tail -n 1); git checkout $branch_name; }; f"
$ git lbn
1 bugfix/1234_CrashInAboutScreen
2 feature/feature1
3 feature/feature2
4 * master
$ git cobn 2
Switched to branch 'feature/feature1'