首先配置ssh密鑰
克隆項目
- 配置兩臺主機(一臺本地mac,一臺云服務器)通過這樣的方式模擬多人開發。
創建分支
[root@ ~/Git_test_多人協作和沖突合并/branch_test]$ ls
README.md
[root@ ~/Git_test_多人協作和沖突合并/branch_test]$ git branch
* master
[root@ ~/Git_test_多人協作和沖突合并/branch_test]$ git branch yun
[root@ ~/Git_test_多人協作和沖突合并/branch_test]$ git branch
* masteryun
[root@ ~/Git_test_多人協作和沖突合并/branch_test]$
轉移到分支并在分支添加項目
[root@ ~/branch_test]$ ls
README.md
[root@ ~/branch_test]$ git checkout yun
切換到分支 'yun'
[root@ ~/branch_test]$ git branchmaster
* yun
[root@ ~/branch_test]$ ls
README.md
[root@ ~/branch_test]$ touch yun1.c
[root@ ~/branch_test]$ ls
README.md yun1.c
[root@ ~/branch_test]$ git add yun1.c
[root@ ~/branch_test]$ git commit -m "yun1.c"*** Please tell me who you are.Rungit config --global user.email "you@example.com"git config --global user.name "Your Name"to set your account's default identity.
Omit --global to set the identity only in this repository.fatal: empty ident name not allowed
要提交自己的用戶名和郵箱才可以繼續
- 提交完畢后
[root@ ~/branch_test]$ git commit -m "yun1.c"
[yun eb267c4] yun1.c1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 yun1.c
沖突測試
master
- 首先使用yun提交一段代碼
[root@ ~/branch_test]$ git branch
* masteroriginyun
[root@ ~/branch_test]$ ls
README.md yun1.c
[root@ ~/branch_test]$ cat yun1.c
hello world
[root@ ~/branch_test]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To git@git.github.com:kouhaozhe/branch_test.giteb267c4..025a01e master -> master
- 這是master的yun1.c文件就有了一段代碼
yun1.c 12 Bytes
hello world
- 我們使用mac進行提交
khz:branch_test kouhz$ ls
README.md yun1.c
khz:branch_test kouhz$ vim yun1.c
khz:branch_test kouhz$ cat yun1.c
HELLO WORLDkhz:branch_test kouhz$ git add yun1.c
khz:branch_test kouhz$ git commit -m "HELLO WORLD"
[master 75da4f2] HELLO WORLD1 file changed, 1 insertion(+)
khz:branch_test kouhz$ git push
To git.github.com:kouhaozhe/branch_test.git! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@git.github.com:kouhaozhe/branch_test.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.
khz:branch_test kouhz$
沖突時我們要先git pull再git push
- git pull
- git diff 查看文件的不同
<<<<<<< HEAD
HELLO WORLD
=======
hello world
>>>>>>> 8d4d57c701bfbda71cc442caf2017c01437a80d1
其他分支
yun 分支添加文字并提交
[root@ ~/branch_test]$ git branchmaster
* yun
[root@ ~/branch_test]$ ls
README.md yun1.c
[root@ ~/branch_test]$ vim yun1.c
[root@ ~/branch_test]$ cat yun1.c
yun hello[root@ ~/branch_test]$ git add yun1.c
[root@ ~/branch_test]$ git commit -m "yun yun1.c"
[yun 4d7c92c] yun yun1.c1 file changed, 1 insertion(+), 1 deletion(-)
[root@ ~/branch_test]$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for yun, visit:
remote: http://git.github.com/kouhaozhe/branch_test/merge_requests/new?merge_request%5Bsource_branch%5D=yun
remote:
To git@git.github.com:kouhaozhe/branch_test.git025a01e..4d7c92c yun -> yun
[root@ ~/Git_test_多人協作和沖突合并/branch_test]$
- 這時網頁已經有了我們提交的帶著,我們切換到另一臺機器
另一臺電腦除了自己創建的分支,看不見另一臺電腦創建的分支,這時使用命令
khz:branch_test kouhz$ git branchmac
* master
同步之后沒有yun這個分支,操作命令
切換另一臺主機創建的分支
git checkout -b yun origin/yun
Tips:
error: you need to resolve your current index first
khz:branch_test kouhz$ git reset --merge
khz:branch_test kouhz$ git checkout -b yun origin/yun
Branch ‘yun’ set up to track remote branch ‘yun’ from ‘origin’.
Switched to a new branch ‘yun’
在進行添加代碼
khz:branch_test kouhz$ cat yun1.c
<<<<<<< HEAD
hello world
HELLO WORLD
=======
yun hello
>>>>>>> 4d7c92c5686128aa3c99c25f3dbfdbbd88f8cf5e
khz:branch_test kouhz$ vim yun1.c
khz:branch_test kouhz$ cat yun1.c
yun hello
hello world
HELLO WORLDkhz:branch_test kouhz$ git add yun1.c
khz:branch_test kouhz$ git commit -m "mac"
[yun ae006aa] mac
khz:branch_test kouhz$ git push origin yun
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 577 bytes | 577.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for yun, visit:
remote: http://git.github.com/kouhaozhe/branch_test/merge_requests/new?merge_request%5Bsource_branch%5D=yun
remote:
To git.github.com:kouhaozhe/branch_test.git4d7c92c..ae006aa yun -> yun
切換到另一臺主機
- 進行git pull操作,結果失敗了!原因是沒有指定本地dev分支與遠程origin/dev分支的鏈接
[root@ ~/branch_test]$ git pull
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
來自 git.github.com:kouhaozhe/branch_test4d7c92c..ae006aa yun -> origin/yun
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for detailsgit pull <remote> <branch>If you wish to set tracking information for this branch you can do so with:git branch --set-upstream-to=origin/<branch> yun
- 我們按照提示進行操作
git branch --set-upstream-to=origin/ yun
git branch --set-upstream-to=origin/yun yun
這時操作就成功了
[root@ ~/branch_test]$ cat yun1.c
yun hello
[root@ ~/branch_test]$ git pull
更新 4d7c92c..ae006aa
Fast-forwardyun1.c | 2 ++1 file changed, 2 insertions(+)
[root@ ~/Git_test_多人協作和沖突合并/bra
nch_test]$ cat yun1.c
yun hello
hello world
HELLO WORLD
如何一次性永久保存密碼
git config --global credential.helper store