GIT多版本并发控制(二)

五、创建与合并分支

1、查看当前分支

$ git branch

* master

2、创建并切换分支,-b创建并切换

$ git checkout -b test

Switched to a new branch 'test'

3、修改之前的代码,发现两个分支读到的内容不一致

$ cat test.txt

2222

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ cat test.txt

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git add test.txt

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git commit -m "test分支"

[test 040ceec] test分支

 1 file changed, 2 insertions(+)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git checkout master

Your branch is up-to-date with 'origin/master'.

Switched to branch 'master'

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

2222

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

4、合并分支

$ git merge test

Updating 92ad860..040ceec

Fast-forward

 test.txt | 2 ++

 1 file changed, 2 insertions(+)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

5、删除分支

$ git branch

* master

  test

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git branch -d test

Deleted branch test (was 040ceec).

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git branch

* master

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

6、小结

查看分支:git branch

创建分支:git branch name

切换分支:git checkout name

创建+切换分支:git checkout –b name

合并某分支到当前分支:git merge name

删除分支:git branch –d name

7、如何解决冲突

test下操作

$ cat test.txt

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ cat test.txt

1111

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git add test.txt

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git commit -m "添加1111"

[test 1aa35de] 添加1111

 1 file changed, 1 insertion(+)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

master上操作

$ git checkout master

Your branch is ahead of 'origin/master' by 1 commit.

  (use "git push" to publish your local commits)

Switched to branch 'master'

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

2222

3333

4444

5555

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git add test.txt

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git commit -m "添加5555"

[master 1fffc7f] 添加5555

 1 file changed, 2 insertions(+), 1 deletion(-)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

现在我们合并master和test的代码发现有冲突

$ git merge test

Auto-merging test.txt

Merge made by the 'recursive' strategy.

 test.txt | 1 +

 1 file changed, 1 insertion(+)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

1111

2222

3333

4444

5555

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

好吧,我承认,高版本这个不是问题了

$ git --version

git version 2.11.1.windows.1

查看分支合并的情况

$ git log

commit bb43c7d8d6aaf6cdac0c1e68f3620e30cc5aa4f6

Merge: 1fffc7f 1aa35de

Date:   Tue Feb 14 13:04:18 2017 +0800



    Merge branch 'test'



commit 1fffc7f636ed596aedb537ed1e7ed8ec0eb0c294

Date:   Tue Feb 14 11:59:45 2017 +0800



    添加5555



commit 1aa35de8823f8e220e424bd9ede0d1ab457b43e5

Date:   Tue Feb 14 11:56:58 2017 +0800



    添加1111



commit 040ceec408515069f0a2801cba4dfb99c94abf9a

Date:   Tue Feb 14 11:44:50 2017 +0800



    test分支



commit 92ad86071889c2e76b4ad3969126a1e58d156492

Date:   Mon Feb 13 18:17:00 2017 +0800



    test



commit 6839a9cf5ba3a8be6e0f66a08b39469bdcc63520

Date:   Mon Feb 13 17:52:42 2017 +0800



    test.txt提交

六、Bug分支

在开发中,会经常碰到bug问题,那么有了bug就需要修复,在Git中,分支是很强大的,每个bug都可以通过一个临时分支来修复,修复完成后,合并分支,然后将临时的分支删除掉。

隐藏当前工作现场

$ git status

On branch test

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.txt

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

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git stash

Saved working directory and index state WIP on test: 1aa35de 添加1111

HEAD is now at 1aa35de 添加1111

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git status

On branch test

nothing to commit, working tree clean

在主分支上修复bug

$ git checkout master

Your branch is ahead of 'origin/master' by 1 commit.

  (use "git push" to publish your local commits)

Switched to branch 'master'

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

##创建bug分支

$ git checkout -b issue-404

Switched to a new branch 'issue-404'

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (issue-404)

$ cat test.txt

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (issue-404)

$ cat test.txt

2222

3333

4444

9999

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (issue-404)

$ git add test.txt

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (issue-404)

$ git commit -m "添加999"

[issue-404 8d9a99f] 添加999

 1 file changed, 2 insertions(+), 1 deletion(-)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (issue-404)

$ git checkout master

Your branch is ahead of 'origin/master' by 1 commit.

  (use "git push" to publish your local commits)

Switched to branch 'master'

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git merge --no-ff -m "merge 404" issue-404

Merge made by the 'recursive' strategy.

 test.txt | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

2222

3333

4444

9999

##删除issue-404

$ git branch -d issue-404

Deleted branch issue-404 (was 8d9a99f).

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git branch

* master

  test

##回到test分支,查看工作现场

$ git checkout test

Switched to branch 'test'

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git status

On branch test

nothing to commit, working tree clean

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git stash list

stash@{0}: WIP on test: 1aa35de 添加1111

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ cat test.txt

1111

2222

3333

4444

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git stash apply

On branch test

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.txt

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

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ cat test.txt

1111

2222

3333

4444

5555

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git status

On branch test

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)



        modified:   test.txt

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

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git stash list

stash@{0}: WIP on test: 1aa35de 添加1111

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git stash drop

Dropped refs/stash@{0} (184ecb16499ca827f15b66c46628b419aa79e11a)

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

$ git stash list

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (test)

 

八、报错解决

1、fatal: remote origin already exists.

解决办法:    

1、先输入$ git remote rm origin

2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!

2、代码冲突处理

现象:

$ git merge --no-ff -m "test1155" test

Auto-merging test.txt

CONFLICT (content): Merge conflict in test.txt

Automatic merge failed; fix conflicts and then commit the result.

$ cat test.txt

1111

2222

3333

4444

<<<<<<< HEAD

9999

=======

5555

>>>>>>> test

$ git status

On branch master

Your branch is ahead of 'origin/master' by 3 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:   test.txt

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

解决:手动修改成你想要的内容

$ git add test.txt

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master|MERGING)

$ git commit -m "master|merge"

[master e8129f6] master|merge

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ git status

On branch master

Your branch is ahead of 'origin/master' by 6 commits.

  (use "git push" to publish your local commits)

nothing to commit, working tree clean

@Lenovo-PC MINGW64 /e/公司资料/Git仓库 (master)

$ cat test.txt

1111

2222

3333

4444

5555

9999

 

为了方便大家交流,本人开通了微信公众号,和QQ群291519319。喜欢技术的一起来交流吧

 

转载于:https://my.oschina.net/u/3023401/blog/838577

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值