Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT - How to know the branch a branch branched from?

Tags:

git

git-branch

Support I create multiple branches (aaa, bbb, ccc) from a commit. Then, I create a new branch (ddd) from one of the branch (bbb) and a make a commit on it.

Then I pushed everything to a remote. How would another person knows the new branch (ddd) comes from which branch?

The git commands I did was:

git branch aaa
git branch bbb
git branch ccc
git branch ddd bbb
git checkout ddd
echo 2 >> file
git add file
git commit -m "2"

And the git log would show


* commit d259a3b (HEAD, ddd)
|
| 2
|

* commit efb038c (develop, ccc, bbb, aaa)
|
| 1
|

* commit dd24bb6 (master)

It is even possible to know that ddd was branched from bbb?

Thanks

like image 590
Ng Khin Hooi Avatar asked Apr 09 '15 08:04

Ng Khin Hooi


People also ask

How can I tell which branch a branch is from?

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?

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master . As you start making commits, you're given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically.

How can you tell if two branches are identical?

In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.

How do I know which branch is my head?

You can view your repository's heads in the path . git/refs/heads/ . In this path you will find one file for each branch, and the content in each file will be the commit ID of the tip (most recent commit) of that branch.


2 Answers

add those two functions to your .bashrc:

function newbranch() {
    history_file=".git/branching_history"
    from=`git rev-parse --abbrev-ref HEAD`
    to=$1
    git checkout -b $1 > /dev/null 2>&1
    DATE=`date '+%Y-%m-%d %H:%M:%S'`
    echo "$DATE: from $from created branch $1" >> $history_file
}

function branchinghistory() {
    cat .git/branching_history
}

then when you create a new branch, don't run git checkout -b mybranch but do:

newbranch mybranch

this will store your branching log in .git/branching_history file. You can check the log with:

branchinghistory

the output should be:

2020-04-22 23:59:06: from master created branch mybranch

like image 140
kkarpieszuk Avatar answered Oct 14 '22 01:10

kkarpieszuk


It is indeed impossible in general. Git records only1 the single commit-ID (SHA-1) to which a reference name (such as a branch or tag) points. The history of that commit is determined solely by that commit's parent IDs, which do not record branch-names. When you push to a remote, your "push" operation sends the SHA-1 of the commit you're pushing (plus more SHA-1s for any other linked objects—trees and files, parent commits, etc—plus the contents of all those objects, as needed based on what's missing on the remote), and asks the remote to set its branch to point to the new SHA-1.

In other words, you tell the remote: "here, have commit 1234567, and set branch label ddd = 1234567". It may tell you "to do that I need five other commits", one of which you have labeled as bbb, but if you don't tell the remote "oh by the way set label bbb to this other commit too" then it won't have that information anywhere.


1This is a bit of an overstatement: git also records, in the reflog, each SHA-1 that is stored in a label, including branch labels. It is therefore possible to walk back through the label's history to "figure out where it started". There are two limits on this though: reflogs are purely local, never transmitted by fetch or push; and reflogs expire, generally after 90 days in these cases (although this is configurable and there are additional complexities). So as long as we say there's a push step, or allow more than 3 months to pass, there's no way to tell.

like image 31
torek Avatar answered Oct 14 '22 01:10

torek