Append this snippet into your .bashrc
or .zshrc
files.
Pay attention to the real path of your git binary by doing:
$ which git
/usr/local/bin/git
If git has a different path, adapt it into the following snippet.
This way you can register the history of a branch when it is created and rebased onto a new branch:
# keeps track of the branch from which a new branch is created.
# it updates the history of the branch in case of rebase onto
# when the branch is deleted it deletes its history also
# es. (branchA) $ git checkout -b branchB
# (branchB) $ git branch --onto branchC branchA
# (branchB) $ git branch history
# branchB created from branchA
# branchB rebased onto branchC
git() {
if [ "${1}" == "checkout" ] && [ "${2}" == "-b" ]; then
mkdir -p .git/branches_history
if [ "${4}" != "" ]; then
# git checkout -b <new-branch> <from-branch>
echo "${3} created from ${4}" > .git/branches_history/${3}
else
# git checkout -b <new-branch>
echo "${3} created from $(/usr/local/bin/git branch --show-current)" > .git/branches_history/${3}
fi
elif [ "${1}" == "rebase" ] && [ "${2}" == "--onto" ]; then
mkdir -p .git/branches_history
if [ "${5}" != "" ]; then
# git rebase --onto <new-base> <old-base> <branch-to-be-rebased>
echo "${5} rebased onto ${3}" >> .git/branches_history/${5}
else
# git rebase --onto <new-base> <old-base>
echo "$(/usr/local/bin/git branch --show-current) rebased onto ${3}" >> .git/branches_history/$(/usr/local/bin/git branch --show-current)
fi
elif [ "${1}" == "branch" ]; then
if [ "${2}" == "-d" ] || [ "${2}" == "-D" ] || [ "${2}" == "--delete" ] || [ "${2}" == "--force-delete" ]; then
# git branch -d <branch> | git branch -D <branch>
rm -rf .git/branches_history/${3} &> /dev/null
elif [ "${2}" == "history" ]; then
if [ "${3}" != "" ]; then
# git branch history <branch>
branchName=${3}
else
# git branch history
branchName=$(/usr/local/bin/git branch --show-current)
fi
cat .git/branches_history/${branchName}
# return because git branch history is not a real git command, so git doesn't have to do anything
return 0
fi
fi
# perform the real git command
/usr/local/bin/git "${@}"
}
So, imagine that from branchA
you create a new branch named branchB
:
(branchA) $ git checkout -b branchB
Then, you rebase this new branch on branchC
, so the new base branch will change:
(branchB) $ git rebase --onto branchC branchA
To know the base branch of the current branchB
just do:
(branchB) $ git branch history
branchB created from branchA
branchB rebased onto branchC
Thanks to @kkarpieszuk for the idea