如何获取分支开始的信息? [英] How to get information where branch starts?

查看:113
本文介绍了如何获取分支开始的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  * branch-1 
|
o
|
o主
|
o

这是某种相当于

  * branch-1 
|
o
/
o master
|
o

但是如果我的主人会前进?

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

在分支-1上, git log --graph --decorate 只会显示我:

  *分支-1 
|
o
|
o
|
o

如果我知道从哪个分支开始,我可以调用 git merge-base master branch-1 ,但是如果我不知道我从哪个分支开始了?


$ b

PS。
我仍​​在学习英语,但有时我犯了愚蠢的错误。我尽我最大努力,用英语写问题和答案,但如果您在出现任何错误时编辑我的帖子,我将非常高兴。我承诺,您的努力不会被浪费。

解决方案

确实,git不会永久存储这些信息,但您可以无论如何可能会发现。 Git有reflogs!

  git reflog show< branch> 

如果分支是在过去90天内创建的(默认情况下使用 gc.reflogExpire 来改变这个),那个reflog的最后一行是创建分支。



我在这里假设你想知道的是一个分支被创建的提交,而不是分支分支。这很难发现 - 我最好的猜测是,对于每个分支,在你的目标分支被创建的时候采取它的位置,并且看看它是否包括目标分支的起点。

这里故事的寓意是你应该采用一个工作流程,在这个工作流程中你知道你从哪个分支分支出你的分支。新功能和错误修正大概会从主分支上的某个稳定点开始,副标题分支可以命名为< topic> - < subtopic> 作为提醒。 p>

编辑:

好吧,让我们假设你知道一个提交和一个时间(在这种情况下,提交和时间在一个分支创建)。你正在寻找那个提交的分支。

  git for-each-ref --format ='%(refname)'refs / heads / * |同时阅读b;做
if [$(git rev-parse $ b @ {$ date_time})=$ target_commit];然后
echobranch $ b contains 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] 只是看到一个不错的图片。


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.

解决方案

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.

这篇关于如何获取分支开始的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆