6

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.

noisy
  • 6,495
  • 10
  • 50
  • 92
  • "but what if I don't know from which branch I was started?" -- Do you use gitk? IMHO it's infinitely helping in visualizing the relationship between different branches. There are plenty of other visualization tools out there too, of course. – Tyler Dec 19 '10 at 06:09
  • I am trying use git by CLI. It helps me better understand how git works, however ASAIR gitk also will not show me name 'master' if it is already few commits ahead – noisy Dec 19 '10 at 07:48
  • What are you trying to achieve? You seem to want the commit where the branch started, but why? Maybe you can achieve your goal without this information... – Gauthier Dec 20 '10 at 12:20
  • see also [Finding a branch point with Git?](http://stackoverflow.com/q/1527234) – CharlesB Jul 22 '13 at 10:22

3 Answers3

12

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    It helps a little, however it only in my case returns information like: `7316b3d branch-1@{0}: commit: b11 07a106a branch-1@{1}: branch: Created from HEAD`. Please correct me if I am wrong, but all(?) branches are created from HEAD. – noisy Dec 19 '10 at 07:43
  • Ah, right. (You can create a branch anywhere, but the problem's there in this case.) I guess you are indeed in the harder case. I'll add a bit to the answer - pretty short on time right now though. – Cascabel Dec 19 '10 at 15:03
  • It's worth mentioning that reflogs are only relevant to your local repository. Any technique that uses them is fine only if all the changes happened on your own branch. Otherwise, you'll have to rely on the dag. – Noufal Ibrahim Oct 14 '14 at 14:10
1

I guess you looking for

git log --graph --oneline --all

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

Community
  • 1
  • 1
pravin
  • 1,106
  • 1
  • 18
  • 27
1

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.

Arafangion
  • 11,517
  • 1
  • 40
  • 72