###git分支###:
分支: 即是平行空间,假设你在为某个手机系统研发拍照功能,代码已经完成了80%,但如果将这不完整的代码直接提交到git仓库中,又有可能影响到其他人的工作,此时我们便可以在该软件的项目之上创建一个名叫"拍照功能"的分支,这种分支只会属于你自己,而其他人看不到,等代码编写完成后再与原来的项目主分支合并并下即可,这样即能保证代码不丢失,又不影响其他人的工作。
一般在实际的项目开发中,我们要尽量保证master分支是非常稳定的,仅用于发布版本,平时不要随便直接修改里面的数据文件,而工作的时候则可以新建不同的工作分支,等到工作完成后在合并到master分支上面,所以团队合作分支看起来会像上图那样。
----------------------------------------------------------------------------------------
#新建testing分支#:
[root@git data]# git branch testing
[root@git data]# git branch
* master
testing #*号在哪里就说明当前在哪个分支上入
[root@git data]# touch aaa bbb ccc
[root@git data]# git add aaa
[root@git data]# git commit -m "add aaa"
[master c9b595d] add aaa
1 file change,1 insertion(+),0 deletions(-)
create mode 100644 aaa
[root@git data]# git add bbb
[root@git data]# git commit -m "add ccc"
[master 925e528] add bbb
1 file change,1 insertion(+),0 deletions(-)
create mode 100644 bbb
[root@git data]# git add ccc
[root@git data]# git commit -m "add ccc"
[master 435e6cf] add ccc
1 file changed,1 insertion(+),0 deletions(-)
create mode 100644 ccc
[root@git data]# git log --oneline --decorate
435e6cf(HEAD,master) add ccc
925e528 add bbb
c9b595d add aaa
dcccbf1 add 123 > a
bd2dea9 add index
951adcc mv a.txt a
799bc0a modified a a.txt
295e997 add newfile a
----------------------------------------------------------------------------------------
#切换分支#:
[root@git data]# git checkout testing
Switched to branch 'testing'
[root@git data]# git branch
master
*testing
#删除testing分支#:
[root@git data]# ll
-rw-r--r-- 1 root root 0 Jan 24 17:47 a
[root@git data]# git branch -d testing
Deleted branch testing (was dcccbf1)
[root@git data]# git branch
*master
[root@git data]#
#创建并且切换到新的分支#:
[root@git data]# git checkout -b testing
Switched to a new branch 'testing'
[root@git data]# git branch
master
*testing
[root@git data]# ls
a aaa bbb ccc
[root@git data]# ll
total 4
-rw-r--r-- 1 root root 10 Jan 24 17:41 a
-rw-r--r-- 1 root root 10 Jan 24 18:01 aaa
-rw-r--r-- 1 root root 10 Jan 24 18:01 bbb
-rw-r--r-- 1 root root 10 Jan 24 18:01 ccc
#查看指针指向#:
[root@git data]# git log --oneline --decorate
435e6cf(HEAD,testing,master) add ccc
925e528 add bbb
c9b595d add aaa
dcccbf1 add 123 > a
bd2dea9 add index
951adcc mv a.txt a
799bc0a modified a a.txt
295e997 add newfile a
[root@git data]# touch test-ddd
[root@git data]# git add .
[root@git data]# git commit -m "add newfile test-ddd"
[testing de60495] add newfile test-ddd
1 file changed,1 insertion(+),0 deletions(-)
create mode 100644 test-ddd
[root@git ~]# git branch
master
*testing
----------------------------------------------------------------------------------------
#返回到主干:(分支不会影响主干)#
[root@git data]# git checkout master
Switched to branch 'master'
[root@git data]# git branch
*master
testing
[root@git data]# ll
total 4
-rw-r--r-- 1 root root 10 Jan 24 17:41 a
-rw-r--r-- 1 root root 10 Jan 24 18:02 aaa
-rw-r--r-- 1 root root 10 Jan 24 18:02 bbb
-rw-r--r-- 1 root root 10 Jan 24 18:02 ccc
#查看指针指向#:
[root@git data]# git log --oneline --decorate
435e6cf(HEAD,master) add ccc
925e528 add bbb
c9b595d add aaa
dcccbf1 add 123 > a
bd2dea9 add index
951adcc mv a.txt a
799bc0a modified a a.txt
295e997 add newfile a
----------------------------------------------------------------------------------------
#新添加了一个master-eee#
[root@git data]# touch master-eee
[root@git data]# git add .
[root@git data]# git commit -m "add newfile master-eee"
[master acf85f7] add newfile master-eee
1 file changed,0 insertion(+),0 deletions(-)
create mode 100644 master-eee
----------------------------------------------------------------------------------------
#查看指针指向#:
[root@git data]# git log --oneline --decorate
acf85f7 (HEAD,master) add newfile master-eee
435e6cf add ccc
925e528 add bbb
c9b595d add aaa
dcccbf1 add 123 > a
bd2dea9 add index
951adcc mv a.txt a
799bc0a modified a a.txt
295e997 add newfile a
----------------------------------------------------------------------------------------
[root@git data]# git merge testing
Merge branch 'testing'
merge testing
#功能写完,删除testing分支#:
[root@git data]# git checkout master
Switched to branch 'master'
[root@git data]# git branch -d testing
Deleted branch testing (was de60495)
#查看指针指向#:
[root@git data]# git log --oneline --decorate
b12af78 (HEAD,master) Merge branch 'testing' merge testing
acf85f7 add newfile master-eee
435e6cf add ccc
925e528 add bbb
c9b595d add aaa
dcccbf1 add 123 > a
bd2dea9 add index
951adcc mv a.txt a
799bc0a modified a a.txt
295e997 add newfile a
----------------------------------------------------------------------------------------
###git代码冲突合并###:
先创建分支:
[root@git data]# git branch testing
[root@git data]# git branch
* master
testing
[root@git data]# echo master >> aaa
[root@git data]# git commit -am "modified aaa add master"
[master f4d31cb] modified aaa add master
1 file changed,0 insertion(+)
[root@git data]# git status
#On branch master
nothing to commit,working directory clean
[root@git data]# cat aaa
master
[root@git data]# git checkout testing
Switched to branch 'testing'
[root@git data]# cat aaa
[root@git data]# echo testing >> aaa
[root@git data]# git commit -am "modified add testing"
[testing 20a3ef1] modified add testing
1 file changed,1 insertion(+)
[root@git data]# cat aaa
testing
[root@git data]#
#切换到master#:
[root@git data]# git checkout master
[root@git data]# git branch
* master
testing
[root@git data]# git merge testing
Auto-merging aaa
CONFLICT (content): Merge config in aaa
[root@git data]# cat aaa
<<<<<<< HEAD
master
========
testing
>>>>>>>> testing
[root@git data]# git status
#On branch master
#You branch master
# (fix conflicts and run "git commit")
#Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: aaa
#
----------------------------------------------------------------------------------------
#出现冲突了,只能通过手动来操作:
[root@git data]# cat aaa
<<<<<<< HEAD
master
========
testing
>>>>>>>> testing
[root@git data]# vim aaa
master
testing
[root@git data]# git commit -am "merge testing"
[master f7ed010] merge testing
[root@git data]# git status
#On branch master
nothing to commit,working directory clean
[root@git data]# cat aaa
master
testing
----------------------------------------------------------------------------------------
###git便签使用###:
#标签也是指向了一次commit提交,是一个里程碑式的标签,回滚打标签直接加标符号,
不需要加唯一子串不好记
# -a指定标签名字 -m 指定说明文字
[root@git data]# git tag -a v1.0 -m "aaa bbb master testing version v1.0"
[root@git data]# git tag
v1.0
#指定某一次的提交为便签
[root@git data]# git tag -a v2.0 dbead4c -m "add bbb version v2.0"
#查看v1.0的信息 git show 加标签查看
[root@git data]# git show v1.0
#直接还原数据到v2.0
[root@git data]# git reset --hard v2.0
HEAD 现在位于 dbead4c add bbb
[root@git data]# ll
总用量 4
-rw-r--r-- 1 root root 8 2月 23 11:26 a
-rw-r--r-- 1 root root 0 2月 23 11:26 b
#删除标签 -d 参数#
[root@git data]# git tag -d v2.0
----------------------------------------------------------------------------------------
[root@git data]# git log --oneline
f7ed010 merge testing
20a3ef1 modified add testing
f4d31cb modified aaa add master
b12af78 Merge branch 'testing' merge testing
acf85f7 add newfile master-eee
bd2dea9 add index
[root@git data]# git tag -a v1.0 bd2dea9 -m "tag v1.0 add index"
[root@git data]#
[root@git data]# git tag
v1.0
[root@git data]#
[root@git data]# git reset --hard v1.0
HEAD is now at bd2dea9 add index
[root@git data]# git tag -a "v2.0" -m "XXX"
[root@git data]# git tag
v1.0
v2.0
----------------------------------------------------------------------------------------
网友评论