git分支-分支管理

分支管理

现在已经创建、合并和删除了一些分支,让我们来看看一些分支管理工具,在开始经常使用分支时会很有用。

git branch命令不仅仅用于创建和删除分支。如果不带参数运行它,会得到当前分支的简单列表。

$ git branch

iss53

* master

Testing

这个*字符是前缀,表示当前检出的分支(即HEAD指向的分支)。这意味着如果在这个点提交,master分支将会随着新工作向前移动。要查看每个分支上的最后一次提交,可以运行git branch -v。

$ git branch -v

iss53 93b412c Fix javascript issue

* master 7a98805 Merge branch 'iss53'

testing 782fd34 Add scott to the author list in the readme

要查看已经合并到当前分支的分支,可以运行git branch --merged。

$ git branch --merged

iss53

* master

因为之前已经合并了iss53分支,所以在列表中看到了它。列表中没有*号前缀的分支通常可以使用git branch -d进行删除;因为已经将它们的工作合并到另一个分支中,所以不会丢失任何内容。

要查看所有包含尚未合并工作的分支,可以运行git branch --no-merged。

$ git branch --no-merged

Testing

这显示了另一个分支。因为它包含尚未合并的工作,尝试使用git branch -d删除它将会失败。

$ git branch -d testing

error: The branch 'testing' is not fully merged.

If you are sure you want to delete it, run 'git branch -D testing'.

如果真的想删除该分支并且丢失那些工作,你可以使用-D参数来强制删除,就像提示信息中指出的那样。

Tip:

上述描述的选项--merged和--no-merged,如果没有给定提交或分支名称作为参数,将分别显示已合并或未合并到当前分支的内容。

可以始终提供额外的参数来询问与其他分支的合并状态,而无需首先检出该其他分支,就像是,未合并到主分支的内容是什么。

$ git checkout testing

$ git branch --no-merged master

topicA

featureB

Caution

不要重命名仍然被其他合作者使用的分支。不要在没有阅读更改主分支名称部分的情况下重命名主分支(如master/main/mainline)。

要将分支从bad-branch-name更改为corrected-branch-name,并保留所有历史记录,以及在远程(GitHub、GitLab或其他服务器)更改分支名称,可以按照以下步骤操作:

在本地更改分支名称:

$ git branch -m bad-branch-name corrected-branch-name

这会将bad-branch-name更改为corrected-branch-name,但此更改目前只是本地的。为了让其他人在远程看到更正后的分支,需要将其推送。

$ git push --set-upstream origin corrected-branch-name

现在我们将简要查看一下我们目前所处的位置。

$ git branch --all

* corrected-branch-name

main

remotes/origin/bad-branch-name

remotes/origin/corrected-branch-name

remotes/origin/main

请注意,目前位于分支corrected-branch-name,并且该分支已在远程上可用。然而,具有错误名称的分支仍然存在,可以通过执行以下命令将其删除。

$ git push origin --delete bad-branch-name

Warning:

将分支名称更改为像master/main/mainline/default这样的名称会中断仓库使用的集成、服务、辅助工具和构建/发布脚本。在执行此操作之前,请确保与协作者进行咨询。另外,请确保彻底搜索存储库,并更新代码和脚本中对旧分支名称的任何引用。

使用以下命令将本地master分支重命名为main。

$ git branch --move master main

现在已经没有本地的master分支了,因为它已经被重命名为main分支。

要让其他人看到新的main分支,需要将其推送到远程。这将使重命名的分支在远程上可用。

$ git push --set-upstream origin main

现在查看下分支状态如下:

$ git branch --all

* main

remotes/origin/HEAD -> origin/master

remotes/origin/main

remotes/origin/master

本地master分支已经消失,因为它已被替换为main分支。main分支已经存在于远程。然而,旧的master分支仍然存在于远程。其他合作者将继续将master分支作为其工作的基础,直到我们进行进一步的更改。

现在,需要完成一些任务来完成过渡:

依赖于该项目的任何项目都需要更新其代码或配置

更新任何测试运行配置文件

调整构建和发布脚本

在仓库主机上重定向设置,例如仓库的默认分支、合并规则以及与分支名称匹配的其他设置。

更新文档中对旧分支的引用。

关闭或合并任何针对旧分支的拉取请求。

在完成所有这些任务并确信main分支与master分支的表现相同后,可以删除master分支:

$ git push origin --delete master

相关推荐
int WINGsssss13 小时前
Git使用
git
用户07605303543815 小时前
Git Revert:安全移除错误提交的方式
git
Good_Starry1 天前
Git介绍--github/gitee/gitlab使用
git·gitee·gitlab·github
云端奇趣1 天前
探索 3 个有趣的 GitHub 学习资源库
经验分享·git·学习·github
F_D_Z1 天前
【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
网络·git·网络协议·ssl
等风来不如迎风去2 天前
【git】main|REBASE 2/6
git
艾伦~耶格尔2 天前
IDEA 配置 Git 详解
java·ide·git·后端·intellij-idea
云兮杜康2 天前
IDEA中用过git之后文件名颜色全变红
java·git·intellij-idea
睡不醒的小泽2 天前
git merge 和 git rebase
git
艾伦~耶格尔2 天前
Git 下载及安装超详教程(2024)
git·gitee·码仓