Delete a local git branch:
git branch -D <branch_name>
Delete remote git branch:
git push origin --delete <branch_name>
If the question is whether it would be a problem: No, if all commits are merged.
문제
If I have a feature branch that has been merged into the master branch and will never be needed as a separate branch again, can I delete the feature branch from the local repo and the origin repo?
해결책
Delete a local git branch:
git branch -D <branch_name>
Delete remote git branch:
git push origin --delete <branch_name>
If the question is whether it would be a problem: No, if all commits are merged.
다른 팁
Yes. You can delete the single feature branch:
# Delete a single branch that has already been completed locally and then remotely
$ git branch -D my-feature-branch
$ git push origin :my-feature-branch
Alternatively, you can delete all feature branches that have been merged into an integration branch:
# The following command deletes all LOCAL branches that have been merged into the current commit, where the branch name starts with 'feature', except for branch 'integration'.
# You will need to remove the 'echo' to actually run it.
$ git branch --merged | grep -i feature | grep -v integration | xargs -i echo git branch -D {}
# The following command deletes all REMOTE branches that have been merged into the current commit, where the branch name starts with 'feature', except for branch 'integration'.
# You will need to remove the 'echo' to actually run it.
$ git branch -r --merged | grep -i feature | grep -v integration | cut -d '/' -f 2 | xargs -n 1 echo git push --delete origin
# The following command deletes all REMOTE branches that have been merged into master
# You will need to remove the 'echo' to actually run it.
$ git checkout master
$ git merge origin/master --ff-only
$ git branch -r --merged | grep -v master | cut -d '/' -f 2 | xargs -n 1 echo git push --delete origin
If you think you've merged the branch, then it's perfectly safe to delete using git branch -d <branchName>
or git branch --delete <branchName>
, as they won't delete a branch that hasn't been merged. The lowercase d
will cause git to warn you if the branch hasn't merged the branch, but if you want to delete a branch without keeping the most recent commits, then git branch -D
will force a delete.
To delete a remote branch, use git push origin :<branchName>
, or git push origin --delete <branchName>
.