1. 程式人生 > >Gitlab基本管理(二)

Gitlab基本管理(二)

warning back ron 問題 ranch 方式 tps switch 內容

一. Gitlab分支

1. 切換到項目位置。


2. 創建一個項目的一新分支。

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git branch first-branch


3. 切換到新建的分支下。

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git checkout first-branch
Switched to branch ‘first-branch‘

mike@win10-001 MINGW64 ~/cookbook/cookbook (first-branch)


4. 第2步和第3步可以合並成一步。

mike@win10-001 MINGW64 ~/cookbook/cookbook (first-branch)
$ git checkout -b first-branch


5. 改變文件的內容。

mike@win10-001 MINGW64 ~/cookbook/cookbook (first-branch)
$ echo "Change" >> README.md


6. 提交這個改變

$ git commit -a -m ‘Readme changed‘
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
[first-branch dc7b6d5] Readme changed
1 file changed, 1 insertion(+)


7. 推送分支到gitlab服務器

mike@win10-001 MINGW64 ~/cookbook/cookbook (first-branch)
$ git push -u origin first-branch
Counting objects: 3, done.
Writing objects: 100% (3/3), 262 bytes | 131.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for first-branch, visit:
remote: http://gitlab.aishangwei.net/root/cookbook/merge_requests/new?merge_request%5Bsource_branch%5D=first-branch
remote:
To gitlab.aishangwei.net:root/cookbook.git
* [new branch] first-branch -> first-branch
Branch ‘first-branch‘ set up to track remote branch ‘first-branch‘ from ‘origin‘.


8. 在Gitlab服務器查看我們推送的分支

技術分享圖片


9. 從以下圖可以看到創建的分支first-branch和master分支。

技術分享圖片


10. 切換到master分支。

$ git checkout master
Switched to branch ‘master‘
Your branch is up to date with ‘origin/master‘.


11. 合並first-branch分支到master分支。

$ git merge first-branch –no-ff

技術分享圖片


12. 從第11步輸出的信息可以看到,給我們一個機會去改變提交的信息。在我們保存和關閉編輯器的時候,這個分支將會合並,具體信息如下.

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git merge first-branch --no-ff
Merge made by the ‘recursive‘ strategy.
README.md | 1 +
1 file changed, 1 insertion(+)


13. 推送改變到Gitlab上的master.

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git push origin master
Counting objects: 1, done.
Writing objects: 100% (1/1), 223 bytes | 223.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To gitlab.aishangwei.net:root/cookbook.git
53ec2ca..5e1ebdd master –> master


14. 刪除所創建的分支。

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git push origin --delete first-branch
To gitlab.aishangwei.net:root/cookbook.git
- [deleted] first-branch


15. 在gitlab服務器上查看信息如下。

技術分享圖片


二. 執行rebase操作

當我們長時間的運行在分支上的話,我們有時想要同步master,可以通過合並到master後,再切換到我們所在的分支,Git有一個更好的方式來這個,叫做rebasing。 在rebasing中,相當於把你當前的branch分支合並到master,並同步master狀態。不過是一步完成了。


1. 使用終端進入到cookbook項目,並創建一個分支。

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git checkout -b rebase-branch
Switched to a new branch ‘rebase-branch‘


2. 創建一個新的文件並提交它。

mike@win10-001 MINGW64 ~/cookbook/cookbook (rebase-branch)
$ echo "File content" >> another_file.md

mike@win10-001 MINGW64 ~/cookbook/cookbook (rebase-branch)
$ git add .
warning: LF will be replaced by CRLF in another_file.md.
The file will have its original line endings in your working directory.

mike@win10-001 MINGW64 ~/cookbook/cookbook (rebase-branch)
$ git commit -m ‘Another commit‘
[rebase-branch c20f042] Another commit
1 file changed, 1 insertion(+)
create mode 100644 another_file.md


3. 切換到master分支。

mike@win10-001 MINGW64 ~/cookbook/cookbook (rebase-branch)
$ git checkout master
Switched to branch ‘master‘
Your branch is up to date with ‘origin/master‘.


4. 假如先前推送過代碼分支到Gitlab服務器,再執行rebase,那麽在推送時候,可能服務器會報錯。為了克服這個問題,可以使用-f參數。

$ git push origin rebase-branch –f

5. 在master分支上創建提交。

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ echo "1" >> README.md

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git add .
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.

mike@win10-001 MINGW64 ~/cookbook/cookbook (master)
$ git commit -m ‘Commit in master‘
[master acb491e] Commit in master
1 file changed, 1 insertion(+)


6. 切換到rebase-branch 分支

mike@win10-001 MINGW64 ~/cookbook/cookbook (rebase-branch)
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: Another commit


三. 擠壓提交信息(Squashing your commits)

在開發的時候可能由於頻繁的提交信息,比較零散和片斷,比如在提交多少次後,想做一個總結。這時候可以把前面幾個合並成一個提交信息。


執行案例:

1. 進入到cookbook項目,並切換到squash-branch分支。

mike@win10-001 MINGW64 ~/cookbook/cookbook (rebase-branch)
$ git checkout -b squash-branch
Switched to a new branch ‘squash-branch‘


2. 在squash-branch分支下創建兩個提交 ,並把它兩個提交擠壓到一起。

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ echo "1" >> README.md

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ git add .
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ git commit -a -m ‘wip1‘
[squash-branch 4e54db8] wip1
1 file changed, 1 insertion(+)

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ echo "2" >> README.md

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ git add .
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ git commit -a -m ‘wip2‘
[squash-branch 5d37a65] wip2
1 file changed, 1 insertion(+)


3. 通過以下命令可以查看我們的提交歷史。

mike@win10-001 MINGW64 ~/cookbook/cookbook (squash-branch)
$ git log --oneline
5d37a65 (HEAD -> squash-branch) wip2
4e54db8 wip1
f5f7fde (rebase-branch) Another commit
acb491e (master) Commit in master
5e1ebdd (origin/master) Merge branch ‘first-branch‘
dc7b6d5 (first-branch) Readme changed
53ec2ca Added readme file


4. 把wip1和wip2的提交信息擠壓在一個更好的提交信息中,執行以下命令。此時會打開一個編輯器。

$ git rebase –i HEAD~2

註解: HEAD~2 代表著我們想擠壓最後兩個提交。假如你想要擠壓最後4個提交,那麽使用HEAD~4

原始:

技術分享圖片

改變後:

技術分享圖片


5. 第4步編輯保存後,Git會打開另外一個編輯窗口,在這個窗口中,編輯提交信息並保存。會彈出如下畫面。

$ git rebase -i HEAD~2
[detached HEAD 48f7dae] Added two number to the readme
Date: Sun Jun 24 17:33:08 2018 +0800
1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/squash-branch.


6. push該分支到gitlab服務器。

$ git push origin squash-branch
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 839 bytes | 104.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for squash-branch, visit:
remote: http://gitlab.aishangwei.net/root/cookbook/merge_requests/new?merge_request%5Bsource_branch%5D=squash-branch
remote:
To gitlab.aishangwei.net:root/cookbook.git
* [new branch] squash-branch -> squash-branch

Gitlab基本管理(二)