DEV Community


Posted on

How to name your default git branch "bananas"

Branch names are not special. They are just references to commits. If you want you can name your branch "bananas" or any other name if you want. Git in its simplest form only deals with blobs, trees, sub-trees, and commit objects.

Let's walk through an example of creating a manual commit to show how you can give a default branch any name:

You are in a working directory and have added a file to the Index using the "git add" command once you do that, git inserts your file into a blob (binary large object). This blob isn’t referenced by a tree and only exists at the .git/Index file. You can confirm this by checking the .git directory, it holds a lot of information normally ignored by casual git users.

Now let's record the contents of the index in a tree:

$ git write-tree

It will return a 40 character hash-id that for example looks like 17e2de4, I truncated it for brevity.

Now manually make a new commit object by using this tree directly:

$ echo "Initial commit" | git commit-tree 17e2de4

A commit -id is returned from that command example cce612

Now you have to register the commit as the new head of the current branch:

$ echo <40 character commit id from the commit-tree above > .git/refs/heads/master

Remember I said, branches are just names or references to commit objects? so the above command can look like this:

$ echo <40 character commit id from the commit-tree above > .git/refs/heads/bananas

please note that I have swapped "master" for "bananas".

Now after creating the "bananas" branch, we must associate our working tree with it:

$ git symbolic-ref HEAD refs/heads/bananas

You can now run git log to verify that your operation was successful. Casual git users never have to do this because git does it under the hood once you issue the git add, git commit commands respectively.

Git commands like "git write-tree and commit-tree" etc are called PLUMBING COMMANDS which commands like "git add, commit" etc are PORCELAIN COMMANDS.

If you are interested in learning more git concepts like this and how git works under the hood, you can check here Learn Git Visually. Disclosure: I wrote it, so shameless plug :-)

Discussion (0)