git使用教程12-創建分支,切換分支,刪除分支


前言

如果本地有個分支不想要了,如何刪除?遠程倉庫的分支不想要了,如何刪除?
git倉庫經常會用到分支管理代碼,本篇講下git創建分支和刪除分支相關的操作。

git 創建分支

git branch 可以查看當前的所有分支

>git branch
* master

創建分支git branch 分支名稱

>git branch yoyo

創建之后再次查看,就會多了個分支

>git branch
* master
  yoyo

創建分支后,此時master分支前面有個星號,此時還在master分支上

checkout 切換分支

上面創建了分支,默認還是在master上,如果我們想切換到新的分支上,用git checkout 分支名稱

>git checkout yoyo
A       .idea/vcs.xml
Switched to branch 'yoyo'

此時切換成功,可以通過git branch 查看

>git branch
  master
* yoyo

查看當前分支狀態也可以用git status

>git status
On branch yoyo

創建分支的同時並切換分支

>git checkout -b yoyo2
A       .idea/vcs.xml
Switched to a new branch 'yoyo2'

相當於先創建分支,再切換分支

git branch yoyo2
git checkout yoyo2

刪除本地分支

如果上面的某個分支不想要了,想刪掉本地的分支,在刪除分支的時候, 我們會使用git branch --delete 分支名稱 來執行.
--delete縮寫就是-d,可以使用 git branch -d 分支名稱來代替

  • -d 是--delete的縮寫,在使用--delete刪除分支時,該分支必須完全和它的上游分支merge完成,如果沒有上游分支,必須要和HEAD完全merge
  • -D 是--delete --force的縮寫,這樣寫可以在不檢查merge狀態的情況下刪除分支
  • --force 簡寫-f,作用是將當前branch重置到初始點(startpoint),如果不使用--force的話,git分支無法修改一個已經存在的分支.

在不檢查merge狀態的情況下刪除分支,可以使用git branch -D 分支名稱 ,它是git branch --delete --force 分支名稱的縮寫

如果當前狀態是在yoyo2分支上,直接刪除yoyo2分支是不可以的

>git branch
  master
  yoyo
* yoyo2

>git branch -D yoyo2
error: Cannot delete branch 'yoyo2' checked out at 'D:/soft/git/web_git'

必須先切換到其它分支上才能刪除

>git checkout master
A       .idea/vcs.xml
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

>git branch -D yoyo2
Deleted branch yoyo2 (was c613c75).

>git branch
* master
  yoyo

刪除遠程分支

如果我們想通過本地的命令行刪除遠程分支,需先建立本地分支和遠程分支的關系。
場景1: 本地新建一個分支,推送到遠程分支,后面不想要這個本地分支和遠程分支了

先按前面的步驟創建本地分支並checkout到你要推送的分支上

>git branch
* master
  yoyo

>git checkout yoyo
A       .idea/vcs.xml
Switched to branch 'yoyo'

>git status
On branch yoyo

接着推送到遠程分支上:git push origin 本地分支名稱:遠程分支名稱,這樣本地分支和遠程分支就建立了關系

>git push origin yoyo:yoyo

接下來先刪除本地分支,再刪除遠程分支

>git checkout master
A       .idea/vcs.xml
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

>git branch -D yoyo
Deleted branch yoyo (was c613c75).

>git push origin --delete yoyo

 - [deleted]         yoyo

這樣我們的本地分支和遠程分支都刪掉掉了

場景2: 本地沒這個分支,遠程上有這個分支,想刪掉遠程的分支

如果我本地有這個項目,我遠程上有個 yoyoketang 的分支(本地沒分支),可以先拉到我們的本地,建立關系

>git checkout -b yoyoketang origin/yoyoketang
fatal: Cannot update paths and switch to branch 'yoyoketang' at the same time.
Did you intend to checkout 'origin/yoyoketang' which can not be resolved as commit?

上面有個報錯,可以執行git fetch更新下

>git fetch
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 13 (delta 7), reused 10 (delta 4), pack-reused 0
Unpacking objects: 100% (13/13), done.

   c613c75..f380d71  master     -> origin/master
 * [new branch]      yoyoketang -> origin/yoyoketang

再執行git checkout -b 本地分支 origin/遠程分支 就可以了

>git checkout -b yoyoketang origin/yoyoketang
A       .idea/vcs.xml
Branch yoyoketang set up to track remote branch yoyoketang from origin.
Switched to a new branch 'yoyoketang'
>git branch
  master
* yoyoketang

同步到本地分支后,跟上面操作一樣,先刪除本地分支,再刪除遠程分支

>git checkout master
A       .idea/vcs.xml
Switched to branch 'master'
Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

>git branch -D yoyoketang
Deleted branch yoyoketang (was eaa102f).

>git push origin --delete yoyoketang

 - [deleted]         yoyoketang


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM