Thursday, September 6, 2018

Git Talk 02 - Difference between "git clone", "git pull" and "git fetch"

"git clone", "git pull" & "git fetch" have similar functionality which is copying the contents from remote repository to local. However, they have different cases.

"git clone"

When you build a new local directory and would like to copy the whole remote repository content to local, "git clone" should be used. So "git clone" is used after the "git init" of the local directory.


"git pull"

"git pull" will copy the update from the remote repository to local and merge to the current branch.

First, we need add the remote repository:


git remote add origin https://github.com/xxx/test.git


Then we can "git pull" the contents from the remote repo to local and merge to the current branch:


[root@localhost test01]# git pull origin
Updating 00d0c77..b5fd00c
Fast-forward
 test.txt   | 7 +++++++
 test01.txt | 4 ++++
 test05.txt | 1 +
 3 files changed, 12 insertions(+)
 create mode 100644 test01.txt
 create mode 100644 test05.txt


"git fetch"

"git fetch" will copy the update in the remote repo to local branch. But it will NOT merge the update to the current branch.



git fetch origin master:tmp
git merge tmp


The above commands will create a new branch call "tmp" and then copy the update to "tmp" branch. Then it merge the "tmp" branch to the master. So "git fetch" will only download the update from the remote repo. But it will not do merge. The "merge" will need to be done manually. 

In summary, "git clone" is used after you initialing the local directory for remote repo. "git fetch" will only download the update of the remote repo to local branch but merge will not happen automatically. "git pull" is equal to "git fetch" + "git merge".

No comments:

Post a Comment

NSX Load Balancer "Application Rules" Examples:

Load Balancing is one of the features provided by the NSX Edge Services Gateway (ESG). It can provide L7 Load Balancing by utilizing the HA...