真正理解 git fetch, git pull 以及 FETCH_HEAD

真正理解 git fetch, git pull 

要講清楚git fetch,git pull,必須要附加講清楚git remote,git merge 、遠程repo, branch 、 commit-id 以及 FETCH_HEAD。

1. 【git remote】首先, git是一個分佈式的結構,這意味着本地和遠程是一個相對的名稱。

本地的repo倉庫要與遠程的repo配合完成版本對應必須要有 git remote子命令,通過git remote add來添加當前本地長度的遠程repo, 有了這個動作本地的repo就知道了當遇到git push 的時候應該往哪裏提交代碼。

2. 【git branch】其次,git天生就是爲了多版本分支管理而創造的,因此分支一說,不得不提, 分支就相當於是爲了單獨記錄軟件的某一個發佈版本而存在的,既然git是分佈式的,便有了本地分支和遠程分支一說,git branch 可以查看本地分支, git branch -r  可以用來查看遠程分支。 本地分支和遠程分支在git push 的時候可以隨意指定,交錯對應,只要不出現版本從圖即可。

3. 【git merge】再者,git的分佈式結構也非常適合多人合作開發不同的功能模塊,此時如果每個人都在其各自的分支上開發一個相對獨立的模塊的話,在每次release製作時都需先將各成員的模塊做一個合併操作,用於合併各成員的工作成果,完成集成。 此時需要的就是git merge.

4.【git push 和 commit-id】在每次本地工作完成後,都會做一個git commit 操作來保存當前工作到本地的repo, 此時會產生一個commit-id,這是一個能唯一標識一個版本的序列號。 在使用git push後,這個序列號還會同步到遠程repo。

在理解了以上git要素之後,分析git fetch 和 git pull 就不再困難了。 

 

首先,git fetch 有四種基本用法

1. git fetch            →→ 這將更新git remote 中所有的遠程repo 所包含分支的最新commit-id, 將其記錄到.git/FETCH_HEAD文件中

                        今天從數據庫git fetch 下(本地和遠程版本庫只有master分支),本地切換到master分支,然後git merge 不了

                    使用git merge FETCH_HEAD ,ok了

2. git fetch remote_repo         →→ 這將更新名稱爲remote_repo 的遠程repo上的所有branch的最新commit-id,將其記錄。 

3. git fetch remote_repo remote_branch_name        →→ 這將這將更新名稱爲remote_repo 的遠程repo上的分支: remote_branch_name

4. git fetch remote_repo remote_branch_name:local_branch_name       →→ 這將這將更新名稱爲remote_repo 的遠程repo上的分支: remote_branch_name ,並在本地創建local_branch_name 本地分支保存遠端分支的所有數據。

git pull 的運行過程

git pull : git fetch 當前指向的遠程分支的數據,然後再將其與本地的當前分支合併。

  

FETCH_HEAD is a short-lived ref, to keep track of what has just been fetched from the remote repository. git pull first invokes git fetch, in normal cases fetching a branch from the remote; FETCH_HEAD points to the tip of this branch (it stores the SHA1 of the commit, just as branches do). git pull then invokes git merge, merging FETCH_HEAD into the current branch.

The result is exactly what you'd expect: the commit at the tip of the appropriate remote branch is merged into the commit at the tip of your current branch.

This is a bit like doing git fetch without arguments (or git remote update), updating all your remote branches, then running git merge origin/<branch>, but using FETCH_HEAD internally instead to refer to whatever single ref was fetched, instead of needing to name things.

Reference :  

1.https://ruby-china.org/topics/4768

2.http://stackoverflow.com/questions/9237348/what-does-fetch-head-in-git-mean

w.cnblogs.com/ToDoToTry/p/4095626.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章