Git: Pull the latest code branch from the remote warehouse and merge it into the local branch

In Git, you can use the git fetchand git pullcommands to pull updates from remote repositories.

git fetch

git fetchCommands allow you to see updates before merging your code, allowing you to better manage your code and resolve possible merge conflicts.
Of course, you can choose to create a new branch locally to pull the remote update (also called a "temporary" branch), and then merge this new branch into your working branch.

Alternatively, you can choose to pull remote updates directly on your working branch.

git Pull

git pullThe command will immediately pull the updates from the remote repository and merge them into your local branch. This can lead to merge conflicts in some cases, so if you need more control or want to see updates before merging, the git fetch+ git mergeway is better.

The commands are explained below:

1.git fetch + merge: Get the latest code to the local, and then manually merge the branch

Create additional local branches

The command shows:

//查看当前远程的版本
git remote -v
//获取最新代码到本地临时分支(本地当前分支为[branch],获取的远端的分支为[origin/branch])
git fetch origin master:master1      [示例1:在本地建立master1分支,并下载远端的origin/master分支到master1分支中]
git fetch origin dev:dev1     [示例2:在本地建立dev1分支,并下载远端的origin/dev分支到dev1分支中]
//查看版本差异
git diff master1    [示例1:查看本地master1分支与当前分支的版本差异]
git diff dev1         [示例2:查看本地dev1分支与当前分支的版本差异]
//合并最新分支到本地分支
git merge master1    [示例1:合并本地分支master1到当前分支]
git merge dev1   [示例2:合并本地分支dev1到当前分支]
//删除本地临时分支
git branch -D master1    [示例1:删除本地分支master1]
git branch -D dev1   [示例1:删除本地分支dev1]

Note: operate carefully, this method is recommended

2. Do not create additional local branches

The command shows:

//查询当前远程的版本
git remote -v
//获取最新代码到本地(本地当前分支为[branch],获取的远端的分支为[origin/branch])
git fetch origin master  [示例1:获取远端的origin/master分支]
git fetch origin dev [示例2:获取远端的origin/dev分支]
//查看版本差异
git log -p master..origin/master [示例1:查看本地master与远端origin/master的版本差异]
git log -p dev..origin/dev   [示例2:查看本地dev与远端origin/dev的版本差异]
//合并最新代码到本地分支
git merge origin/master  [示例1:合并远端分支origin/master到当前分支]
git merge origin/dev [示例2:合并远端分支origin/dev到当前分支]

Note: This method is recommended

2.git pull: Get the latest code locally and automatically merge it into the current branch

The command shows:

//查询当前远程分支
git remote -v
//直接拉取并合并最新代码
git pull origin master    [示例1:拉取远端origin/master分支合并到本地当前分支]
git pull origin dev        [示例2:拉取远端origin/dev分支合并到本地当前分支]

Note: This method is not recommended, because it is a direct merge and cannot handle conflicts in advance.

Guess you like

Origin blog.csdn.net/weixin_38428126/article/details/131760189