###git分支###:
分支:?即是平行空間,假設(shè)你在為某個(gè)手機(jī)系統(tǒng)研發(fā)拍照功能膳殷,代碼已經(jīng)完成了80%操骡,但如果將這不完整的代碼直接提交到git倉(cāng)庫(kù)中,又有可能影響到其他人的工作赚窃,此時(shí)我們便可以在該軟件的項(xiàng)目之上創(chuàng)建一個(gè)名叫"拍照功能"的分支册招,這種分支只會(huì)屬于你自己,而其他人看不到勒极,等代碼編寫(xiě)完成后再與原來(lái)的項(xiàng)目主分支合并并下即可是掰,這樣即能保證代碼不丟失,又不影響其他人的工作辱匿。
一般在實(shí)際的項(xiàng)目開(kāi)發(fā)中键痛,我們要盡量保證master分支是非常穩(wěn)定的,僅用于發(fā)布版本匾七,平時(shí)不要隨便直接修改里面的數(shù)據(jù)文件絮短,而工作的時(shí)候則可以新建不同的工作分支,等到工作完成后在合并到master分支上面昨忆,所以團(tuán)隊(duì)合作分支看起來(lái)會(huì)像上圖那樣丁频。
----------------------------------------------------------------------------------------
#新建testing分支#:
[root@git data]# git branch testing
[root@git data]# git branch
* master
? testing? #*號(hào)在哪里就說(shuō)明當(dāng)前在哪個(gè)分支上入
[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]#?
#創(chuàng)建并且切換到新的分支#:
[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
----------------------------------------------------------------------------------------
#返回到主干:(分支不會(huì)影響主干)#
[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
----------------------------------------------------------------------------------------
#新添加了一個(gè)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
#功能寫(xiě)完,刪除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代碼沖突合并###:
先創(chuàng)建分支:
[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
#
----------------------------------------------------------------------------------------
#出現(xiàn)沖突了邑贴,只能通過(guò)手動(dòng)來(lái)操作:
[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便簽使用###:
#標(biāo)簽也是指向了一次commit提交限府,是一個(gè)里程碑式的標(biāo)簽,回滾打標(biāo)簽直接加標(biāo)符號(hào)痢缎,
不需要加唯一子串不好記
# -a指定標(biāo)簽名字 -m 指定說(shuō)明文字
[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 加標(biāo)簽查看
[root@git data]# git show v1.0
#直接還原數(shù)據(jù)到v2.0
[root@git data]# git reset --hard v2.0
HEAD 現(xiàn)在位于 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
#刪除標(biāo)簽 -d 參數(shù)#
[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
----------------------------------------------------------------------------------------