Git 配置别名问题

Git config alias issue

我正在尝试为我的 "merge to staging" 工作流程创建一个别名。

假设我在分支 "dev/layout_fix" 中完成了一项任务,并希望在提交后将其部署到暂存区。

这是我的方式:

  1. git checkout staging
  2. git merge dev/layout_fix --no-ff
  3. git push
  4. git checkout dev/layout_fix

现在我尝试了这个方法:

[alias] branch-name = "!git rev-parse --abbrev-ref HEAD" stagify = "!git checkout staging && git merge $(git branch-name) --no-ff && git push && git checkout $(git branch-name)"

但由于结帐,别名分支名称是 "staging"。

是否可以在签出登台前将当前分支名称写入变量?

Is there a possibility to write the current branch name in a variable before staging is checked out?

当然可以。由于您在 git 别名前加上 !,它们只是 shells 脚本。因此,您可以像对任何 shell 脚本一样,将 git 命令的输出粘贴到变量中,例如,像这样:

[alias]
branch-name = "rev-parse --abbrev-ref HEAD"                             
mybranch = "!branch=$(git branch-name); echo my branch is: $branch"     

有了这个定义,我可以运行这个:

git mybranch

并得到:

my branch is: master

这应该足以让你继续前进。

您可以使用git describe --exact-match --all获取当前分支名称。

您可以编写脚本并将其部署到 /usr/libexec/git,而不是别名。您只需将它放在那里并命名为 git-stagify。我是凭记忆写的,但我相信这是默认路径。您可以通过搜索 git-pull 所在的位置(使用破折号)、使用 locate 或您的发行版中可用的任何内容来找到它。这将为您提供 bash 脚本环境的全部好处,仅使用别名时可能会缺少这些好处。

完整的别名命令如下所示:

stagify = "!branch=$(git rev-parse --abbrev-ref HEAD) && git checkout staging && git merge $branch --no-ff && git push && git checkout $branch"

感谢@larsks 的正确方法。