Gitブランチをローカルおよびリモートで削除する
1. 概要
Git は、業界でバージョン管理システムとして広く使用されています。 さらに、Gitブランチは私たちの日常の開発プロセスの一部です。
このチュートリアルでは、Gitブランチを削除する方法について説明します。
2. Gitリポジトリの準備
Gitブランチを削除する方法を簡単に説明するために、最初に例としてGitリポジトリを準備しましょう。
まず、テストのためにGitHubから myRepo リポジトリ( https://github.com/sk1418/myRepo )のクローンを作成しましょう。
$ git clone [email protected]:sk1418/myRepo.git
Cloning into 'myRepo'...
...
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done
次に、ローカルの myRepo ディレクトリに入り、ブランチを確認します。
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
上記の出力からわかるように、現在、myRepoリポジトリにはmasterブランチが1つだけあります。 また、マスターブランチはmyRepoのデフォルトブランチです。
次に、いくつかのブランチを作成し、ローカルおよびリモートでブランチを削除する方法を示しましょう。 このチュートリアルでは、コマンドラインでブランチを削除することに焦点を当てます。
3. ローカルブランチの削除
まず、ローカルブランチの削除を見てみましょう。
Gitのgitbranchコマンドには、ローカルブランチを削除するための-dと-Dの2つのオプションがあります。
次に、それらを詳しく見て、例を通してこれら2つのオプションの違いを理解しましょう。
3.1. -dオプションを使用したローカルブランチの削除
まず、ローカルブランチを作成してみましょう。
$ git checkout -b feature
Switched to a new branch 'feature'
次に、 -d オプションを使用して、featureブランチを削除しましょう。
$ git branch -d feature
error: Cannot delete branch 'feature' checked out at '/tmp/test/myRepo'
ご覧のとおり、エラーメッセージが表示されます。 これは、現在機能ブランチを使用しているためです。
$ git branch
* feature
master
つまり、現在チェックアウトされているブランチを削除することはできません。では、 master ブランチに切り替えて、コマンドを再度実行してみましょう。
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -d feature
Deleted branch feature (was 3aac499)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
ご覧のとおり、ローカルのfeatureブランチは正常に削除されました。
3.2. -Dオプションを使用したローカルブランチの削除
まず、機能ブランチを再度作成しましょう。 しかし今回は、いくつかの変更を加えてコミットします。
$ git checkout -b feature
Switched to a new branch 'feature'
# ... modify the README.md file ...
$ echo "new feature" >> README.md
$ git status
On branch feature
Changes not staged for commit:
...
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git ci -am'add "feature" to the readme'
[feature 4a87db9] add "feature" to the readme
1 file changed, 1 insertion(+)
-d オプションを引き続き使用している場合、Gitはfeatureブランチの削除を拒否します。
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -d feature
error: The branch 'feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature'.
これは、削除されるブランチ(機能)がデフォルトのブランチ(マスター)よりも進んでいるためです:
$ git log --graph --abbrev-commit
* commit 4a87db9 (HEAD -> feature)
| Author: ...
| Date: ...|
| add "feature" to the readme
|
* commit 3aac499 (origin/master, origin/HEAD, master)
| Author: ...
| Date: ...|
| the first commit
|
* commit e1ccb56
Author: ...
Date: ...
Initial commit
この問題を解決するには2つの方法があります。 まず、featureブランチをmasterにマージしてから、「 git branch-dfeature」を再度実行します。
ただし、マージされていないコミットを破棄する場合は、エラーメッセージが示すように、「gitbranch-Dfeature」を実行して強制削除を実行できます。
$ git branch -D feature
Deleted branch feature (was 4a87db9)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master.
3.3. git branch -d /-Dリモートブランチを削除しません
これまで、 gitbranchと-dおよび-Dオプションを使用してローカルブランチを削除しました。 -dまたは-Dのどちらで削除しても、このコマンドはローカルブランチのみを削除することに注意してください。 削除されたローカルブランチがリモートブランチを追跡している場合でも、リモートブランチは削除されません。
次に、例を通してこれを理解しましょう。 繰り返しになりますが、 feature ブランチを作成し、いくつかの変更を加えて、コミットをリモートリポジトリにプッシュしましょう。
$ git checkout -b feature
Switched to a new branch 'feature'
# add a new file
$ echo "a wonderful new file" > wonderful.txt
$ git add . && git ci -am'add wonderful.txt'
[feature 2dd012d] add wonderful.txt
1 file changed, 1 insertion(+)
create mode 100644 wonderful.txt
$ git push
...
To github.com:sk1418/myRepo.git
* [new branch] feature -> feature
上記の出力が示すように、 featureブランチにwonderful.txt、という新しいファイルを作成し、コミットをリモートリポジトリにプッシュしました。
したがって、ローカルのfeatureブランチはリモートのfeatureブランチを追跡しています。
$ git remote show origin | grep feature
feature tracked
feature pushes to feature (up to date)
機能をマスターにマージしていないので、-Dオプションを使用してローカル機能ブランチを削除しましょう。
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch -D feature
Deleted branch feature (was 2dd012d).
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/feature
remotes/origin/master
コマンドgitbranch -a の出力からわかるように、ローカルのfeatureブランチはなくなりました。 ただし、 / remotes / origin /featureブランチは削除されません。
ここで、 feature ブランチをもう一度チェックすると、行った変更はまだそこにあります。
$ git checkout feature
Switched to branch 'feature'
Your branch is up to date with 'origin/feature'.
$ cat wonderful.txt
a wonderful new file
次に、リモートブランチを削除する方法を見てみましょう。
4. リモートブランチの削除
コマンドを使用できます git push origin:
ちょうど今、ローカルブランチ機能を削除しましたが、リモート機能ブランチがまだ残っていることがわかりました。 それでは、前述のコマンドを使用して、リモートのfeatureブランチを削除しましょう。
リモート機能を削除する前に、まずローカル機能ブランチを作成してリモートブランチを追跡しましょう。 これは、リモートブランチを削除すると、ローカルブランチの追跡に影響するかどうかを確認するためです。
$ git checkout feature
branch 'feature' set up to track 'origin/feature'.
Switched to a new branch 'feature'
$ git branch -a
* feature
master
remotes/origin/HEAD -> origin/master
remotes/origin/feature
remotes/origin/master
これで、ローカルブランチとリモート機能ブランチができました。 さらに、現在、ローカルのfeatureブランチを使用しています。
次に、リモートの機能ブランチを削除しましょう。
$ git push origin -d feature
To github.com:sk1418/myRepo.git
- [deleted] feature
$ git branch -a
* feature
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
ご覧のとおり、 git push -d feature コマンドを実行すると、リモートのfeatureブランチが削除されました。 ただし、ローカルのfeatureブランチはまだ存在します。 つまり、リモートブランチを削除しても、ローカル追跡ブランチには影響しません。 したがって、ここで git push を起動すると、ローカルのfeatureブランチが再びリモートにプッシュされます。
さらに、ローカルブランチの削除とは異なり、現在作業しているローカルブランチに関係なく、リモートブランチを削除できます。 上記の例では、ローカルの feature ブランチを使用していますが、リモートのfeatureブランチを問題なく削除できます。
5. 結論
この記事では、コマンドを使用してGitのローカルブランチとリモートブランチを削除する方法について説明しました。
それらを簡単に要約しましょう:
- ローカルブランチを削除します。 git branch -d / -D
( -D オプションは強制削除用です) - リモートブランチを削除します。 git push origin -d
また git push origin:
また、ローカルまたはリモートのブランチを削除しても、反対側のブランチには影響しないことも理解しています。