开发者

Are git branches just tags?

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 t开发者_StackOverflowag 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜