文檔:https://git-scm.com/book/zh/v2/Git-分支-分支簡介
分支理解
-
master分支是項目在創建時候的默認分支,除此之外,它並沒有更多的含義。
-
剩下的 “開發分支”,“灰度分支”, “預發布分支”, “需求分支”,“測試分支” 都是根據項目和需求約定的。它們本質上只是一個分支而已。
分支在項目中的應用
1、首先,我們創建了一個項目:
http://10.2.16.183/zhiheng/myproject
這是我局域網搭建的gitlab,我們就以這個項目為例。
2、項目的基本流程:
- 克隆項目到本地
> git clone http://10.2.16.183/zhiheng/myproject
Cloning into 'myproject'...
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
切換到項目:
> cd myproject
- 查看當前狀態
> git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
.idea/
a.py
nothing added to commit but untracked files present (use "git add" to track)
- 提交代碼
> git add a.py
> git commit -m "first file"
> git push origin master
3、分支的使用
為什么要使用分支?
1、你在開發項目里面一個很大的模塊,這個模塊需要連續開發一個月,你可以選擇一個月提交一次,但一個月的開發代碼都存在你的本地電腦是很危險的。萬一電腦丟失,代碼被誤刪,損失很大!
2、你們團隊的項目有十幾個人在維護,每天會有N多次的提交,一旦你拉取和提交的間隙,被別人提交了代碼,當你在提交的時候別人就需要解決沖突。每次解決和提交沖突是很浪費時間的。
分支的使用
- 查看所有分支(遠程分支和本地分支)
> git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
- 查看本地分支
> git branch -a
* master
- 創建分支
> git branch dev
- 切換分支
> git checkout dev
當你當前分支有未提交的文件時,不允許你提交分支。
- 在 dev 分支上面操作
創建 dev_a.py 文件
dev> git status
On branch dev
Untracked files:
(use "git add <file>..." to include in what will be committed)
.idea/
dev_a.py
nothing added to commit but untracked files present (use "git add" to track)
dev> git add dev_a.py
dev> git commit -m "dev a file"
[dev ace2539] dev a file
1 file changed, 1 insertion(+)
create mode 100644 dev_a.py
- 目前雖然本地多了一個
dev
分支, 但遠程是沒有的。
dev> git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
- 提交到遠程分支。
git push origin dev
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.2.16.183/zhiheng/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To http://10.2.16.183/zhiheng/myproject
* [new branch] dev -> dev
- 再次查看所有分支, 遠程分支也多了一個
dev
dev> git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
- 不同分支下面,文件數量不一樣。
## dev 分支
dev> ls
README.md a.py dev_a.py
## master分支
master> ls
README.md a.py
4、代碼的沖突與解決。
假設A 和 B 在一個分支上開發
1、A 拉取 common.py 文件,修改。
2、B 拉取 common.py 文件,修改。
3、B 提交了 common.py 文件的修改。
4、A 在提交 common.py 文件時就會遇到沖突, A 應該怎么做?
- 拉取遠程代碼
git pull origin master
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From http://10.2.16.183/zhiheng/myproject
* branch master -> FETCH_HEAD
ed59b2e..c166f93 master -> origin/master
Updating ed59b2e..c166f93
error: Your local changes to the following files would be overwritten by merge:
common.py
Please commit your changes or stash them before you merge.
Aborting
這個時候發現代碼被 B 修改了,因為我本地也做了更新,所以不允許拉取。
- 先提交提交代碼,再拉取。
> git add common.py
> git commit -m "A 也修改了文件"
> git pull origin master
warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
From http://10.2.16.183/zhiheng/myproject
* branch master -> FETCH_HEAD
Auto-merging common.py
CONFLICT (content): Merge conflict in common.py
Automatic merge failed; fix conflicts and then commit the result.
- 解決沖突
vim common.py
# 這是一個公共文件
def common(a, b):
<<<<<<< HEAD
c = a + b
return c
=======
return a + b
>>>>>>> c166f934de72779168cd00ef40bb96ff65e09ab5
開發的過程盡量避免多人改一個方法,像這樣的沖突就比較解決了。 A和B需要坐到一起,這個沖突解決。
- 重新提交沖突
> git add common.py
> git commit -m "合並沖突"
[master 485cfaa] 合並沖突
> git push origin master
5、分支的合並。
如果多個開發同時在一個分支上開發,上面的沖突每天要發生很多次。這將嚴重影響開發效率。 每個開發都在自己的分支上面開發。
- A開發在
dev
分支。
git branch
master
* dev
test> ls
README.md a.py dev1.py dev2.py dev_a.py
- B開發在
test
分支。
git branch
master
* test
test> ls
a.py common.py README.md test1.py test2.py
此時,兩個分支的上的代碼出現了較大的不同。
將test
和dev
合並到master
1、 在A電腦上有本地只有 master 和 dev ,可以直接合並。
> git merge dev
Merge made by the 'recursive' strategy.
dev_a.py | 1 +
1 file changed, 1 insertion(+)
create mode 100644 dev_a.py
2、B電腦本地只有 master 和 test 分支。
- B電腦:先把 test 分支推送
git add .
> git commit -m "test分支代碼"
[test 14032ea] test分支代碼
2 files changed, 2 insertions(+)
create mode 100644 test1.py
create mode 100644 test2.py
> git push origin test
- A電腦:本地創建 test 分支,拉取遠程 test 分支的代碼
> git branch test
> git checkout test
Switched to branch 'test'
> git pull
- A電腦:回到 master 分支,合並 test 分支。
> git merge test
Updating 34fa4b3..7677952
Fast-forward
test1.py | 1 +
test2.py | 1 +
2 files changed, 2 insertions(+)
create mode 100644 test1.py
create mode 100644 test2.py
> ls
README.md a.py common.py dev1.py dev2.py dev_a.py test1.py test2.py
master 分支就擁有了所有分支的代碼。 在這個過程中,
1、在代碼分支合並的過程中如果出現沖突,就要解決沖突。
2、我們在分支開發的過程中,也可以時長拉取master分支的內容,一般合並到master的代碼都是相對完整的。這樣可以避免master合並的時候出現過多的沖突。