在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Linux/ 如何理解git中的HEAD和branch?

如何理解git中的HEAD和branch?

在git中HEAD和branch是兩個(gè)特殊的引用,它們都指向commit。而且一般情況下,是HEAD指向branch然后再指向commit,但是當(dāng)HEAD處于游離狀態(tài)時(shí)它就不再指向branch而是直接指向commit,所以說(shuō)HEAD是指向活躍分支的游標(biāo)這句話似乎不太準(zhǔn)確,而是指向當(dāng)前的commit。

關(guān)于branch,本質(zhì)是指向commit的引用,這里的commit是單個(gè)的commit,當(dāng)有新的commit提交時(shí),branch會(huì)移動(dòng)到新的commit。但是我們?cè)诜种蠒?huì)提交很多的commit,然后再進(jìn)行合并的時(shí)候是將分支的所有commits合并過(guò)去,這樣的話是否可以將branch理解成一個(gè)commit串,它代表了所有提交的commit,在進(jìn)行合并的時(shí)候本質(zhì)合并的就是commit串,這樣看似是合理的,但是它和“branch的本質(zhì)是指向commit的引用”這句定義不太相符,這句話在我的理解中就是branch只指向單個(gè)commit。

還請(qǐng)各路大神幫助我理解一下,謝謝?。?/p>

回答
編輯回答
我以為
2018年4月8日 09:26
編輯回答
礙你眼

branch只指向單個(gè)commit這個(gè)理解是對(duì)的。

之所以你的理解是將branch理解成一個(gè)commit串,是因?yàn)間it存儲(chǔ)這些commit是一個(gè)樹(shù)結(jié)構(gòu),用來(lái)描述他們的先后關(guān)系

用代碼來(lái)解釋一下:

function Branch(name, commit) {
    this.name = name;
    this.commit = commit;
}

function Git(name) {
    this.name = name; // Repo name
    this.lastCommitId = -1; // Keep track of last commit id.
    this.HEAD = null; // Reference to last Commit.

    var master = new Branch('master', null); // null is passed as we don't have any commit yet.
        this.HEAD = master;
}

Git.prototype.commit = function (message) {
    // Increment last commit id and pass into new commit.
    var commit = new Commit(++this.lastCommitId, this.HEAD.commit, message);

    // Update the current branch pointer to new commit.
    this.HEAD.commit = commit;

    return commit;
};
2018年3月22日 16:15