git fetch - git merge - git pull 指令
git fetch - git merge - git pull 指令
Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
在默認模式下,git pull 命令是 git fetch 和 git merge FETCH_HEAD 命令的組合,git pull = git fetch + git merge FETCH_HEAD,將遠程存儲庫中的更改合并到當前分支中。pull 指令其實就是去 a remote repository 抓東西下來 (fetch),并且更新 current branch 的進度 (merge)。
1 git fetch 指令
yongqiang@yongqiang:~$ git fetch --help references,refs:引用 reference specification,refspec:具體的引用 origin:遠程倉庫鏈接標記名,遠程倉庫鏈接別名Download objects and refs from another repository.
從另一個存儲庫下載對象和引用。
Fetch branches and/or tags (collectively, refs) from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated.
從一個或多個其它存儲庫中獲取分支和/或標簽 (統稱為 refs),以及使其歷史完整所需的對象。遠程跟蹤分支已更新,將這些更新取回本地,就要用到 git fetch 指令。
git fetch can fetch from either a single named repository or URL, or from several repositories at once if <group> is given and there is a remotes.<group> entry in the configuration file.
git fetch 可以從單個命名存儲庫或 URL 獲取,或者如果給定 <group> 并且配置文件中有 remotes.<group> 條目,則可以同時從多個存儲庫獲取。
The above command copies all branches from the remote refs/heads/ namespace and stores them to the local refs/remotes/origin/ namespace, unless the branch.<name>.fetch option is used to specify a non-default refspec.
上述命令從遠程 refs/heads/ namespace 復制所有分支,并將它們存儲到本地的 refs/remotes/origin/ namespace 中,除非使用 branch.<name>.fetch 選項來指定非默認的 refspec。
This updates (or creates, as necessary) branches pu and tmp in the local repository by fetching from the branches (respectively) pu and maint from the remote repository.
通過從遠程存儲庫的分支 (分別) 獲取 pu and maint 來更新 (或根據需要創建) 本地存儲庫中的分支 pu and tmp。
The pu branch will be updated even if it does not fast-forward, because it is prefixed with a plus sign; tmp will not be.
pu 分支即使不 fast-forward (快進) 合并也會被更新,因為它有一個加號前綴,tmp 不會。
The first command fetches the maint branch from the repository at git://git.kernel.org/pub/scm/git/git.git and the second command uses FETCH_HEAD to examine the branch with git log. The fetched objects will eventually be removed by git’s built-in housekeeping.
第一個命令從位于 git://git.kernel.org/pub/scm/git/git.git 的存儲庫中獲取 maint 分支,第二個命令使用 FETCH_HEAD 通過 git log 檢查分支,獲取的對象最終將被 git 的內置管家刪除。
將遠程倉庫的更新,全部取回本地。默認情況下,git fetch 取回所有分支的更新。
git branch -r 查看遠程分支,git branch -a 查看所有分支,本地的當前分支是 master,遠程分支是 origin/master。
yongqiang@yongqiang:~/yongqiang_work/darknet$ git status On branch master Your branch is up to date with 'origin/master'.nothing to commit, working tree clean yongqiang@yongqiang:~/yongqiang_work/darknet$ yongqiang@yongqiang:~/yongqiang_work/darknet$ git branch -rorigin/HEAD -> origin/masterorigin/masterorigin/tjluyao-masterorigin/yolov3 yongqiang@yongqiang:~/yongqiang_work/darknet$ yongqiang@yongqiang:~/yongqiang_work/darknet$ git branch -a * masterremotes/origin/HEAD -> origin/masterremotes/origin/masterremotes/origin/tjluyao-masterremotes/origin/yolov3 yongqiang@yongqiang:~/yongqiang_work/darknet$將遠程 origin 的 master 分支代碼拉取下來,在本地要用 “遠程倉庫鏈接標記名/分支名” 的形式讀取。遠程 origin 的 master 分支,在本地可以用 origin/master 讀取。
取回遠程更新以后,可以在它的基礎上,使用 git checkout 命令創建一個新的分支。在 origin/master 的基礎上,創建一個新分支 yongqiang。
$ git checkout -b yongqiang origin/master使用 git merge 命令或者 git rebase 命令,在本地分支上合并遠程分支 origin/master。
$ git merge origin/master 等同于 $ git rebase origin/master $ git fetch origin master # 從遠程 origin 的 master 分支下載最新的版本到 origin/master 分支上 $ git log -p master..origin/master # 比較本地的 master 分支和 origin/master 分支的差別 $ git merge origin/master # 進行合并上述過程可以用更清晰的方式來進行:
$ git fetch origin master:tmp $ git diff tmp $ git merge tmp1.1 Example
yongqiang@yongqiang:~$ mkdir yongqiang_work yongqiang@yongqiang:~$ cd yongqiang_work/ yongqiang@yongqiang:~/yongqiang_work$ git clone https://github.com/pjreddie/darknet.git Cloning into 'darknet'... remote: Enumerating objects: 5955, done. remote: Total 5955 (delta 0), reused 0 (delta 0), pack-reused 5955 Receiving objects: 100% (5955/5955), 6.37 MiB | 1.34 MiB/s, done. Resolving deltas: 100% (3932/3932), done. yongqiang@yongqiang:~/yongqiang_work$ cd darknet/ yongqiang@yongqiang:~/yongqiang_work/darknet$ git status On branch master Your branch is up to date with 'origin/master'.nothing to commit, working tree clean yongqiang@yongqiang:~/yongqiang_work/darknet$ git fetch yongqiang@yongqiang:~/yongqiang_work/darknet$此時執行 git fetch 指令沒有任何信息,因為本地的進度跟線上的是一樣的。
在 GitHub 網站上,直接線上編輯某個文件。在右上角會有個編輯的按鈕,按下下方的 Commit changes 進行存檔并新增一次 Commit,這樣線上版本的 Commit 數就領先本機一次了。
此時再次執行 git fetch 指令:
$ git fetch remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:kaochenlong/practice-git85e848b..8c3a0a5 master -> origin/mastergit fetch 指令執行前:
本地 HEAD & master 同遠程 origin/HEAD & origin/master 保持一致。
git fetch 指令執行后:
遠程 origin/HEAD & origin/master 比本地 HEAD & master 多一個 commit。
2 git merge 指令
yongqiang@yongqiang:~$ git merge --helpJoin two or more development histories together.
將兩個或兩個以上的開發歷史合并一起。
Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used by git pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another.
將 the named commits 的更改 (自從它們的歷史與當前分支不同時起) 合并到當前分支中。git pull 使用此命令合并來自另一個存儲庫的更改,并且可以手動使用此命令將更改從一個分支合并到另一個分支。
- Merge branches fixes and enhancements on top of the current branch, making an octopus merge (在當前分支之上合并分支“fixes”和“enhancements”,進行章魚合并)
- Merge branch obsolete into the current branch, using ours merge strategy (使用 ours 的合并策略將 obsolete 分支合并到當前分支中)
- Merge branch maint into the current branch, but do not make a new commit automatically (將分支 maint 合并到當前分支,但不自動進行新提交)
This can be used when you want to include further changes to the merge, or want to write your own merge commit message.
當您想要包含對合并的進一步更改,或者想要編寫您自己的合并提交消息時,可以使用它。
You should refrain from abusing this option to sneak substantial changes into a merge commit. Small fixups like bumping release/version name would be acceptable.
你應該避免濫用此選項將大量更改出其不意的合并提交中。可以接受小的修復,例如修改發布/版本名稱。
2.1 Example
為了使本地 HEAD & master 同遠程 origin/HEAD & origin/master 保持一致,執行 git merge 指令:
$ git merge origin/master Updating 85e848b..8c3a0a5 Fast-forwardREADME.md | 2 ++1 file changed, 2 insertions(+)因為 origin/master 分支跟 master 分支本是同根生,所以在上面合并的過程可以看到是使用快轉模式 (Fast Forward) 方式進行。
3 git pull 指令
命令格式:
git pull <遠程倉庫鏈接標記名> <遠程分支名>:<本地分支名> yongqiang@yongqiang:~$ git pull --help如果你有一個分支設置為跟蹤一個遠程分支,可以使用 git pull 命令來自動的抓取然后合并遠程分支到當前分支。默認情況下,git clone 命令會自動設置本地 master 分支跟蹤克隆的遠程倉庫的 master 分支,即本地的 master 分支自動追蹤 origin/master 分支。運行 git pull 通常會從最初克隆的服務器上抓取數據并自動嘗試合并到當前所在的分支。
yongqiang@yongqiang:~/yongqiang_work/darknet$ git status On branch master Your branch is up to date with 'origin/master'.nothing to commit, working tree clean yongqiang@yongqiang:~/yongqiang_work/darknet$ yongqiang@yongqiang:~/yongqiang_work/darknet$ git remote -v origin https://github.com/pjreddie/darknet.git (fetch) origin https://github.com/pjreddie/darknet.git (push) yongqiang@yongqiang:~/yongqiang_work/darknet$ yongqiang@yongqiang:~/yongqiang_work/darknet$ git pull Already up to date. yongqiang@yongqiang:~/yongqiang_work/darknet$ yongqiang@yongqiang:~/yongqiang_work/darknet$ git pull origin Already up to date. yongqiang@yongqiang:~/yongqiang_work/darknet$ yongqiang@yongqiang:~/yongqiang_work/darknet$ git pull origin master From https://github.com/pjreddie/darknet* branch master -> FETCH_HEAD Already up to date. yongqiang@yongqiang:~/yongqiang_work/darknet$將遠程 origin 的 master 分支代碼拉取下來,與本地當前分支代碼合并。如果當前分支與遠程分支存在追蹤關系,git pull 就可以省略遠程分支名。
$ git pull $ git pull origin $ git pull origin master將遠程 origin 的 qiang 分支代碼拉取下來,與本地當前分支代碼合并:
$ git pull origin qiang 等同于 $ git fetch origin $ git merge origin/qiang將遠程 origin 的 master 分支代碼拉取下來,與本地 develop 分支代碼合并:
$ git pull origin master:develop本地的 master 分支追蹤遠程 origin/qiang 分支:
$ git branch --set-upstream master origin/next當 git fetch 命令從服務器上抓取本地沒有的數據時,它并不會修改工作目錄中的內容。它只會獲取數據然后讓你自己合并。git pull 在大多數情況下的含義是一個 git fetch 緊接著一個 git merge 命令。如果有一個設置好的跟蹤分支,不管它是顯式地設置還是通過 git clone 或 git checkout 命令為你創建的,git pull 都會查找當前分支所跟蹤的服務器與分支,從服務器上抓取數據然后嘗試合并入那個遠程分支。
strong@foreverstrong:~/project_work/airport_project$ git pull Username for 'https://github.com': yqcheng@deepnorth.cn Password for 'https://yqcheng@deepnorth.cn@github.com': remote: Enumerating objects: 35, done. remote: Counting objects: 100% (35/35), done. remote: Compressing objects: 100% (9/9), done. remote: Total 25 (delta 16), reused 25 (delta 16), pack-reused 0 Unpacking objects: 100% (25/25), done. From https://github.com/DeepNorthAI/airport_projectdceebe9..d2751f3 master -> origin/master Updating dceebe9..d2751f3 Fast-forwardabove_the_wing/configs/vest_classify_cfgs.py | 5 ++above_the_wing/examples/action_recognition_demo.py | 57 ++++++++++++++++------.../examples/vest_classification_demo.py | 15 ++++--above_the_wing/src/application_util/processing.py | 12 +++++4 files changed, 70 insertions(+), 19 deletions(-) strong@foreverstrong:~/project_work/airport_project$在實際使用中,git fetch 更安全一些,因為在 merge 前,我們可以查看更新情況,然后再決定是否合并。
3.1 git pull --rebase
More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch. With --rebase, it runs git rebase instead of git merge.
在執行 git pull 指令的時候,可以再加上 --rebase 參數,它在 fetch 完成之后,便會使用 rebase 方式進行合并。
命令格式:
git pull --rebase <遠程倉庫鏈接標記名> <遠程分支名>:<本地分支名> $ git pull --rebase在多人共同開發的時候,大家各自在自己的分支進行 commit,所以拉回來用一般的方式合并的時候常會產生為了合并而產生的額外 commit。為了合并而產生的這個 commit 本身并沒有什么問題,但如果你不想要這個額外的 commit,可考慮使用 rebase 方式來進行合并。
References
https://yongqiang.blog.csdn.net/
(日) 大塚弘記 著, 支鵬浩, 劉斌 譯. GitHub 入門與實踐[M]. 北京:人民郵電出版社, 2015. 1-255
https://gitbook.tw/
https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
總結
以上是生活随笔為你收集整理的git fetch - git merge - git pull 指令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对计算机辅助英语教学的建议,CALL引入
- 下一篇: flink 复postgresql数据库