Git步步进阶---Git分支创建和删除

创建本地分支

#查看本地和远程分支,此时还没有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).

你可能感兴趣的:(技术思想-工具使用,Git)