Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get information where branch starts?

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.

like image 719
noisy Avatar asked Dec 18 '10 11:12

noisy


People also ask

How do you find out from which branch a branch was created?

You can use git branch --contains to list all the branches descended from the tip of develop , then use grep to make sure feature is among them. If it is among them, it will print " feature" to standard output and have a return code of 0.

Where do git branches point?

In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes.

Where does the git begin?

The GI tract includes all structures between the mouth and the anus, forming a continuous passageway that includes the main organs of digestion, namely, the stomach, small intestine, and large intestine.


2 Answers

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.

like image 196
Cascabel Avatar answered Nov 12 '22 12:11

Cascabel


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.

like image 31
Arafangion Avatar answered Nov 12 '22 12:11

Arafangion