Branch descriptions in Git
Is there a way in Git to have a 'description' for branches?
While I try to use descriptive names, working for a while on a single branch sometimes dampens my memory of why I made some of the other topic branches. I try to use descriptive names for the branches, but I think a 'description' (short note about the purpose of the branch) would be nice.
Solution 1:
Git 1.7.9 supports this. From the 1.7.9 release notes:
* "git branch --edit-description" can be used to add descriptive text to explain what a topic branch is about.
You can see that feature introduced back in September 2011, with commits 6f9a332, 739453a3, b7200e8:
struct branch_desc_cb {
const char *config_name;
const char *value;
};
--edit-description::
Open an editor and edit the text to explain what the branch is for, to be used by various other commands (e.g.
request-pull
).
Note that it won't work for a detached HEAD branch.
That description is used by the script request-pull: see commit c016814783, but also git merge --log
.
request-pull
is a script used to summarizes the changes between two commits to the standard output, and includes the given URL in the generated summary.
[From @AchalDave] Unfortunately, you can't push descriptions since they're stored in your config, making it useless for the sake of documenting branches in a team.
Solution 2:
If you do end up using the README, create a git alias modifying git checkout
so that your README is displayed every time you switch branches.
For example, add this in ~/.gitconfig, under [alias]
cor = !sh -c 'git checkout $1 && cat README' -
After this, you can run git cor <branch_name>
to switch branch and display the README of the branch you're switching to.
Solution 3:
Use git branch --edit-description
to set or edit a branch description.
Here is a shell function to show branches similar to git branch
but with descriptions appended.
# Shows branches with descriptions
function gb() {
current=$(git rev-parse --abbrev-ref HEAD)
branches=$(git for-each-ref --format='%(refname)' refs/heads/ | sed 's|refs/heads/||')
for branch in $branches; do
desc=$(git config branch.$branch.description)
if [ $branch == $current ]; then
branch="* \033[0;32m$branch\033[0m"
else
branch=" $branch"
fi
echo -e "$branch \033[0;36m$desc\033[0m"
done
}
Here is what gb
looks like, shown here as text in case the image rots:
$ gb
* logging Log order details. Waiting for clarification from business.
master
sprocket Adding sprockets to the parts list. Pending QA approval.
And as an image, so you can see the colors:
Solution 4:
The README
suggested by Chris J can work, provided it is setup with a custom merge driver defined in a .gitattribute
.
That way, the local version of the README
is always preserved during merges.
The "description" for branches is also know as a "comment" associated with that meta data, and it is not supported.
At least, with a README
file, you can, for any branch, do a:
$ git show myBranch:README
If your README is at the root directory of your REPO, it will work from any path, since the path used by git show
is an absolute one from the top directory of said repo.