git - 如何从 git 目录外获取 $(git_current_branch) 变量

git - how to get $(git_current_branch) variable from outside the git directory

我有一个包含以下代码的 bash 脚本:

git -C "" push origin "$(git_current_branch)"

其中 "" 是我要推送的 git 目录。

问题是,当我从 git 目录外 运行 脚本时,我无法获得正确的 "$(git_current_branch)" 变量。我知道在推送之前我可以 cd"",但我想要一个更好的解决方案(这使我的脚本更干净)。

有什么办法吗?提前致谢!

编辑:如果可能的话,我更喜欢通过 git 命令选项来执行此操作。

你可以试试:

current_git_branch=$(git -C "" branch | sed  '/^\*/!d;s/\* //')

说明

  • 在目录“$1”中调用 git 并打印所有分支
  • 删除行首没有 * 的所有分支
  • 从行中删除当前分支的“*”标记