Git Fetch & Pull

1, a brief summary

Git fetch and Git pull

It can be summarized as follows:

Git fetch is to pull the latest content from the remote host to the local, which the user checks and decides whether to merge into the working native branch.

Git pull is used to pull down the latest contents of the remote host and merge them, that is, git pull = git fetch + git merge. Conflicts may occur and need to be resolved manually.

Git fetch and Git pull \

2. The concept of branching

Branches are used to mark the submission of specific code. Each branch is identified by a SHA1sum value, so the operation on branches is lightweight, and all you change is the SHA1sum value.

As shown in the figure below, there are currently two branches: A,C, and E belong to the master branch, while A,B, D, and F belong to the dev branch.

A----C----E (Master) \ B-- D-- F(dev)Copy the code

Their head Pointers point to E and F respectively, so do the following:

Git merge dev // Merge the dev branch into the master branchCopy the code

After completion of the merger:

A-- C-- E-- G(master) \ / B-- D-- F (dev)Copy the code

Now ABCDEFG belongs to the master. G is the result of a merge. It is the result of merging the E and F codes. ABDF is still part of the Dev branch. You can continue development on the dev branch:

A-- C-- E-- G-- H(master) \ / B-- D-- F-- I (dev)Copy the code

The basic operations of a branch:

git branch // View all local branches

git branch -r // View all remote branches

git branch -a // View all local and remote branches

git branch <branchname> // Create a new branch

git branch -d <branchname> // Delete the local branch

git branch -d -r <branchname> // Delete the remote branch and push it to the server
git push origin:<branchname>  // Delete and push to the server

git branch -m <oldbranch> <newbranch> // Rename the local branch
/** * Rename remote branch: *1, delete remote branch to be modified *2, push local new branch to remote server */

// Git has some options:

-d
--delete: Delete -d --delete-f --force: force -m --move: move or rename -m --move --force -r --remote: remote -A --all: allCopy the code

 

Git fetch

Git fetch command:

$git fetch < remote host name > // This command will fetch all updates from a remote hostCopy the code

If you only want to fetch updates for a particular branch, you can specify the branch name:

$git fetch < remote host name > < branch name > // Note that there are Spaces between themCopy the code

The most common command is fetching the master branch of the Origin host:

$ git fetch origin master
Copy the code

When the update is fetched, a FETCH_HEAD is returned, which refers to the latest status of a branch on the server and can be used locally to view the updated information that was just fetched:

$ git log -p FETCH_HEAD
Copy the code

As shown in figure:

You can see that the information returned includes the file name of the update, the author and time of the update, and the code of the update (19 lines of red [delete] and green [add] sections).

We can use this information to determine if conflicts have occurred to determine whether updates are merged into the current branch.

4. Git pull

The process of git pull can be understood as:

Git merge FETCH_HEAD Git merge FETCH_HEAD git merge FETCH_HEAD git merge FETCH_HEAD git merge FETCH_HEADCopy the code

To fetch an update from a branch of a remote host and merge it with a locally specified branch, the complete format can be expressed as:

$git pull = $git pull = $git pullCopy the code

If the remote branch is merged with the current branch, the part after the colon can be omitted:

$ git pull origin next
Copy the code

FETCH_HEAD can be understood as a temporary branch that stores the collection of changes just fetched (because that branch of a FETCH might change more than once).