git branch //查看分支
git branch name//创建name分支
git checkout name//切换到name分支
git checkout -n name //创建+切换分支
git merge name//合并分支到当前分支
git branch -d name//删除分支
冲突的解决方法
bo@bo MINGW64 ~/Desktop/test (master)
$ git checkout -b fenzhi1
Switched to a new branch 'fenzhi1'//创建并切换到fenzhi1
bo@bo MINGW64 ~/Desktop/test (fenzhi1)//查看fenhzhi1的内容
$ cat a
1111111
bo@bo MINGW64 ~/Desktop/test (fenzhi1)//在fenzhi1的a中添加2222222
$ vi a
bo@bo MINGW64 ~/Desktop/test (fenzhi1)
$ git add a
bo@bo MINGW64 ~/Desktop/test (fenzhi1)//提交fenzhi1
$ git commit -m "fenzhi1添加22222"
[fenzhi1 aa21427] fenzhi1添加22222
1 file changed, 1 insertion(+)
bo@bo MINGW64 ~/Desktop/test (fenzhi1)
$ git checkout master
Switched to branch 'master'
bo@bo MINGW64 ~/Desktop/test (master)//切换到主分支,编辑a文件,增加一行3333333
//此时master分支上a文件的内容为
//1111111
//3333333
$ vi a
bo@bo MINGW64 ~/Desktop/test (master)
$ git add a
bo@bo MINGW64 ~/Desktop/test (master)//master分支提交a
$ git commit -m "master添加333333"
[master 75607e7] master添加333333
1 file changed, 1 insertion(+)
bo@bo MINGW64 ~/Desktop/test (master)//master分支合并fenzhi1
$ git merge fenzhi1
Auto-merging a
CONFLICT (content): Merge conflict in a
Automatic merge failed; fix conflicts and then commit the result.//产生冲突
bo@bo MINGW64 ~/Desktop/test (master|MERGING)
$ git status
On branch master
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: a//原因是两个分支都修改了a文件
no changes added to commit (use "git add" and/or "git commit -a")
bo@bo MINGW64 ~/Desktop/test (master|MERGING)
$ cat a
1111111//<<<<<<<到=======是master的内容
<<<<<<< HEAD
3333333
=======
2222222
>>>>>>> fenzhi1//>>>>>>>到=======是fenzhi1的内容
bo@bo MINGW64 ~/Desktop/test (master|MERGING)//将fenzhi1的修改全部删掉,只留下master的内容
$ vi a
bo@bo MINGW64 ~/Desktop/test (master|MERGING)
$ cat a
1111111
3333333
bo@bo MINGW64 ~/Desktop/test (master|MERGING)
$ git add a
bo@bo MINGW64 ~/Desktop/test (master|MERGING)//再次提交,冲突解决
$ git commit -m "解决冲突"
[master 848039d] 解决冲突
bo@bo MINGW64 ~/Desktop/test (master)
$
多人合作
git remote //查看远程库的信息
gitremote-v//查看远程库的详细信息
//此仓库是同事的仓库
bo@bo MINGW64 ~/Desktop/test1/test (dev)//查看a内容
$ cat a
1111111
2222222
3333333
bo@bo MINGW64 ~/Desktop/test1/test (dev)//同事和我编辑同一个文件的同一个地方
$ vi a
bo@bo MINGW64 ~/Desktop/test1/test (dev)
$ git add a
bo@bo MINGW64 ~/Desktop/test1/test (dev)//提交
$ git commit -m "同事的dev修改a"
[dev 3d00551] 同事的dev修改a
1 file changed, 1 insertion(+)
bo@bo MINGW64 ~/Desktop/test1/test (dev)//push时候说同事的仓库落后于远程的dev
$ git push origin dev
To https://github.com/xiaoPOoo/test.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to 'https://github.com/xiaoPOoo/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.
bo@bo MINGW64 ~/Desktop/test1/test (dev)//同事拉取远程
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/xiaoPOoo/test
36a8264..abfdabc dev -> origin/dev
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git 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> dev//说同事没有与远程建立链接
bo@bo MINGW64 ~/Desktop/test1/test (dev)
$ git branch --set-upstream-to=origin/dev//同事的dev分支与远程建立链接
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
bo@bo MINGW64 ~/Desktop/test1/test (dev)//再次拉取成功,但是产生冲突
$ git pull
Auto-merging a
CONFLICT (content): Merge conflict in a
Automatic merge failed; fix conflicts and then commit the result.
bo@bo MINGW64 ~/Desktop/test1/test (dev|MERGING)//查看冲突文件
$ cat a
1111111
2222222
3333333
<<<<<<< HEAD
5555555
=======
4444444
>>>>>>> abfdabcdcba7b6e1fa6354085b95547fccad525c
bo@bo MINGW64 ~/Desktop/test1/test (dev|MERGING)//修改冲突文件
$ vi a
bo@bo MINGW64 ~/Desktop/test1/test (dev|MERGING)
$ git add a
bo@bo MINGW64 ~/Desktop/test1/test (dev|MERGING)//同事解决了冲突之后再次提交
$ git commit -m "同事的dev修改冲突"
[dev 2751621] 同事的dev修改冲突
bo@bo MINGW64 ~/Desktop/test1/test (dev)//同事push到远程
$ git push origin dev
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 613 bytes | 306.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/xiaoPOoo/test.git
abfdabc..2751621 dev -> dev
多人协作的工作模式一般是这样的:
1首先,可以视图用git push origin branch-name推送自己的分支
2 如果推送失败,是因为远程分支宾密的本地更新早,需要用git pull试图合并
3如果合并有冲突,需要解决冲突,并在本地提交,再用git push origin branch-name推送