git-pull

发布时间 2023-07-13 17:46:25作者: lxd670

1.pull说明

用于从远程更新仓库的本地版本

  • 更新当前本地工作分支(当前签出分支)
  • 更新所有其他分支的远程跟踪分支

2.拉取原理

2-1.git pull原理

git pull=git fetch+git merge FETCH_HEAD

2-1-1.拉取远程master到FETCH_HEAD

git fetch origin master
From gitlab.xxxxx-inc.com:xxx/test1
 * branch            master        -> FETCH_HEAD

2-1-2.查看FETCH_HEAD指向

cat .git/FETCH_HEAD 
0e67d2e134a2d9ad3c3ec30464486d4981138f78		branch 'master' of gitlab.xxxxx-inc.com:xxx/test1

2-1-3.合并FETCH_HEAD

git merge FETCH_HEAD 
Updating 52cb5f1..0e67d2e
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

2-2.git pull -r原理

git pull -r=git fetch+git rebase FETCH_HEAD

-r=--rebase

2-2-1.拉取远程master到FETCH_HEAD

git fetch origin master
From gitlab.xxxxx-inc.com:xxx/test1
 * branch            master        -> FETCH_HEAD

2-2-2.查看FETCH_HEAD指向

cat .git/FETCH_HEAD 
0e67d2e134a2d9ad3c3ec30464486d4981138f78		branch 'master' of gitlab.xxxxx-inc.com:xxx/test1

2-2-3.变基FETCH_HEAD

git rebase FETCH_HEAD 
Successfully rebased and updated refs/heads/master.

3.拉取指定分支

将远程主机origin的dev分支拉取过来,与本地的ccc分支合并

3-1.方式一

3-1-1.查看分支

git br
* master

3-1-2.拉取指定分支

git pull origin dev:ccc
From gitlab.xxxxx.com:xxx/test1
 * [new branch]      dev        -> ccc
Updating 52cb5f1..0e67d2e
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

3-1-3.查看分支

git br
  ccc
* master

3-2.方式二

3-2-1.查看分支

git br
* master

3-2-2.使用fetch拉取创建

git fetch origin dev:ccc
From gitlab.xxxxx.com:xxx/test1
 * [new branch]      dev        -> ccc

3-2-3.查看分支

git br
  ccc
* master

4.git pull和git pull -r区别

远程分支有新的提交,本地分支没有新提交时,git pullgit pull -r效果是一样的(git-merge原理)

远程分支有新的提交,本地分支也有新的提交时,git pull会生成一条合并(merge)记录

4-1.git pull案列

4-1-1.查看日志

git logs
acec4ae (add b.txt, 2023-07-13)
0480eb9 (add a.txt, 2023-07-13)
bbd2664 (Add new file, 2023-07-13)

4-1-2.直接拉取

git pull

4-1-3.进入合并确认

Merge branch 'master' of gitlab.xxxxx.com:xxx/test_demo

* 'master' of gitlab.xxxxx.com:xxx/test_demo:
  add aa.txt
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

4-1-4.合并完成

remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 247 bytes | 247.00 KiB/s, done.
From gitlab.xxxxx.com:xxx/test_demo
   0480eb9..33ff90a  master     -> origin/master
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
Merge made by the 'recursive' strategy.
 aa.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aa.txt

4-1-5.查看日志

会生成一条Merge记录

git logs
d1ec83e (Merge branch 'master' of gitlab.xxxxx.com:xxx/test_demo, 2023-07-13)
acec4ae (add b.txt, 2023-07-13)
33ff90a (add aa.txt, 2023-07-13)
0480eb9 (add a.txt, 2023-07-13)
bbd2664 (Add new file, 2023-07-13)

4-1.-6查看图形化日志

git log --oneline --graph
*   d1ec83e (HEAD -> master) Merge branch 'master' of gitlab.xxxxx.com:xxx/test_demo
|\  
| * 33ff90a (origin/master, origin/HEAD) add aa.txt
* | acec4ae add b.txt
|/  
* 0480eb9 add a.txt
* bbd2664 Add new file

4-2.git pull -r

4-2-1.查看日志

git logs
acec4ae (add b.txt, 2023-07-13)
0480eb9 (add a.txt, 2023-07-13)
bbd2664 (Add new file, 2023-07-13)

4-2-2.直接rebase拉取

git pull -r
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 247 bytes | 247.00 KiB/s, done.
From gitlab.xxxxx.com:xxx/test_demo
   0480eb9..33ff90a  master     -> origin/master
Successfully rebased and updated refs/heads/master.

4-2-3.查看日志

git logs
5dd65ff (add b.txt, 2023-07-13)
33ff90a (add aa.txt, 2023-07-13)
0480eb9 (add a.txt, 2023-07-13)
bbd2664 (Add new file, 2023-07-13)

4-2-4.查看图形化日志

git log --oneline --graph 
* 5dd65ff (HEAD -> master) add b.txt
* 33ff90a (origin/master, origin/HEAD) add aa.txt
* 0480eb9 add a.txt
* bbd2664 Add new file