Git分支管理(三)

分支如同平行宇宙,如果不合并分支,那么各个分支就互不干扰;如果在某个时间点合并分支,那么合并后的“主分支”就同时拥有了各个分支的内容。其他版本控制系统如SVN等都有分支管理,但是切换分支极慢,但Git的分支,无论创建、切换和删除分支,都能很快完成!

分支在工程开发过程的作用:A和B两人合作开发一个项目;A负责部分需要1个月完成,这期间,如果A一直不提交自己的代码,中途可能代码丢失损失巨大;如果A提交自己的不完善代码,B拿去不完整的代码可能会跑出错误,影响B的开发;这时,如果A创建自己的分支就可以完美解决问题了,A的分支既可以保存A的代码,也可以不让B看到;直到开发完毕后,再一次性合并到原来的分支上,这样,既安全,又不影响别人工作。

Git里,Git把“每次提交”串成一条时间线,这条时间线就是一个分支。master是主分支,HEAD指针指向的是当前分支

(1)创建新分支dev,Git新建一个dev指针,指向master相同的提交,再把HEAD指针指向dev分支,就表示当前分支在dev。【Git创建新分支——增加一个dev指针,将HEAD指针指向dev指针】

(2)在dev分支上的工作完成了,master指针指向dev的当前提交,即完成了dev分支合并到master

(3)删除dev分支,只需要删掉dev指针有些提交是通过分支完成的

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git branch dev   #新建分支dev
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git branch   #查看分支,*指示当前分支
  dev
* master

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout dev   #切换到dev分支
Switched to branch 'dev'
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git branch
* dev
  master
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt   #在dev分支下修改文件
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
try branch,just have fun!
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add readme.txt
 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "branch try"   #在dev分支下更新版本库
[dev 75cf5f2] branch try
 1 file changed, 1 insertion(+)
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout master
Switched to branch 'master'

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git merge dev   #将dev分支合并到主分支
Updating 981b076..75cf5f2
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git branch -d dev   #删去dev分支
Deleted branch dev (was 75cf5f2).
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git branch
* master

分支提交修改冲突

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout -b feature1   #新建并进入分支
Switched to a new branch 'feature1'
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
try branch,just have fun!
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt   #在feature1分支下修改文件readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
Creating a new branch is quick AND simple.
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add readme.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "AND simple"
[feature1 da6ba56] AND simple
 1 file changed, 1 insertion(+), 1 deletion(-)

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout master
Switched to branch 'master'
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
try branch,just have fun!
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt   #在主分支下修改文件readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
I try to see branch conflict,just have fun!
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "master change"
[master 6d434eb] master change
 1 file changed, 1 insertion(+), 1 deletion(-)

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git merge feature1   #合并分支,出现冲突
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ 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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt   #查看分支合并冲突文件
git maybe better than svn
I just want to try another version management
<<<<<<< HEAD
I try to see branch conflict,just have fun!
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt   #手动修改文件,解决冲突 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
Creating a new branch is quick AND simple.
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "solve conflict by hand"
[master 5cf662f] solve conflict by hand

Fast forward模式:关闭,可记录分支变化历史

合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout -b newbra
Switched to a new branch 'newbra'
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
Creating a new branch is quick AND simple.
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt   #修改文件
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management
Creating a new branch is quick AND simple.

CHANGE FILE.
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "add merge"
[newbra 576006b] add merge
 1 file changed, 2 insertions(+)
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout master
Switched to branch 'master'

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git merge --no-ff -m "merge with no-ff" newbra   #禁用Fast forward模式,可从分支历史上看出分支信息。
Merge made by the 'recursive' strategy.
 readme.txt | 2 ++
 1 file changed, 2 insertions(+)

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git log --graph --pretty=oneline --abbrev-commit   #查看分支历史变化
*   751edc8 (HEAD -> master) merge with no-ff
|\  
| * 576006b (newbra) add merge
|/  
*   5cf662f solve conflict by hand
|\  
| * da6ba56 (feature1) AND simple
* | 6d434eb master change
|/  
* 75cf5f2 branch try
* 981b076 +wrong example
* c8c2c16 modify readme.txt
* 59dcbd5 + one flie

 

git branch:查看分支

git branch <name>:创建分支

git checkout <name>:切换分支

git checkout -b <name>:创建+切换分支

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

git branch -d <name>:删除分支

Git提供了git stash命令,把当前工作现场“储藏”起来,等以后恢复工作现场之后,再继续工作。用git stash list命令,查看刚才的工作现场存到哪。恢复工作现场的办法:(1)用git stash apply恢复工作现场,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;(2)用git stash pop恢复工作现场,恢复的同时把stash内容也删了。此外,可以多次stash,恢复的时候,先用git stash list查看,然后用git stash apply加上指定的stash进行恢复。

通过git branch -D <name>命令,强行删除一个没有被合并过的分支

从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来,并且,远程仓库的默认名是origin

(1)git remote,查看远程库的信息git remote -v查看更详细的信息:显示fetch和push的origin地址。如果没有推送权限,就看不到push的地址。

(2)git push <far_storage> <local_branch>把“该分支上的所有本地提交”推送到远程仓库推送时指定“本地分支”,这样,Git就会把“该分支”推送到远程仓库对应的“远程分支”

(3)git clone <site>,克隆远程仓库内容

(4)git checkout -b dev origin/dev在本地创建和远程origin仓库相同的dev分支

(5)git branck --set-upstream-to=origin/dev dev指定本地dev分支与远程origin/dev分支的链接 。

(6)git pull,从远程仓库抓取分支,如果有冲突,要先处理冲突。

猜你喜欢

转载自blog.csdn.net/cymy001/article/details/81265771
今日推荐