1.git fetch
用户一:本地初始化项目,创建文件,保存本地仓库,提交远程仓库
$ git init
$ touch file.txt
$ git add .
$ git commit -m "创建了file.txt文件"
[master (root-commit) 4dcee36] 创建了file.txt文件
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file.txt
$ git remote add origin git@gitee.com:RzgLL/test.git
$ git push origin master:master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 229 bytes | 229.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:RzgLL/test.git
* [new branch] master -> master
用户二:clone代码到本地
$ git clone **
//然后bash切换进项目,并且修改file.txt 文件
$ cat file.txt
用户二:修改了代码
//提交到本地仓库,继续提交到远端
$ git add .
$ git commit -m "用户二修改代码01"
[master 5aff391] 用户二修改代码01
1 file changed, 2 insertions(+)
$ git push origin master:master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:RzgLL/test.git
4dcee36..5aff391 master -> master
用户一:fetch 远端的仓库到本地
$ git fetch origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 279 bytes | 3.00 KiB/s, done.
From gitee.com:RzgLL/test
* branch master -> FETCH_HEAD
4dcee36..5aff391 master -> origin/master
$ git log --oneline --graph
* 4dcee36 (HEAD -> master) 创建了file.txt文件
查看本地版本库,并没有用户二提交的一版。pull的时候提示:* branch master -> FETCH_HEAD 应该是远端的master分支到了本地的FETCH_HEAD分支,至于要将FETCH_HEAD分支和当前分支合并即可
$ git merge FETCH_HEAD
Updating 4dcee36..5aff391
Fast-forward
file.txt | 2 ++
1 file changed, 2 insertions(+)
$ git log --oneline --graph //再次查看,出现了用户二修改的代码
* 5aff391 (HEAD -> master, origin/master) 用户二修改代码01
* 4dcee36 创建了file.txt文件
2.git pull = fetch + merge
用户二继续修改代码并且提交
$ vim file.txt
$ cat file.txt
用户二:修改了代码
用户二:又修改了代码
$ git add .
$ git commit -m "用户二修改了代码02"
[master 65c2619] 用户二修改了代码02
1 file changed, 1 insertion(+), 1 deletion(-)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 309 bytes | 309.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:RzgLL/test.git
5aff391..65c2619 master -> master
用户一:
$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), 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), 289 bytes | 28.00 KiB/s, done.
From gitee.com:RzgLL/test
* branch master -> FETCH_HEAD
5aff391..65c2619 master -> origin/master
Updating 5aff391..65c2619
Fast-forward
file.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log --oneline --graph
* 65c2619 (HEAD -> master, origin/master) 用户二修改了代码02
* 5aff391 用户二修改代码01
* 4dcee36 创建了file.txt文件
pull 之后,直接合并到了master分支。
3.pull冲突
用户二修改了file文件一行代码,并且push到了远程仓库。
用户一也修改了file文件的同一行代码,然后pull远程仓库了一下。
用户二:
$ vim file.txt
$ cat file.txt
用户二:修改了代码
用户二:又修改了代码
用户二:123
$ git add .
$ git commit -m "用户二:123"
[master f454dd6] 用户二:123
1 file changed, 1 insertion(+)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 288.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:RzgLL/test.git
65c2619..f454dd6 master -> master
用户一:修改file文件同一行代码,并且pull一下
$ vim file.txt
$ cat file.txt
用户二:修改了代码
用户二:又修改了代码
用户一:456
$ git add .
$ git commit -m "用户一:456"
[master 6a1409e] 用户一:456
1 file changed, 1 insertion(+)
$ git pull origin master
* branch master -> FETCH_HEAD
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat file.txt
用户二:修改了代码
用户二:又修改了代码
<<<<<<< HEAD
用户一:456
=======
用户二:123
>>>>>>> f454dd6c1b2c3fe7842ead118861674f63b0c52f
$ vi file.txt //解决冲突
$ git add .
$ git commit -m "用户一:解决冲突"
[master 77e484d] 用户一:解决冲突
$ git log --oneline --graph
* 77e484d (HEAD -> master) 用户一:解决冲突
|\
| * f454dd6 (origin/master) 用户二:123
* | 6a1409e 用户一:456
|/
* 65c2619 用户二修改了代码02
* 5aff391 用户二修改代码01
* 4dcee36 创建了file.txt文件