Merge pull request #25734 from lkxed/20220520-How-to-rename-a-branch,-delete-a-branch,-and-find-the-author-of-a-branch-in-Git

[提交译文][tech]: 20220520 How to rename a branch, delete a branch, and find the author of a branch in Git.md
This commit is contained in:
六开箱 2022-05-20 21:10:32 +08:00 committed by GitHub
commit 958f7832a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 204 additions and 203 deletions

View File

@ -1,203 +0,0 @@
[#]: subject: "How to rename a branch, delete a branch, and find the author of a branch in Git"
[#]: via: "https://opensource.com/article/22/5/git-branch-rename-delete-find-author"
[#]: author: "Agil Antony https://opensource.com/users/agantony"
[#]: collector: "lkxed"
[#]: translator: "lkxed"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to rename a branch, delete a branch, and find the author of a branch in Git
======
Become an expert at the most common Git tasks for managing local and remote branches.
![tree branches][1]
Image by [Erik Fitzpatrick][2], [CC BY-SA 4.0][3]
One of Git's primary strengths is its ability to "fork" work into different branches.
If you're the only person using a repository, the benefits are modest, but once you start working with many other contributors, branching is essential. Git's branching mechanism allows multiple people to work on a project, and even on the same file, at the same time. Users can introduce different features, independent of one another, and then merge the changes back to a main branch later. A branch created specifically for one purpose, such as adding a new feature or fixing a known bug, is sometimes called a topic branch.
Once you start working with branches, it's helpful to know how to manage them. Here are the most common tasks developers do with Git branches in the real world.
### Rename a branch using Git
Renaming a topic branch is useful if you have named a branch incorrectly or you want to use the same branch to switch between different bugs or tasks after merging the content into the main branch.
#### Rename a local branch
1. Rename the local branch:
```
$ git branch -m <old_branch_name> <new_branch_name>
```
Of course, this only renames your copy of the branch. If the branch exists on the remote Git server, continue to the next steps.
2. Push the new branch to create a new remote branch:
```
$ git push origin <new_branch_name>
```
3. Delete the old remote branch:
```
$ git push origin -d -f <old_branch_name>
```
#### Rename the current branch
When the branch you want to rename is your current branch, you don't need to specify the existing branch name.
1. Rename the current branch:
```
$ git branch -m <new_branch_name>
```
2. Push the new branch to create a new remote branch:
```
$ git push origin <new_branch_name>
```
3. Delete the old remote branch:
```
$ git push origin -d -f <old_branch_name>
```
### Delete local and remote branches using Git
As part of good repository hygiene, it's often recommended that you delete a branch after ensuring you have merged the content into the main branch.
#### Delete a local branch
Deleting a local branch only deletes the copy of that branch that exists on your system. If the branch has already been pushed to the remote repository, it remains available to everyone working with the repo.
1. Checkout the central branch of your repository (such as main or master):
```
$ git checkout <central_branch_name>
```
2. List all the branches (local as well as remote):
```
$ git branch -a
```
3. Delete the local branch:
```
$ git branch -d <name_of_the_branch>
```
To remove all your local topic branches and retain only the *main* branch:
```
$ git branch | grep -v main | xargs git branch -d
```
#### Delete a remote branch
Deleting a remote branch only deletes the copy of that branch that exists on the remote server. Should you decide that you didn't want to delete the branch after all, you can re-push it to the remote, such as GitHub, as long as you still have your local copy.
1. Checkout the central branch of your repository (usually main or master):
```
$ git checkout <central_branch_name>
```
2. List all branches (local as well as remote):
```
$ git branch -a
```
3. Delete the remote branch:
```
$ git push origin -d <name_of_the_branch>
```
### Find the author of a remote topic branch using Git
If you are the repository manager, you might need to do this so you can inform the author of an unused branch that it should be deleted.
1. Checkout the central branch of your repository (such as main or master):
```
$ git checkout <central_branch_name>
```
2. Delete branch references to remote branches that do not exist:
```
$ git remote prune origin
```
3. List the author of all the remote topic branches in the repository, using the `--format` option along with special selectors (in this example, `%(authorname)` and `%(refname)` for author and branch name) to print just the information you want:
```
$ git for-each-ref --sort=authordate --format='%(authorname) %(refname)' refs/remotes
```
Example output:
```
tux  refs/remotes/origin/dev
agil refs/remotes/origin/main
```
You can add further formatting, including color coding and string manipulation, for easier readability:
```
$ git for-each-ref --sort=authordate \
--format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)%(align:25,left)%(color:yellow) %(authorname)%(end)%(color:reset)%(refname:strip=3)' \
refs/remotes
```
Example output:
```
01/16/2019 03:18 PM tux      dev
05/15/2022 10:35 PM agil     main
```
You can use grep to get the author of a specific remote topic branch:
```
$ git for-each-ref --sort=authordate \
--format='%(authorname) %(refname)' \
refs/remotes | grep <topic_branch_name>
```
### Get good at branching
There are nuances to how Git branching works depending on the point at which you want to fork the code base, how the repository maintainer manages branches, squashing, rebasing, and so on. Here are three articles for further reading on this topic:
* [Explaining Git branches with a LEGO analogy][4], by Seth Kenlon
* [My guide to using the Git push command safely][5], by Noaa Barki
* [A guide to Git branching][6], by Kedar Vijay Kulkarni
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/5/git-branch-rename-delete-find-author
作者:[Agil Antony][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/agantony
[b]: https://github.com/lkxed
[1]: https://opensource.com/sites/default/files/tree-branches.jpg
[2]: https://www.flickr.com/photos/22244945@N00/3353319002
[3]: https://creativecommons.org/licenses/by-sa/4.0/
[4]: https://opensource.com/article/22/4/git-branches
[5]: https://opensource.com/article/22/4/git-push
[6]: https://opensource.com/article/18/5/git-branching

View File

@ -0,0 +1,204 @@
[#]: subject: "How to rename a branch, delete a branch, and find the author of a branch in Git"
[#]: via: "https://opensource.com/article/22/5/git-branch-rename-delete-find-author"
[#]: author: "Agil Antony https://opensource.com/users/agantony"
[#]: collector: "lkxed"
[#]: translator: "lkxed"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Git 教程:重命名分支、删除分支、查看分支作者
======
掌握管理本地/远程分支等最常见的 Git 任务。
![树枝][1]
图源:[Erik Fitzpatrick][2][CC BY-SA 4.0][3]
Git 的主要优势之一就是它能够将工作“分叉”到不同的分支中。
如果只有你一个人在使用某个存储库分支的好处是有限的。但是一旦你开始与许多其他贡献者一起工作分支就变得必不可少。Git 的分支机制允许多人同时处理一个项目,甚至是同一个文件。用户可以引入不同的功能,彼此独立,然后稍后将更改合并回主分支。那些专门为一个目的创建的分支,有时也被称为主题分支,例如添加新功能或修复已知错误。
当你开始使用分支,了解如何管理它们会很有帮助。以下是开发者在现实世界中使用 Git 分支执行的最常见任务。
### 重命名分支
有时候,你或许会错误地命名了一个分支,或者你会想要在内容合并到主分支后,使用同一个分支在不同的错误或任务之间切换。在这种情况下,重命名主题分支就会很有帮助。
#### 重命名本地分支
1. 重命名本地分支:
```
$ git branch -m <old_branch_name> <new_branch_name>
```
当然,这只会重命名您的分支副本。如果远程 Git 服务器上存在该分支,请继续执行后续步骤。
2. 推送这个新分支,从而创建一个新的远程分支:
```
$ git push origin <new_branch_name>
```
3. 删除旧的远程分支:
```
$ git push origin -d -f <old_branch_name>
```
#### 重命名当前分支
当你要重命名的分支恰好是当前分支时,你不需要指定旧的分支名称。
1. 重命名当前分支:
```
$ git branch -m <new_branch_name>
```
2. 推送新分支,从而创建一个新的远程分支:
```
$ git push origin <new_branch_name>
```
3. 删除旧的远程分支:
```
$ git push origin -d -f <old_branch_name>
```
### 使用 Git 删除本地和远程分支
为了保持存储库的整洁,通常建议你在确保已将内容合并到主分支后,删除临时分支。
#### 删除本地分支
删除本地分支只会删除系统上存在的该分支的副本。如果分支已经被推送到远程存储库,它仍然可供使用该存储库的每个人使用。
1. 签出存储库的主分支(例如 `main``master`
```
$ git checkout <central_branch_name>
```
2. 列出所有分支(本地和远程):
```
$ git branch -a
```
3. 删除本地分支:
```
$ git branch -d <name_of_the_branch>
```
要删除所有本地主题分支并仅保留 `main` 分支:
```
$ git branch | grep -v main | xargs git branch -d
```
#### 删除远程分支
删除远程分支只会删除远程服务器上存在的该分支的副本。如果你想撤销删除,也可以将其重新推送到远程(例如 GitHub只要你还有本地副本即可。
1. 签出存储库的主分支(通常是 `main``master`
```
$ git checkout <central_branch_name>
```
2. 列出所有分支(本地和远程):
```
$ git branch -a
```
3. 删除远程分支:
```
$ git push origin -d <name_of_the_branch>
```
### 查看远程主题分支的作者
如果你是存储库管理员,你可能会有这个需求,以便通知未使用分支的作者它将被删除。
1. 签出存储库的主分支(例如 `main``master`
```
$ git checkout <central_branch_name>
```
2. 删除不存在的远程分支的分支引用:
```
$ git remote prune origin
```
3. 列出存储库中所有远程主题分支的作者,使用 `--format` 选项,并配合特殊的选择器来只打印你想要的信息(在本例中,`%(authorname)` 和 `%(refname)` 分别代表作者名字和分支名称)
```
$ git for-each-ref --sort=authordate --format='%(authorname) %(refname)' refs/remotes
```
示例输出:
```
tux  refs/remotes/origin/dev
agil refs/remotes/origin/main
```
你可以添加更多格式,包括颜色编码和字符串操作,以便于阅读:
```
$ git for-each-ref --sort=authordate \
--format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)%(align:25,left)%(color:yellow) %(authorname)%(end)%(color:reset)%(refname:strip=3)' \
refs/remotes
```
示例输出:
```
01/16/2019 03:18 PM tux      dev
05/15/2022 10:35 PM agil     main
```
你可以使用 `grep` 获取特定远程主题分支的作者:
```
$ git for-each-ref --sort=authordate \
--format='%(authorname) %(refname)' \
refs/remotes | grep <topic_branch_name>
```
### 熟练运用分支
Git 分支的工作方式存在细微差别,具体取决于你想要分叉代码库的位置、存储库维护者如何管理分支、<ruby>压缩<rt>squashing</rt></ruby><ruby>变基<rt>rebasing</rt></ruby>等。若想进一步了解该主题,你可以阅读下面这三篇文章:
* [《用乐高来类比解释 Git 分支》][4]作者Seth Kenlon
* [《我的 Git push 命令的安全使用指南》][5]作者Noaa Barki
* [《Git 分支指南》][6]作者Kedar Vijay Kulkarni
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/5/git-branch-rename-delete-find-author
作者:[Agil Antony][a]
选题:[lkxed][b]
译者:[lkxed](https://github.com/lkxed)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/agantony
[b]: https://github.com/lkxed
[1]: https://opensource.com/sites/default/files/tree-branches.jpg
[2]: https://www.flickr.com/photos/22244945@N00/3353319002
[3]: https://creativecommons.org/licenses/by-sa/4.0/
[4]: https://opensource.com/article/22/4/git-branches
[5]: https://opensource.com/article/22/4/git-push
[6]: https://opensource.com/article/18/5/git-branching