Gitlab基本管理(二)

一. Gitlab分支

1. 切換到項目位置。


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

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


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
gitlab


5. 改變文件的內容。

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


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(+)3d


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'.
code


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

image


9. 從如下圖能夠看到建立的分支first-branch和master分支。

image


10. 切換到master分支。

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


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

$ git merge first-branch –no-ffip

image


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服務器上查看信息以下。

image


二. 執行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

原始:

image

改變後:

image


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

相關文章
相關標籤/搜索