git - 命令确定当前 HEAD 的上游 ref?

标签 git git-branch

我正在寻找我希望的简单的一行命令来确定当前 checkout 分支的正确上游引用?

本质上类似

git branch --remote HEAD

哪个(如果有效)会将符号模式 HEAD 转换为当前分支名称,然后是选项 --remote然后将其更改为远程跟踪分支的 ref。 (但它不会那样做!)

如果我有分支morehelp配置为

remote = origin
merge = refs/heads/morehelp

简单的命令行将返回 refs/remotes/origin/morehelp这是它的上游跟踪分支(非常适合 git reset --hard <ref> 覆盖更新的情况)

最佳答案

我觉得你想要

git rev-parse --symbolic-full-name @{u}

@{u}HEAD的上游跟踪分支的缩写,选项告诉rev-parse在您想要的格式,而不是打印 SHA 提交 ID。

来自 git help rev-parse

   --symbolic
       Usually the object names are output in SHA1 form (with possible ^ prefix); this option makes them output in a form as close to the original
       input as possible.

   --symbolic-full-name
       This is similar to --symbolic, but it omits input that are not refs (i.e. branch or tag names; or more explicitly disambiguating
       "heads/master" form, when you want to name the "master" branch when there is an unfortunately named tag "master"), and show them as full
       refnames (e.g. "refs/heads/master").

关于git - 命令确定当前 HEAD 的上游 ref?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15284109/

相关文章:

macos - mac osx 上的 git : how to push filename cases to origin?

git - 您如何配置 git 以在每次 pull 时自动获取标签?

git - 跟踪源代码变体

git - 未出现在任何分支中但在 "git show"中可用的提交发生了什么

git - 如何列出包含特定存储库上给定提交的分支?

merge 两个分支时git merge ambiguous

git - 你什么时候使用 Git rebase 而不是 Git merge?

Gitolite git克隆错误

git - IntelliJ 中与 'Default Task' 关联的分支

git - 在 Git 中跨分支共享文件