GIT多人协作开发步骤总结

目录

 

写在最前端

整体使用步骤

详细步骤

其他操作

优点


 

写在最前端

我使用的存放git代码的工具是GitLab,记得需要和公司要GitLab的账号和密码。关于GitLab的账号创建、登陆、SSH配置等步骤不是本次的重点,而且网上有很多配置步骤。本次只讲解Git托管代码时多人协作的使用步骤。

整体使用步骤

GIT会默认给使用者创建一个主分支,名称为master。

按照下面的循环进行代码开发:

        先保证master的代码与远程仓库代码相同(也可以不相同,但是最好养成相同的习惯,这样会减少冲突的产生)。创建本地分支test并切换到该分支上,然后在test上进行代码开发,需要提交时先将master分支合并到test分支上,再将代码push到远程仓库,最后通过pull指令将master分支上的代码更新成最新的,将test分支删除。

详细步骤

1,先在主分支master上拉取最新代码:
  

 git pull

2,创建本地分支test:
    

git branch test

3,将android studio切换到test分支,并在在test上写代码:
    

git checkout test

4,下班时,先将test分支上的代码隔离,将分支切换到master,并更新master上的代码为远程仓库中的最新代码:
    

git stash save "暂时隔离的说明信息"
git checkout master
git pull

5,将分支切换到test,先将test分支上隔离的代码取出,之后将代码提交,再将master分支合并到test分支上:
    

git checkout test
git rebase master
git stash pop      #注意:在该步骤可能会出现冲突,若出现在该步骤进行冲突合并后再进行下面的操作
git add .
git commit -m"提交的日志"

6,将test分支上的代码push到远程仓库中:
    

git push 远程仓库地址

7,在GitLab中添加 Merge Requests,等待合并完成代码的邮件,或者如果在GitLab中有合并的权限,直接自己合并。

      

8,将分支切换到master分支,并将master分支上的代码更新为远程仓库中最新代码:
    

git checkout master
git pull

9,将test分支删除:
 

 git branch -d test

其他操作

1、 冲突解决

        合并文件时,若有冲突会提示如下:

$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

      通过git status 查看冲突文件:定位到时readme.txt文件。

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

        打开readme.txt文件,找到冲突点:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1

        根据实际业务情况,将冲突点处的代码进行更改后,再进行提交即可:

$ git add readme.txt 
$ git commit -m "conflict fixed"
[master cf810e4] conflict fixed

2、 修改本地分支名称

        查看本地所有分支

git branch

        更改本地分支名称

git branch -m old_branch_name new_branch_name

 

优点

        代码的开发与合并都在重新创建的本地分支上,不会影响master分支与远程仓库中的代码。

----------------------------------------------------------------彩---蛋---如---下--------------------------------------------------------------------------

最后附上廖雪峰大神的GIT详细讲解链接:

      https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

一个总结的比较好的GIT使用指南:

      https://www.cnblogs.com/justuntil/p/8966383.html

一个GIT使用步骤的总结:

    https://blog.csdn.net/Dream_201603/article/details/84071092

猜你喜欢

转载自blog.csdn.net/Dream_201603/article/details/84070508