Information Technology Reference
In-Depth Information
What happens when you create a new branch with $ git branch doc ? Git cre-
ates a new file .git/refs/heads/doc and stores the SHA-1 of your current revi-
sion within it. Initially every branch is stored in a loose format.
Think of a branch as a pointer to a single node in a graph of revisions. Because
SHA-1 stored in hexadecimal textual format consumes 40 bytes, therefore creating a
branch means storing 40 bytes in a text file. This is one of the reasons why branching
in git is so efficient. The whole process of branch creation consists of saving a 41-bytes
long reference in a local storage system (40 bytes for SHA-1 and a newline character)!
Not only isn't there any data transfer, but there isn't any communication. It is instantan-
eous!
Notice that when you create a new branch with the $ git branch doc com-
mand , the file .git/HEAD remains unchanged. This means that git doesn't automat-
ically switch to a new branch. To switch to a new branch you have to issue the $ git
checkout doc command. This command changes the contents of .git/HEAD file
to:
ref: refs/heads/doc
As you can see the information about the current branch, the one that is denoted
with asterisk in the output of $ git branch command, is stored in .git/HEAD
file.
Which internal operations are performed by git when you commit? Git resolves the
name of your current branch using the .git/HEAD file. Let's suppose that the file
.git/HEAD contains ref: refs/heads/abc . This means that your current
branch is named abc . Once the name of the branch is resolved git reads the SHA-1
name of the latest revision stored in the abc branch. If the branch is stored in loose
format—the name is read from .git/refs/heads/abc file. Otherwise the
branch's name comes from .git/packed-refs . We will denote the SHA-1 of the
latest revision in the abc branch as XXXX . The revision XXXX will be used as a parent
of a new revision. Next, git creates and stores the new revision YYYY in the database.
During the process the name XXXX is used as a parent for the YYYY revision. Finally,
the SHA-1 name of a newly created revision, that is YYYY , is stored in the file .git/
refs/heads/abc . As you remember, a side effect of every commit is that the cur-
rent branch is stored in a loose format again.
To summarize, we can say that while you work on branch:
Search WWH ::




Custom Search