简体   繁体   English

git将所有分支bash脚本克隆为git别名

[英]git clone all branches bash script as git alias

I would like to create a git alias that clones all branches . 我想创建一个git别名克隆所有分支

We have the bash script, thanks to this post: How to clone all remote branches in Git? 多亏了这篇文章,我们有了bash脚本: 如何在Git中克隆所有远程分支?

Here is the bash script (multi-line version): 这是bash脚本(多行版本):

    #!/bin/bash
    for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
        git branch --track "${branch##*/}" "$branch"
    done

(one line version): (单行版本):

    git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs

Let's call the git alias git cloneallbranches 我们称git别名git cloneallbranches

I have tried setting both one-line and multi-line versions using: 我尝试使用以下方法设置单行和多行版本:

$ git config --global alias.cloneallbranches '...'

and tried to paste both versions into my .gitconfig file unsuccessfully (I have other git aliases, but none are bash scripts). 并尝试将两个版本都成功粘贴到我的.gitconfig文件中(我还有其他git别名,但都不是bash脚本)。

Can someone help me alter a bash script, so that I can paste it into my .gitconfig file, to make the git alias works properly? 有人可以帮我修改bash脚本,以便将其粘贴到.gitconfig文件中,以使git别名正常工作吗?

Thank you. 谢谢。


ANSWER: 回答:

Running a separate bash script as a "git alias" answer in reply below. 在下面的答复中,运行单独的bash脚本作为“ git别名”答案。

However, for those who want a quick way to add a git alias, $ git clone-all-branches here is an answer: 但是,对于那些想要快速添加git别名的人,这里的$ git clone-all-branches是一个答案:

Create a "git alias" which will run a script: 创建一个“ git alias”,它将运行一个脚本:

$ git config --global alias.clone-all-branches '! git branch -a | sed -n "/\\/HEAD /d; /\\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'

Now you can run (from any directory that has a git repo): 现在您可以运行(从具有git repo的任何目录中运行):

$ git clone-all-branches

Thanks to @g-sliepen suggestion, I did the following: 感谢@ g-sliepen的建议,我做了以下工作:

For Ubuntu: 对于Ubuntu:

#1 Go to home directory: #1转到主目录:

`$ cd ~`

Note: Should be same directory as .gitconfig and .bashrc files, which you will need in a shortly. 注意:应该与.gitconfig和.bashrc文件位于同一目录,稍后您将需要它们。

#2 Create a new file, a bash script, that we will execute later (make sure you are in the home directory): #2创建一个新文件,一个bash脚本,稍后我们将执行它(确保您位于主目录中):

`$ touch .gitcloneallbranches.sh`

Open the file: 打开文件:

`$ subl .gitcloneallbranches.sh`

Paste and save the following inside your file: 将以下内容粘贴并保存到文件中:

# Bash script that we will execute as an "alias" either as a "git alias" or a "bash alias"
#!/bin/bash
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs`

#3 Give that file the permissions it needs to be executed: #3授予该文件执行所需的权限:

`$ chmod u+x .gitcloneallbranches.sh`

Use script as #4 a "git alias" -OR- as #5 a "bash alias" 将脚本用作#4的“ git别名” -OR-用作#5的“ bash别名”

#4 Create a "git alias" which will allow you to run this script as a git command: #4创建一个“ git别名”,使您可以将此脚本作为git命令运行:

  • [Skip to #5 if you prefer a bash alias] [如果您喜欢bash别名,请跳至#5]

PICK ONE of A -OR- B ways to create the git alias (the "bash alias" is #5): 选择一种 -或 -B创建git别名的方式 (“ bash别名”为#5):

(A: via terminal) (A:通过终端)

$ git config --global alias.clone-all-branches '!sh ~/.gitcloneallbranches.sh'

(OR, B: or, add alias directly in .gitconfig file:) (或,B:或直接在.gitconfig文件中添加别名:)

In home directory, open .gitconfig: 在主目录中,打开.gitconfig:

$ subl .gitconfig

Under the [alias] section, paste the following: [alias] clone-all-branches = !sh ~/.gitcloneallbranches.sh 在[alias]部分下,粘贴以下内容: [alias] clone-all-branches = !sh ~/.gitcloneallbranches.sh

Try our git alias! 试试我们的git别名!

Go to directory where your git repo resides. 转到您的git repo所在的目录。

  • (Note: If a repo does not already exist, do a normal "git clone git@..." command, which will clone the master branch first, before our new "git clone-all-branches" command will work.) (注意:如果回购尚不存在,请执行常规的“ git clone git @ ...”命令,该命令将首先克隆master分支,然后才能使用新的“ git clone-all-branches”命令。)

View the current branches: 查看当前分支:

$ git branch // Should be the current branches available $ git branch //应该是当前可用的分支

Try our new git alias: 试试我们新的git别名:

$ git clone-all-branches

View branches again: 再次查看分支:

$ git branch // All branches now available! $ git branch //所有分支现已可用!

#5 Alternatively, create a "bash alias" to run our .gitcloneallbranches.sh script: #5或者,创建一个“ bash别名”来运行我们的.gitcloneallbranches.sh脚本:

  • [Instead of #4, open .bashrc (or .bash_profile or .profile)]: [代替#4,打开.bashrc(或.bash_profile或.profile)]:

$ subl .bashrc

Inside that file, add an "alias" by adding the following: 在该文件内,通过添加以下内容来添加“别名”:

# Adds ability to clone all branches of a repo, by typing 'clone-all-branches' in any directory with a git repo: alias clone-all-branches='source ~/.gitcloneallbranches.sh'

Try our bash alias! 试试我们的bash别名!

Go to directory where your git repo resides. 转到您的git repo所在的目录。

  • (Note: If a repo does not already exist, do a normal "git clone git@..." command, which will clone the master branch first, before our new "clone-all-branches" command will work.) (注意:如果回购尚不存在,请执行常规的“ git clone git @ ...”命令,该命令将首先克隆master分支,然后新的“ clone-all-branches”命令将起作用。)

View the current branches: 查看当前分支:

$ git branch // Should be the current branches available $ git branch //应该是当前可用的分支

Try our new git alias: 试试我们新的git别名:

$ clone-all-branches

View branches again: 再次查看分支:

$ git branch // All branches now available! $ git branch //所有分支现已可用!

There is an alternative way: just create a script called git-cloneallbranches that lives somewhere in your $PATH (for example, if you have $HOME/bin in your $PATH , that might be a good place). 还有另外一种方法就是:创建一个名为脚本git-cloneallbranches的某个地方生活在你的$PATH (例如,如果你有$HOME/bin在你的$PATH ,这可能是一个好地方)。 When you run git cloneallbranches , it will then automatically run your script. 当您运行git cloneallbranches ,它将自动运行您的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM