4、Git多人單分支集成協作時的常見場景
阿新 • • 發佈:2019-03-29
forward ranch ror rac osi font rate .html The
1.1、不同人修改了不同文件如何處理
# user: git本地使用laowang,github使用dy201
# 本地clone一個倉庫 git_learning_02
k@k-PC MINGW64 /user/dy201/101-GitRunner
$ git clone [email protected]: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 ‘[email protected]: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、同時變更了文件名和文件內容如何處理
1.5、把同一文件改成不同的文件名如何處理
4、Git多人單分支集成協作時的常見場景