4、Git多人单分支集成协作时的常见场景
1.1、不同人修改了不同文件如何处理
# user: git本地使用laowang,github使用dy201
# 本地clone一个仓库 git_learning_02
k@k-PC MINGW64 /user/dy201/101-GitRunner
$ git clone git@github.com:dy201/git_learning.git git_learning_02;
Cloning into 'git_learning_02'...
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
remote: Enumerating objects: 38, done.
remote: Counting objects: 100% (38/38), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 38 (delta 7), reused 32 (delta 7), pack-reused 0
Receiving objects: 100% (38/38), 4.39 KiB | 179.00 KiB/s, done.
Resolving deltas: 100% (7/7), done.
# 查看 git_learning_02 分支情况
$ git branch -va
* master 65ac276 Merge remote-tracking branch 'github/master'
remotes/origin/HEAD -> origin/master
remotes/origin/css-fix c412e25 Backgroud: orange -> green
remotes/origin/feature/add_git_commands 65ac276 Merge remote-tracking branch 'github/master'
remotes/origin/master 65ac276 Merge remote-tracking branch 'github/master'
remotes/origin/temp e722d48 second time vim
# 依赖远端创建feature/add_git_commands分支并切换到该分支
$ git checkout -b feature/add_git_commands origin/feature/add_git_commands
Switched to a new branch 'feature/add_git_commands'
Branch 'feature/add_git_commands' set up to track remote branch 'feature/add_git_commands' from 'origin'.
# update该仓库内容(user是github的用户)
$ git status
On branch feature/add_git_commands
Your branch is up to date with 'origin/feature/add_git_commands'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.md
#将本地修改push到远端
$ git push
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 316 bytes | 39.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:dy201/git_learning.git
65ac276..d83a5b6 feature/add_git_commands -> feature/add_git_commands
# 切换到本地仓库 git_learning
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (master)
$ git branch -av
css-fix c412e25 Backgroud: orange -> green
* master 65ac276 Merge remote-tracking branch 'github/master'
temp e722d48 second time vim
remotes/github/css-fix c412e25 Backgroud: orange -> green
remotes/github/master 65ac276 Merge remote-tracking branch 'github/master'
remotes/github/temp e722d48 second time vim
remotes/zhineng/temp e722d48 second time vim
# fetch远端仓库
$ git fetch github
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:dy201/git_learning
* [new branch] feature/add_git_commands -> github/feature/add_git_commands
# 根据远端仓库,创建新分支
$ git checkout -b feature/add_git_commands github/feature/add_git_commands
Switched to a new branch 'feature/add_git_commands'
Branch 'feature/add_git_commands' set up to track remote branch 'feature/add_git_commands' from 'github'.
# 修改 git_learning的index, _2的readme.md
# _02的push到远端
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning_02 (feature/add_git_commands)
$ git push
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 307 bytes | 61.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:dy201/git_learning.git
d83a5b6..b6b96cc feature/add_git_commands -> feature/add_git_commands
#将git_learning的新分支push到远端
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (feature/add_git_commands)
$ git push github
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
To github.com:dy201/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:dy201/git_learning.git'
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.
# fetch远端的仓库
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (feature/add_git_commands)
$ git fetch github
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
From github.com:dy201/git_learning
d83a5b6..b6b96cc feature/add_git_commands -> github/feature/add_git_commands
# 查看分支情况
$ git branch -av
css-fix c412e25 Backgroud: orange -> green
* feature/add_git_commands 47dad32 [ahead 1, behind 1] test different user to update the same project
master 65ac276 Merge remote-tracking branch 'github/master'
temp e722d48 second time vim
remotes/github/css-fix c412e25 Backgroud: orange -> green
remotes/github/feature/add_git_commands b6b96cc fix readme.md
remotes/github/master 65ac276 Merge remote-tracking branch 'github/master'
remotes/github/temp e722d48 second time vim
remotes/zhineng/temp e722d48 second time vim
#分支进行合并
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (feature/add_git_commands)
$ git merge github/feature/add_git_commands
Merge made by the 'recursive' strategy.
readme.md | 2 ++
1 file changed, 2 insertions(+)
# push到github
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (feature/add_git_commands)
$ git push github
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
Enumerating objects: 9, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 633 bytes | 70.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:dy201/git_learning.git
b6b96cc..9ecaa51 feature/add_git_commands -> feature/add_git_commands
1.2、不同人修改了同文件的不同区域如何处理
# 修改 02的 index
# 修改 的 index
# 提交 git_learning 到 github
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (feature/add_git_commands)
$ git push github
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 75.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:dy201/git_learning.git
9ecaa51..decebd1 feature/add_git_commands -> feature/add_git_commands
# 先fetch 后merge (遇到了问题,根据提示直接pull)
$ git merge origin/feature/add_git_commands
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
#读取 index文件(内容正确)
$ cat index.html
this is a index.html
this is second time
this third time : diff between commit and HEAD
<<<<<<< HEAD
02 -> non fast forward
=======
test different user to update project!
-> non fast forward
>>>>>>> origin/feature/add_git_commands
# push到远端
$ git push
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 701 bytes | 116.00 KiB/s, done.
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To github.com:dy201/git_learning.git
decebd1..783e619 feature/add_git_commands -> feature/add_git_commands
1.3、不同人修改了通文件的同一区域如何处理
# pull 后产生一个状态
$ git status
On branch feature/add_git_commands
Your branch and 'github/feature/add_git_commands' have diverged,
and have 1 and 3 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
# 进行提交后,进行push
1.4、同时变更了文件名和文件内容如何处理
#index.html修改为index.htm
#01中修改html中的内容
#先push修改文件夹的
#在01中push失败,然后进行pull操作。
#发现它会自动识别
#查看其中内容,和修改的一致
#更新两个库的内容
1.5、把同一文件改成不同的文件名如何处理
#两个用户分别修改文件index.htm,为 1和2
#提交1,同步到远端
#提交2
$ git push
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
To github.com:dy201/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:dy201/git_learning.git'
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.
#2进行pull
$ git pull
Enter passphrase for key '/c/Users/k/.ssh/id_rsa':
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
From github.com:dy201/git_learning
bcf3a11..b3448a4 feature/add_git_commands -> origin/feature/add_git_commands
Updating bcf3a11..b3448a4
Fast-forward
index.htm => index1.htm | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.htm => index1.htm (100%)
#修改后提交