1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git】main以外の任意のブランチから新しいブランチを切る方法

Posted at

はじめに

developブランチからfeatureブランチを派生させたいときに意外と手間取ったので、備忘録を兼ねて記事にします。

方法1:git checkout -bコマンドを使用する方法

1. 派生元になるブランチへ移動する
まずは、checkoutコマンドで起点となるブランチに移動します。

git checkout {起点となるブランチ名}

2. 新しいブランチを作成し、移動する
checkout -bコマンドを使用すると、起点となるブランチから新たなブランチを作成し、そのまま新たなブランチにチェックアウトします。

git checkout -b {新しいブランチ}

1行で書く方法

git checkout -b {新しいブランチ} {起点となるブランチ}

この方法だと、現在どのブランチにいるかに関わらず、派生ブランチの作成と移動が行えます。

方法2:git branchコマンドを使う方法

こちらの方法でも、現在どのブランチにいるかに関わらず、派生ブランチの作成と移動が行えます。
checkout -bを使う方法との違いは、こちらの場合はコマンドを実行しても、チェックアウトしているブランチは移動しない点です。

git branch {新しいブランチ} {起点となるブランチ}

補足

featureブランチが存在する状態でfeature/hogeブランチを作成するなど、既存ブランチと同じ階層で同じ名前を含むブランチを作成する場合は、先に既存ブランチをリネームか削除しておかないと、fatal: cannot lock ref~というエラーが出ます。
Githubなど、リモートリポジトリにプッシュする際も同様の注意が必要です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?