[git] <branch > 로컬 & 원격 삭제 방법

Hyeseong·2021년 1월 21일
0

GIT

목록 보기
21/24

freecodecamp에서 글을 가져왔습니다.

How to Delete a Git Branch Both Locally and Remotely

In most cases, it is simple to delete a Git branch. You'll learn how to delete a Git brach locally and remotely in this article.

Syntax

// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

When to Delete branches

It is common for a Git repo to have different branches. They are a great way to work on different features and fixes while isolating the new code from the main codebase.

Repos often have a master branch for the main codebase and developers create other branches to work on different features.

Once work is completed on a feature, it is often recommended to delete the branch.

Deleting a branch LOCALLY

Git will not let you delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting. For example: git checkout master

Delete a branch with git branch -d .

For example: git branch -d fix/authentication

The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

The branch is now deleted locally.

Deleting a branch REMOTELY
Here's the command to delete a branch remotely: git push --delete .

For example: git push origin --delete fix/authentication

The branch is now deleted remotely.

You can also use this shorter command to delete a branch remotely: git push :

For example: git push origin :fix/authentication

If you get the error below, it may mean that someone else has already deleted the branch.

error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'

Try to synchronize your branch list using:

git fetch -p
The -p flag means "prune". After fetching, branches which no longer exist on the remote will be deleted.

profile
어제보다 오늘 그리고 오늘 보다 내일...

0개의 댓글