删除 Git 远程分支的本地引用
Git
![](/data/attachment/album/202208/11/150612dv5diwgve5k2cobk.jpg)



> 
> 遵循几个简单的步骤来保持 Git 仓库的整洁
> 
> 
> 


在合并一个 GibLab 的合并请求（MR）或 GitHub 的拉取请求（PR）后，你通常需要从远程仓库中删掉这个主题分支来保持仓库的整洁。然而，这只会删掉远程仓库的主题分支。本地 Git 仓库也会从例行清理中收益。


要同步本地仓库和远程仓库的信息，可以执行 `git prune` 命令来删除本地仓库中远程分支的本地引用。


按照以下三个简单的步骤：


1、检出仓库中的核心分支（比如 `main` 或者 `master`）：



```
$ git checkout <central_branch_name>

```

2、列出所有远程和本地分支：



```
$ git branch -a

```

示例输出：



```
4.10.z
* master
  remotes/mydata/4.9-stage
  remotes/mydata/4.9.z
  remotes/mydata/test-branch

```

在这个例子中，`test-branch` 是从远程仓库中删除的主题分支的名字。


3、删除远程分支的本地引用：


首先，列出所有可以从本地仓库中删除的分支：



```
$ git remote prune origin --dry-run

```

示例输出：



```
Pruning origin
URL: git@example.com:myorg/mydata-4.10.git
* [would prune] origin/test-branch

```

然后，删除远程分支的本地引用：



```
$ git remote prune origin

```

示例输出：



```
Pruning origin
URL: git@example.com:myorg/mydata-4.10.git
* [pruned] origin/test-branch

```

就是这样！


### 维护 Git 仓库


保持 Git 仓库的整洁，一开始似乎并不紧急，但是随着仓库规模的增长，删除不必要的数据就变得更为重要。不要让从无用的数据筛选而拖慢你。


经常删除远程分支的本地引用，是维护一个可用的 Git 仓库是一个好方法。




---


via: <https://opensource.com/article/22/8/delete-local-reference-remote-branch-git>


作者：[Agil Antony](https://opensource.com/users/agantony) 选题：[lkxed](https://github.com/lkxed) 译者：[Yufei-Yan](https://github.com/Yufei-Yan) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
After you merge a GitLab or GitHub pull request, you usually delete the topic branch in the remote repository to maintain repository hygiene. However, this action deletes the topic branch only in the remote repository. Your local Git repository also benefits from routine cleanup.

To synchronize the information in your local repository with the remote repository, you can execute the `git prune`

command to delete the local reference to a remote branch in your local repository.


Follow these three simple steps:

1. Checkout the central branch of your repository (such as main or master).

```
````$ git checkout <central_branch_name>`


2. List all the remote and local branches.

```
````$ git branch -a`


Example output:

```
``````
4.10.z
* master
remotes/mydata/4.9-stage
remotes/mydata/4.9.z
remotes/mydata/test-branch
```


In this example, `test-branch`

is the name of the topic branch that you deleted in the remote repository.

3. Delete the local reference to the remote branch.

First, list all the branches that you can delete or prune on your local repository:

```
````$ git remote prune origin --dry-run`


Example output:

```
``````
Pruning origin
URL: git@example.com:myorg/mydata-4.10.git
* [would prune] origin/test-branch
```


Next, prune the local reference to the remote branch:

```
````$ git remote prune origin`


Example output:

```
``````
Pruning origin
URL: git@example.com:myorg/mydata-4.10.git
* [pruned] origin/test-branch
```


That's it!

## Maintaining your Git repository

Keeping your Git repository tidy may not seem urgent at first, but the more a repository grows, the more important it becomes to prune unnecessary data. Don't slow yourself down by forcing yourself to sift through data you no longer need.

Regularly deleting local references to remote branches is a good practice for maintaining a usable Git repository.

## Comments are closed.