Git遠(yuǎn)程協(xié)作
?基本流程
A 將本地的commits推送至 遠(yuǎn)端
B 從遠(yuǎn)端 拉取至 本地
B 修改拉取的commits,然后 推送至 遠(yuǎn)端
情景一:A 推送 B拉取
該場景下 A和B都沒有 額外的操作
?A 將本地的commits 推送至 遠(yuǎn)端
新建文件 echo ‘A add the fisrt line’ -> hello.txt
將hello.txt提交并推送至 遠(yuǎn)端
git add .
git commit -m 'A add the first line'
git push -u origin master
?B 將遠(yuǎn)端的commits 拉到 本地
git pull <remote> <branch>
remote:是遠(yuǎn)程的分支
branch:要拉到的分支
git pull包含了兩步操作:
第一:將遠(yuǎn)端的 commits 拉取到本地 fetch
第二:將本地和拉取到的 合并 merge
git pull origin master
$ git pull origin master
From 遠(yuǎn)端URL
* branch master -> FETCH_HEAD //mater 指向了 FETCH_HEAD 即 進(jìn)行了merge
//FETCH_HEAD 就是拉取到的HEAD
?B git log
Author:
Date:
A add the first line //這就是 遠(yuǎn)端的commit信息,
?流程圖
image
情景二:B將修改的文件 push 至遠(yuǎn)端
該場景下:
B 首先了拉取了 文件,并對文件進(jìn)行了 修改
然后 將修改的commit信息 推送至遠(yuǎn)端
?B 修改hello.txt文件 并將文件 commit
vi hello.txt
B add the first line
git commit -am 'B add the first line'
?B 將修改 推送至 遠(yuǎn)端
git push -u origin master
此時 git status
$ git status
On branch master
Your branch is up-to-date with 'origin/master'. //本地分支和遠(yuǎn)端是一致的
nothing to commit, working tree clean
?流程圖
image
情景三:A將修改的文件 push 至遠(yuǎn)端
此時遠(yuǎn)端已經(jīng)比A多了一次提交
?A 修改hello.txt文件 并 提交
vi hello.txt
A add the second line
git commit -am 'A add the second line'
?A push 自己的commit
$ git push
To 遠(yuǎn)程
! [rejected] master -> master (fetch first) //拒絕 首先要 fetch
error: failed to push some refs to '' //錯誤:推送至遠(yuǎn)程失敗
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
//由于遠(yuǎn)程包含了本地沒有的工作,因此 推送失敗。
//通常 是由于 別的倉庫推送了 相同的 引用。
//你可能需要 合并 遠(yuǎn)程的 改變在 push 之前
//因此遠(yuǎn)端 比A本地 多了一次 提交 并且 改變的都是同一行 內(nèi)容
//需要,處理這些相同的內(nèi)容,在push之前 就需要 先pull一下。
git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From
827ed67..2aaca86 master -> origin/master //遠(yuǎn)程的版本信息已經(jīng) fetch下來
Auto-merging hello.txt //自動合并 hello.txt
CONFLICT (content): Merge conflict in hello.txt //合并沖突 ----> 由于 修改的是同一行
Automatic merge failed; fix conflicts and then commit the result. //需要解決沖突 merge失敗
vi 命令
dd 刪除行
2,4d 刪除2,3,4行
沖突解決后
git add hello.txt
git commit
git push
?流程分析
image