I will repeat what @matt has amply detailed in his answer : git
tracks commits, but not branches
There is however something that looks like "the life of that branch", it's the reflog for that branch :
git reflog my/branch
You can inspect the history and see if you find any information there.
For example : the commit when the branch was created would be :
git reflog my/branch | tail -1
# and use something like `| awk '{ print $1 }'` for the sha,
# or parse the 'branch: Created from ...' message (if there is one)
# to see if there is a branch name
git reflog --format="%H" my/branch | tail -1 # if you want only the sha
But again, there are caveats : if you have run git rebase
during the lifetime of the branch, then the initial commit is probably not the fork point you are looking for anymore.
Another proxy could be : look at the commits that are in the history of branch B
, and in the history of no other branch :
# the following will list all branches except 'my/branch' :
git branch --format="%(refname:short)" | grep -v "my/branch"
# here is a way to list all commits on 'my/branch', excluding commits
# from all other branches.
# Adding `--boundary` will also display the first 'hidden' commit
git log --boundary --oneline --graph my/branch --not $(<the command above>)
# you can also replace 'git log' with 'git rev-list', if you only want the hashes
With some luck, the commit mentioned as a boundary is the one you are looking for.
The caveats being : if there are some merges in the history of your branch, you may have several "boundary" points, and if there is another branch that "forked off" from B more recently than the X
you are looking for, then the boundary would be the fork point with that branch, and not with master
or develop
.
update based on your comment : you can
- list all feature branches in your local clone (except your own branch)
- look for the "fork point" of your current branch from any of those branches
# get the name of your current active branch :
mybranch=$(git rev-parse --abbrev-ref HEAD)
# make a separate function to list on stdout "all the branches except mine"
list_branches () {
# list the `master` branch
echo "master"
# get all 'origin/somefeature' branches, excluding mybranch
# suggestion: list remote branches (you will get all features,
# even if you haven't created a local checkout of a branch)
git branch -r --format="%(refname:short)" |\
grep "/feat/" |\ # you can grep for a pattern if you have one
grep -b "$mybranch" # remove your branch from the lot
}
# get the 'boundary' commits of your branch with respect to
# all the other branches
base=$(
git rev-list --boundary --graph "$mybranch" --not $(list_branches "$mybranch") |\
grep -e "^-" |\ # rev-list will prefix boundary commits with a '-'
tail -1 |\ # if there are several, only take the last one
tr -d '-' # delete that leading '-'
)
# with some luck, we computed the correct base commit :
git diff $base...