如何获取分支开始的信息?

发布于 2024-10-08 09:37:12 字数 587 浏览 0 评论 0原文

据我所知,提交对象仅包含有关父母的信息,所以如果我遇到这样的情况:

 *  branch-1
 |
 o
 |
 o  master
 |
 o 

这相当于

   *  branch-1
   |
   o
  /
 o  master
 |
 o 

但如果我的主人继续前进怎么办?

 o master
 |
 o *  branch-1
 | |
 o o
 |/
 o
 |
 o 

在branch-1上, git log --graph --decorate 只会显示:

 *  branch-1
 |
 o
 |
 o
 |
 o 

如果我知道从哪个分支开始,我可以调用 git merge-base masterbranch-1 ,但是如果我不知道我是从哪个分支开始的怎么办?


附言。 我仍在学习英语,但有时我会犯一些愚蠢的错误。我正在尽我所能,用英语写问题和答案,但是如果您能编辑我的帖子,以防出现任何错误,我将非常高兴。我保证,你的努力不会白费。

As far as I know, commit object contains information only about parents, so if I have situation something like this:

 *  branch-1
 |
 o
 |
 o  master
 |
 o 

which is some kind of equivalent of

   *  branch-1
   |
   o
  /
 o  master
 |
 o 

but what if my master will go forward?

 o master
 |
 o *  branch-1
 | |
 o o
 |/
 o
 |
 o 

being on branch-1, git log --graph --decorate will show me only:

 *  branch-1
 |
 o
 |
 o
 |
 o 

if I know from which branch I was started, I can call git merge-base master branch-1, but what if I don't know from which branch I was started?


PS.
I am still learning English, however sometimes I am making stupid mistakes. I am doing my best, writing questions and answers in English, however I would be very glad if you will edit my post in case of any mistakes. I promise, your effort will not be wasted.

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

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

发布评论

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

评论(3

画▽骨i 2024-10-15 09:37:12

确实,git 不会永久存储这些信息,但无论如何您都可以找到。 Git 有重新记录!

git reflog show <branch>

如果分支是在过去 90 天内创建的(默认情况下;使用 gc.reflogExpire 来更改此设置),则该引用日志中的最后一行将是分支的创建。

我在这里假设您想知道的是创建分支的提交,而不是它从哪个分支分叉。这将很难找到 - 我最好的猜测是,对于每个分支,在创建目标分支时获取其位置,并查看它是否包含目标分支的起点。

这个故事的寓意是,您应该采用一种工作流程,在该工作流程中您知道您从哪个分支派生了分支。新功能和错误修复可能会在主分支上的某个稳定点开始,并且子主题分支可以命名为 - 作为提醒。

编辑:

好的,假设您知道提交和时间(在本例中,是创建分支的提交和时间)。您正在寻找当时提交所在的分支。

git for-each-ref --format='%(refname)' refs/heads/* | while read b; do
    if [ "$(git rev-parse $b@{$date_time})" = "$target_commit" ]; then
        echo "branch $b contained commit $target_commit at $date_time"
    fi
done

我认为这应该作为 bash 脚本/单行代码工作。想法:对于每个分支,在给定的日期和时间获取其位置,并查看它是否是目标提交。如果你想更灵活,你可以测试 $(git merge-base $(git-rev-parse $b@{$date_time})) 是否是目标提交;也就是说,目标提交是否是当时给定分支的祖先

当然,如果您的历史记录相对干净,您始终可以使用 gitk --branches 或 git log --graph --branches [--oneline] 来只是看到一张漂亮的照片。

It's true that git doesn't permanently store this information, but you can likely find out anyway. Git has reflogs!

git reflog show <branch>

If the branch was created in the last 90 days (by default; use gc.reflogExpire to change this), the last line in that reflog will be the creation of the branch.

I'm assuming here that what you want to know is the commit a branch was created at, not which branch it forked from. That'd be a lot harder to find out - my best guess would be to, for each branch, take its position at the time your target branch was created and see if it includes the target branch's starting point.

The moral of the story here is that you should adopt a workflow in which you know which branch you forked your branch from. New features and bugfixes presumably will start at some stable point on your master branch, and subtopic branches can be named <topic>-<subtopic> as a reminder.

Edit:

Okay, so let's say you know a commit and a time (in this case, the commit and time a branch was created at). You're looking to find out what branch that commit was on at that point.

git for-each-ref --format='%(refname)' refs/heads/* | while read b; do
    if [ "$(git rev-parse $b@{$date_time})" = "$target_commit" ]; then
        echo "branch $b contained commit $target_commit at $date_time"
    fi
done

I think that should work as a bash script/one-liner. The idea: for every branch, take its position at the given date and time, and see whether it's the target commit. If you want to be even more flexible, you could test whether $(git merge-base $(git-rev-parse $b@{$date_time})) is the target commit; that is, whether the target commit was an ancestor of the given branch at that time.

And of course, in cases when your history is relatively clean, you can always use gitk --branches or git log --graph --branches [--oneline] to just see a nice picture.

紫瑟鸿黎 2024-10-15 09:37:12

我猜您正在寻找

git log --graph --oneline --all

查看此线程以获取更详细的解释。
Stackoverflow 答案

I guess you looking for

git log --graph --oneline --all

Check this thread out for a more detailed explaination.
Stackoverflow answer

清欢 2024-10-15 09:37:12

Git 并没有真正的概念,如果你想添加这个,你需要添加一个启发式,因为 git 真正关心的是为树中的给定提交附加一个标签。

Git doesn't really have a concept of this, if you want to add this, you'd need to add a heuristic, because all git really cares about is attaching a label to a given commit in the tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文