【问题标题】:Create an alias for several git commands [duplicate]为几个 git 命令创建别名 [重复]
【发布时间】:2013-05-17 10:42:00
【问题描述】:

我通常在 git 的“工作”分支上使用这些命令

git commit -a -m "blah! blah!"
git checkout master
git merge work
git checkout work

我听说过 git 别名。是否可以通过别名或其他方式将所有这些命令组合成一个。

【问题讨论】:

标签: windows git command-line git-bash


【解决方案1】:

除了在 cmets 中发布的duplicate question 的答案之外,您还可以在您的PATH 的某个位置放置一个名为git-foo(例如)的可执行shell 脚本,并使用git foo <args> 访问它,就好像它一样是原生 Git 命令。

如果别名的逻辑对于配置文件中的单行来说太长,这可能会很有用。

例如:

$ cat > ~/bin/git-foo
#!/bin/bash
echo "Foo: $1"
^C
$ chmod +x ~/bin/git-foo
$ git foo test
Foo: test

【讨论】:

  • 对不起,我是 Windows 用户,刚接触 git。那么你能告诉我你的例子在做什么吗
【解决方案2】:

您还可以将以下内容添加到您的 shell.rc 文件中:

gmm 将所有给定的分支与您当前所在的分支合并

调用:gmm branch1-to-be-merged branch2-to-be-merged ...

gmm () {
        src=`git branch --no-color | grep "*" | cut -f 2`
        for i in $@
        do
                git checkout $i
                git merge $src
        done
        git checkout $src
}

【讨论】:

    【解决方案3】:

    正如@Will-Vousden 所建议的,可以编写一个 shell 脚本来执行此操作。例如:

    #!/bin/bash
    git commit -a -m "$1"
    git checkout master
    git merge work
    git checkout work
    

    你会这样称呼:

    $ git-commit.sh "Commit message in quotes"
    

    但是,如果存在合并冲突等情况,这种方法(或您自己的别名)很容易失败:

    $ git-commit.sh "Commit message in quotes"
    [work 1a6e17f] Commit message in quotes
     1 file changed, 1 insertion(+), 1 deletion(-)
    Switched to branch 'master'
    Auto-merging file.txt
    CONFLICT (content): Merge conflict in file.txt
    Automatic merge failed; fix conflicts and then commit the result.
    file.txt: needs merge
    error: you need to resolve your current index first
    

    将您留在主分支中,但合并冲突未解决。

    您可能可以在脚本中添加一些逻辑来检测 - 并可能解决 - 此类问题,但这听起来比仅运行四个单独的命令要多得多的工作和努力!

    【讨论】:

      猜你喜欢
      • 2012-05-30
      • 2013-07-17
      • 2021-12-30
      • 2013-07-06
      • 2020-04-21
      • 2020-01-26
      • 1970-01-01
      • 2017-09-05
      • 2011-04-22
      相关资源
      最近更新 更多