git冲突解决、多人合作

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推送

 

git 基本常用命令如下

Git 基本常用命令如下:
mkdir: XX (创建一个空目录 XX 指目录名)
pwd: 显示当前目录的路径。
git init 把当前的目录变成可以管理的 git 仓库,生成隐藏.git 文件。
git add XX 把 xx 文件添加到暂存区去。
git commit –m “XX” 提交文件 –m 后面的是注释。
git status 查看仓库状态
git diff XX 查看 XX 文件修改了那些内容
git log 查看历史记录
git reset –hard HEAD^ 或者 git reset –hard HEAD~ 回退到上一个版本
(如果想回退到 100 个版本,使用 git reset –hard HEAD~100 )
cat XX 查看 XX 文件内容
git reflog 查看历史记录的版本号 id
git checkout — XX 把 XX 文件在工作区的修改全部撤销。
git rm XX 删除 XX 文件
git remote add origin https://github.com/tugenhua0707/testgit 关联一个
远程库
git push –u(第一次要用-u 以后不需要) origin master 把当前 master 分支推送
到远程库

git clone https://github.comxiaoPOoo/CRM   从远程库中克隆
git checkout –b dev 创建 dev 分支 并切换到 dev 分支上
git branch 查看当前所有的分支
git checkout master 切换回 master 分支
git merge dev 在当前的分支上合并 dev 分支
git branch –d dev 删除 dev 分支
git branch name 创建分支
git stash 把当前的工作隐藏起来 等以后恢复现场后继续工作
git stash list 查看所有被隐藏的文件列表
git stash apply 恢复被隐藏的文件,但是内容不删除
git stash drop 删除文件
git stash pop 恢复文件的同时 也删除文件
git remote 查看远程库的信息
git remote –v 查看远程库的详细信息
git push origin master Git 会把 master 分支推送到远程库对应的远程分支上

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值