我们在实际的多人合作中,时常用到git,那么怎么样才能有序的进行提交操作呢?
例子:直接以远程端的origin/master为远程开发分支(注:实际中常常在远程分支,创建dev分支用于日常开发,dev不是很稳定,而master分支常用于存放稳定的一个版本,dev开发完毕后,才会合并到master)。
如果在开发过程中,你提交的内容,与远程端没有冲突,通过简单的,三个步骤,即可完成提交:
git add .
git commit -m "test"
[master 26e919d] test
1 file changed, 1 insertion(+), 1 deletion(-)
git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 258 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To ******************************.git
c11b82b..26e919d master -> master
但是比如存在一个文件例如readme.rst,在你提交之前,其他人已经对此文件做了修改,再按照上述步骤,执行到git push时,就会出现错误,例如:
git add .
git commit -m "test"
[master 060b314] test
1 file changed, 1 insertion(+), 1 deletion(-)
git push origin master
To ******************************.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '******************************.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.
此时我们要做的是,获取远程端的最新状态,并在本地处理冲突,然后再执行push操作
用git pull 获取远程端最新版本
git pull origin master
然后用git status 查看的状态(可查看冲突文件)
git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: readme.rst
no changes added to commit (use "git add" and/or "git commit -a")
然后我们到readme.rst中修改冲突的地方
vi readme.rst
<<<<<<< HEAD
123456
=======
12345abc
>>>>>>> f01e511d9298789e7d1c55c4803b2120fb23f7c5
冲突部分会被
<<<<<<< HEAD
=======
>>>>>>>
包裹,上面的部分为本地的,下面的部分为远程端的,只需要根据实际情况,修改或删除相应的内容就可以了,然后在重新提交。
12345abc
git add readme.rst
git commit -m "conflict fixed"
git push origin master
就能成功提交了。