我怎样才能知道哪个远程“父母”?我的分支是基于什么分支?

发布于 2024-11-17 00:09:21 字数 337 浏览 3 评论 0原文

我有一个场景,其中我的本地存储库中有几个必须同步的远程跟踪分支。我们的工作流程模型是:

  • 根据所需的远程跟踪分支在本地创建一个分支
  • 使我们的更改
  • 构建/测试/修复
  • 提交
  • 推送回远程服务器

我注意到“git status”没有显示我的分支除非有什么变化,否则以本地分支为基础;即未提交的本地更改或最近的获取使我的本地分支落后于时代。有没有什么方法可以知道我的本地分支基于哪个分支而无需进行更改?类似“git status -showparentbranch”或其他一些可以显示这一点的命令。有时我会遇到这种需求,但还不知道如何满足它。

I have a scenario in which there a several remote tracking branches within my local repository that I must sync up to. Our workflow model is:

  • make a branch locally, based off of the desired remote tracking branch
  • make our changes
  • build/test/fix
  • commit
  • push back to the remote server

I've noticed that "git status" doesn't show me what branch my local branch is based on unless something has changed; i.e. uncommitted local changes or a recent fetch puts my local branch behind the times. Is there some way of knowing what branch my local branch is based on without having to change things? Something like, "git status -showparentbranch" or some other command that would show this. Occasionally I run into this need but don't know yet how to satisfy it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雪落纷纷 2024-11-24 00:09:21

试试这个:

git log --graph --decorate

try this:

git log --graph --decorate
奢望 2024-11-24 00:09:21

Git 不跟踪提交通过了哪些分支。没有办法说清楚。如果提交发生在您的存储库上,那么您可以检查引用日志,但仅此而已。看看Pro Git 书中对 DAG 的解释 - 还可以阅读在那里重新登录。

您还可以使用 gitk --all 或 git log --graph --decorate 更好地可视化历史记录

希望这会有所帮助。

Git does not track what branches a commit was put through. There is no way to tell. If the commits happened on your repo, then you can inspect the reflog, but that's about it. Take a look at the explanation of the DAG in the Pro Git book - also read up on reflog in there.

You can also visualize history better with gitk --all or git log --graph --decorate

Hope this helps.

云柯 2024-11-24 00:09:21

git Branch -vv 将:

  • 列出所有您的本地分支,
  • 在每个本地分支旁边显示远程分支的名称,
  • 突出显示活动的本地分支

...从此您将能够确定当前活动分支的远程分支等等。

如果您有很多本地分支机构,则列表可能会很长。使用 git 分支 -vv | grep SOMEWORD 将输出限制为仅包含 SOMEWORD 的分支。如果您能想到您的分支独有的单词,您将获得最佳过滤器(仅一个结果)。

您还将在输出中获得一些附加数据,即上次提交的编号 (SHA1) 和消息。 grep 过滤器将应用于这些。我找不到排除它的方法。

来自 Git 分支文档

-v

-vv

--详细

在列表模式下,显示每个头的 sha1 和提交主题行,以及与上游分支(如果有)的关系。如果给出两次,也打印上游分支的名称(另请参阅 git remote show )。

(根据您的评论,是的,似乎“正确”的问题会询问“远程”分支而不是“父”分支。但这也是我搜索的!:))

git branch -vv will:

  • list all your local branches
  • display the name of the remote branch next to each local branch
  • highlight the active local branch

...from this you will be able to determine the remote branch of the current active branch, and more besides.

If you have a lot of local branches, the list may be very long. Use git branch -vv | grep SOMEWORD to limit the output to only branches containing SOMEWORD. If you can think of a word unique to your branch you'll get the best filter (one result only).

You will also get some additional data in the output, namely the number (SHA1) and message of the last commit. The grep filter will apply to these to. I couldn't find a way to exclude it.

From the Git branch documentation:

-v

-vv

--verbose

When in list mode, show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well (see also git remote show ).

(Based on your comment, yes, it seems that the 'correct' question would ask about the "remote" branch rather than the "parent" branch. But that's what I searched for too! :) )

菊凝晚露 2024-11-24 00:09:21

有多种方法可以检查这一点。我最终以免费的方式获得了这个结果。

  1. 使用 gitkraken(付费私人存储库!),它为您提供了查看分支和提交树的绝佳方式。通过向下滚动,您将到达父分支的根部。

  2. 如果您使用 bitbucket,那么它们会在网络中提供视图,以查看您对所有分支的所有提交(通过从下拉列表中选择所有分支)。

  3. 免费方式,通过 git commnad。

    git log --graph --decorate --oneline --all
    

    命令说明

    git log 查看日志。

    --graph以图表形式查看日志。

    --装饰以创辉模式查看日志。

    --oneline查看日志,仅一行,未提交完整详细信息。

    --全部查看日志,所有分支。 (解决此问题很重要

there are multiple ways to check that. i have endup with Free way to get this result.

  1. use gitkraken(paid for private repository!), it provide you wonderfull way to see Tree of your branchs and commits. by scolling down you will reach root of your parent branch.

  2. if you use bitbucket, then they provide view in web to see all your commits for all branchs(By selecting all branch from dropdown).

  3. Free way, via git commnad.

    git log  --graph --decorate --oneline --all
    

    Commnad Expaination

    git log view log.

    --graph view log as graph.

    --decorate view log in colorfull mode.

    --oneline view log, row only one line not full detail in commits.

    --all view log, all branchs. (important to solve this thread)

讽刺将军 2024-11-24 00:09:21

这是查看当前分支的父分支的命令

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'

Here is a command to see the parent branch of your current branch

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
凡间太子 2024-11-24 00:09:21

这是 Arpit 的答案的副本,但有解释。

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'

该命令链用于查找不是当前分支的最新分支。让我们一步步分解:

  1. git show-branch -a:

    • 显示所有分支和引用的提交祖先图。
  2. grep '\*'

    • 过滤输出以仅显示包含星号 (*) 的行。在 git show-branch 中,星号表示当前分支或包含提交的分支。
  3. grep -v \git rev-parse --abbrev-ref HEAD``

    • 排除与当前分支对应的行。 git rev-parse --abbrev-ref HEAD 返回当前分支的名称,grep -v 排除与此名称匹配的行。
  4. 头-n1

    • 从其余行中取出第一行。这应该是不是当前分支的最新分支。
  5. sed 's/.*\[\(.*\)\].*/\1/'

    • 使用sed(流编辑器)从行中提取分支名称。该模式匹配方括号 ([...]) 之间的所有内容并捕获它。
  6. sed 's/[\^~].*//'

    • 进一步处理分支名称以删除 ^~ 后面的任何字符。这些字符在 Git 中用于引用提交的祖先(例如,branch^ 是分支尖端的父级,branch~1 是第一个祖先,等等.).

This is a copy of Arpit's answer but with an explanation.

git show-branch -a | grep '\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'

This command chain is used to find the most recent branch that is not the current branch. Let's break it down step-by-step:

  1. git show-branch -a:

    • Displays the commit ancestry graph of all branches and references.
  2. grep '\*':

    • Filters the output to show only the lines that contain an asterisk (*). In git show-branch, the asterisk indicates the current branch or a branch that contains the commit.
  3. grep -v \git rev-parse --abbrev-ref HEAD``:

    • Excludes the lines that correspond to the current branch. git rev-parse --abbrev-ref HEAD returns the name of the current branch, and grep -v excludes lines that match this name.
  4. head -n1:

    • Takes the first line from the remaining lines. This should be the most recent branch that is not the current branch.
  5. sed 's/.*\[\(.*\)\].*/\1/':

    • Uses sed (stream editor) to extract the branch name from the line. The pattern matches everything between square brackets ([...]) and captures it.
  6. sed 's/[\^~].*//':

    • Further processes the branch name to remove any characters following ^ or ~. These characters are used in Git to refer to ancestors of commits (e.g., branch^ is the parent of the tip of the branch, branch~1 is the first ancestor, etc.).
忘年祭陌 2024-11-24 00:09:21

一个也许更简单(最近)的替代方案(我的):https://stackoverflow.com/a/79132926/2039709

git merge-base main $1 \
| xargs -I{} git branch --sort=creatordate --contains {} \
| grep -Ev "main|$1" \
| head -n 1

A perhaps simpler (recent) alternative (mine): https://stackoverflow.com/a/79132926/2039709

git merge-base main $1 \
| xargs -I{} git branch --sort=creatordate --contains {} \
| grep -Ev "main|$1" \
| head -n 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文