git - 如何查找分支是本地跟踪分支还是用户创建的本地分支?

标签 git git-branch git-pull

我在我的本地存储库中使用“git branch -b branch-name origin/branch-name”在本地跟踪了一个远程跟踪分支。 我的远程分支是 test2/test2 (origin/branch-name),它在本地被跟踪为 test2。

原点也被命名为test2。 我还没有 checkout 我的本地跟踪分支 test2。

当我执行“git pull origin remote-branch:local-tracked-branch”时出现此错误

[test2]$ git pull test2 test2:test2 来自/gitvobs/git_bare/test2 ! [rejected] test2 -> test2(非快进)

而当我检查我的本地跟踪分支 test2 并执行 pull 'git pull origin local-tracked-branch' 我没有收到错误 我使用“git pull test2 test2”进行 pull

来自/gitvobs/git_bare/test2 * 分支 test2 -> FETCH_HEAD 自动 merge a.txt 自动 merge 失败;修复冲突,然后提交结果。

我知道添加 + (git pull test2 +test2:test2) 会有所帮助,但它会覆盖本地更改。

那么我如何知道哪些本地分支是我在本地使用“git branch new-branch-name”创建的,或者是使用 git branch -b branch-name origin/branch-name 在本地跟踪的?

最佳答案

git pull 困惑

过于具体

您的 git pull 命令包含太多信息。

[test2]$ git pull test2 test2:test2
From /gitvobs/git_bare/test2
! [rejected] test2 -> test2 (non fast forward)

i know that adding a + (git pull test2 +test2:test2) would help but it overwrites local changes.

这就是你的命令的意思:

#             *------------ (1) remote repository name
#            /     *------- (2) ref in remote repository
#           /     /     *-- (3) ref in  local repository
#          /     /     /
git pull test2 test2:test2

# Means this: From remote repository `test2` (1),
# fetch branch `test2` (2), store it in local branch `test2` (3), then
# merge the fetched history into HEAD.

你告诉 git pull 覆盖你的本地 test2使用 Remote 在其 test2 上的任何内容进行分支分支,然后将其与 HEAD merge 。您可能不想包含 refspec 的目标部分(:test2)。


如果您 checkout 的本地分支配置为跟踪某些内容(请参阅下面的“分支:...”),只需执行

git pull

如果您需要提供(或覆盖)远程和存储库,只需提供远程名称/url 和远程上的本地分支(省略 refspec 的最后部分):

git pull test2 test2

pull 入未 check out 的分支

git pull 是(如上所述)git fetchgit merge(或 git rebase).

一般来说, merge 可能涉及解决冲突。冲突解决需要工作树。因此,没有工作树就不可能执行正常的 merge 操作。这意味着您当前的 HEAD 必须是 merge 的父项之一(它将是第一个父项)。进行 rebase 还需要一个工作树来解决冲突。

由于 pull 涉及 merge 或 rebase ,因此不可能 pull 入未 checkout 的本地分支。您只能 pull 入当前 checkout 的分支。

分支:本地、跟踪、远程跟踪

各种类型的 Git 分支都是同一个底层对象:refs。引用住在 refs/ $GIT_DIR/refs/ 中的命名空间和 $GIT_DIR/packed-refs .

  • “本地”分支机构位于 refs/heads/命名空间。
    • 检查 test2本地分支机构引用:
      • git show-ref refs/heads/test2 , 或者
        • cat .git/refs/heads/test2 , 或
        • grep -F refs/heads/test2 .git/packed-refs
  • “远程跟踪”分支位于 refs/remotes/<remote-name>/ namespace 。
    • 远程跟踪分支是远程存储库分支的本地副本。
      • 当您这样想时,“远程跟踪”这个名称是有道理的,但它可能会与不幸命名为 --track 的名称混淆。 git branchgit checkout 的功能(查看最终分支类型)。
    • 检查 test2远程跟踪分支参​​考:
      • git show-ref refs/remotes/test2/test2 , 或者
        • cat .git/refs/remotes/test2/test2 , 或
        • grep -F refs/remotes/test2/test2 .git/packed-refs
  • 跟踪另一个分支的本地分支是在 refs/heads/ 中具有额外配置的普通本地分支(在 $GIT_DIR/config 中) :

    [branch "test2"]
            remote = test2
            merge = refs/heads/test2
    

    重要的是要注意 merge (或 rebase )配置选项在 Remote 上命名一个ref。所以refs/heads/test2这里表示本地分支test2在 Remote 上找到 test2 .特殊远程名称 .可用于引用本地存储库中的本地分支。

    • “跟踪”其他分支机构的本地分支机构的目的是让您可以轻松地输入 git pull。并将其 merge 到(或基于)其他分支的历史记录中。

您说您想要将普通本地分支与跟踪其他分支的本地分支区分开来。您可以通过在 $GIT_DIR/config 中查找分支配置来完成此操作文件。

您可以使用 git config 来执行此操作:

branch_tracks_something() {
    {
        git config branch."$1".merge ||
        git config branch."$1".rebase
    } >/dev/null 2>&1
}
# test local branch 
branch_tracks_something test2 && echo 'test2 tracks something' || echo 'test2 does not track anything'

或者,如果您有 Git 1.6.3 或更高版本,您可以使用 %(upstream) git for-each-ref 的格式:

{ echo 'show_ref_desc() {
    case "$1" in
        refs/heads/*)
            t=''
            test -n "$2" && t=" (tracks $2)"
            echo "local: $1$t"
        ;;
        refs/remotes/*)
           echo "remote tracking: $1"
        ;;
        *)
            echo "other: $1"
        ;;
    esac
}'; git for-each-ref --shell --format='show_ref_desc %(refname) %(upstream)'; } |
sh

输出看起来像这样:

local: refs/heads/test2 (tracks refs/remotes/test2/test2)
remote tracking: refs/remotes/test2/HEAD
remote tracking: refs/remotes/test2/test2

关于git - 如何查找分支是本地跟踪分支还是用户创建的本地分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2887242/

相关文章:

git - 需要 git rebase -Ignore-all-space 来保留我的空间

git - git子模块和子树之间的区别

git - 我应该如何组织我的 Git 存储库和分支来创建一个网站的多个版本?

Git:在 checkout 新分支之前没有提交分支

git - 如何推/pull Git rebase

php - Git上传项目全站

git - 如何创建一个新的(空的!) "root"分支?

git - Git分支未显示所有分支

git - 是什么导致 git 在 pull 时自动提交?

git - 在 pull 之前存储/提交不要丢失本地文件