Git分支建立和刪除

FeelTouch發表於2019-02-11

建立本地分支

#檢視本地和遠端分支,此時還沒有temp分支
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config   (master)
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  
#新建本地分支temp
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config  (master)
$ git branch temp

#檢視,發現有 temp說明已經有本地分支
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config   (master)
$ git branch -a
* master
  temp
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  
#切換到本地temp分支
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config    (master)
$ git checkout temp
Switched to branch 'temp'
M       consumer-task/pom.xml

#檢視,發現* temp說明已經切換到本地temp分支
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config    (temp)
$ git branch -a
  master
 * temp
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

此外若已經有遠端分支,也可以利用遠端分支來建立本地分支:

  • git checkout -b my_develop origin/develop
  • git fetch origin my_develop:develop
    git checkout my_develop

建立遠端分支

#編輯檔案
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ vim README

#add檔案
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git add README
warning: LF will be replaced by CRLF in README.
The file will have its original line endings in your working directory.

#commit檔案
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git commit -m "add README"
[temp 95447d5] add README
 1 file changed, 1 insertion(+)
 create mode 100644 README

#push改動並把本地temp分支push到遠端temp;若遠端沒有temp,則建立遠端temp分支,並完成本地temp對遠端temp的跟蹤
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git push origin temp:temp
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 146.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote:
remote: To create a merge request for temp, visit:
remote:   http://gitlab.ushareit.org:8000/interlive/cloud/server-services-config/merge_requests/new?merge_request%5Bsource_branch%5D=temp
remote:
To gitlab.ushareit.org:interlive/cloud/server-services-config.git
 * [new branch]      temp -> temp

#檢視本地和遠端分支,發現有了remotes/origin/temp遠端分支
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git branch -a
  master
* temp
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/temp

刪除遠端分支

#刪除遠端分支temp
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git push origin --delete temp
To gitlab.ushareit.org:interlive/cloud/server-services-config.git
 - [deleted]         temp

#檢視發現已經沒有遠端分支temp
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git branch -a
  master
* temp
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

#總結有如下兩種方式,第二種方式也較為常用
1)使用命令 git push origin --delete temp(temp為遠端分支名稱)
2)使用命令 git push origin :temp

刪除本地分支

#檢視,發現有本地分支temp
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git branch -a
  master
* temp
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

#需要先切換到目標之外的分支,才能刪除目標分支
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (temp)
$ git checkout master
Switched to branch 'master'
M       weshow-sqs-consumer-task/pom.xml
Your branch is up to date with 'origin/master'.

#如還有未被提交的change,執行如下命令會有錯誤提示
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (master)
$ git branch -d temp
error: The branch 'temp' is not fully merged.
If you are sure you want to delete it, run 'git branch -D temp'.

#需要用如下命令進行刪除
cc@cc-PC MINGW64 /f/my-center/cloud/server-services-config (master)
$ git branch -D temp
Deleted branch temp (was 95447d5).

相關文章