**整体使用步骤 **
Git会默认给使用者创建一个主分支,名称为master。
按照下面的循环进行代码开发:
先保证master的代码与远程仓库代码相同(也可以不相同,最好杨成相同的习惯,这样会减少冲突的产生)。创建本地分支test并切换到该分支上,然后在test上进行代码开发,需要提交时先将master分支合并到test分支上,在将代码push到远程仓库,最后通过pull指令将master分支上的代码更新成最新的,将test分支删除。
详细步骤
1.现在主分支master上拉取最新代码:
git pull
2.创建分支
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 "提交的日志"
git stash pop 从git栈中获取到最近一次stash进去的内容,恢复工作区的内容。
6.将test分支上的代码push到远程仓库中
git push 远程仓库位置 #提交代码到远程仓库
7.在远程仓库合并
8将分支切换到master分支,并将master分支上的代码更新为远程仓库中最新代码
git checkout master
git pull
9.将test分支删除
git branch -d test
其他操作
1.冲突解决
合并文件时,若有冲突会提示如下:
$ git merge featurel
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Auromatic 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 commit.
(Use 'git push' to publish your local commits)
Your have unmerged paths.
(fix confglicts and run 'git commit')
(use 'git merge --abort' to abort the merge)
Unerged 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 GRL.
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.
>>>>>>>>>> featurel
根据实际业务情况,将冲突点的代码进行更改后,在进行提交即可。
$ 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分支与远程仓库中的代码