I noticed the ability of git to checkout to a specific commit. After that, I began to understand how git really works.
But I want to be sure that this is correct: When I create a branch, it is nothing more than a tag that points to the current commit. When I checkout this branch, I checkout the commit this "tag" points to. Now, when I commit something, a new commit is created. The current checked out branch tag is now updated, so that it points to the new commit.
So... in fact, I could do all of this manually, right? It´s just a feature to make things easier.
Yes, that's a good model for what a branch is. Beware, though, about the terminology - git also has a concept of tags, but tags don't move - they forever point to the same commit.
Update: adding a little more detail, which might be of interest...
Your current branch is stored in the file HEAD
, which either points to a branch, in which case the contents look like:
ref: refs/heads/master
... or it points directly to a commit, in which case the contents will look like:
2b45553eec2019594724dcbb4c252a74cbb5f38e
In the former case, the branch master
is advanced when you create a new commit, but in the latter situation (known as "detached HEAD" for hopefully obvious reasons), no branch will be changed when you create a new commit.
Branch is a reference pointer, it points to current commit as you say.
But you shouldn't use those words interchangeably because they mean something else.
Yes, for git, a branch is basically just a reference to a commit that is automatically updated when you commit. You could track it yourself, as you noted.
In fact, git expose all the low-level blocks upon which more higher-level abstraction are build. You can if you want use command like git hash-object
, git mk-tree
and git commit-tree
to manually do what git add
and git commit
if you want (see the Raw Git chapter in the Git Book to know how to do it in details).
Please also note that git has two kind of tag. The lightweight tags are just reference to commit (via its hash) that don't move automatically. There is also the notion of tag objects that are real git objects, archived like commit, and that can contains a message and a signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With