我在Git中有一个存储库。我做了一个分支,然后对主分支和分支都做了一些更改。

然后,几十次提交之后,我意识到分支的状态比主分支好得多,所以我希望分支“成为”主分支,并忽略对主分支的更改。

我无法合并它,因为我不想将更改保留在master上。我该怎么办?

额外:在本例中,“旧”主控已被推送到另一个存储库,如GitHub。这是如何改变事情的?


当前回答

只需访问gitlab或github网站,查找设置即可。

然后在设置下查找存储库。

在那里找到默认分支,展开它,您可以获得重命名它或将其更改为其他分支的选项。

我在gitlab尝试过,结果成功了。

其他回答

我发现这个简单的方法效果最好。它不会重写历史,分支的所有先前签入都将附加到主节点。没有丢失任何内容,您可以在提交日志中清楚地看到发生了什么。

目标:使“分支”的当前状态成为“主”

在分支上工作,提交并推送更改,以确保本地和远程存储库是最新的:

git checkout master      # Set local repository to master
git reset --hard branch  # Force working tree and index to branch
git push origin master    # Update remote repository

在此之后,您的master将是分支的最后一次提交的确切状态,并且您的master提交日志将显示分支的所有签入。

编辑:你没说你已经推到了公开回购!这是天壤之别。

有两种方式,“脏”的方式和“干净”的方式。假设您的分支名为new master。这是一种干净的方式:

git checkout new-master
git branch -m master old-master
git branch -m new-master master
# And don't do this part.  Just don't.  But if you want to...
# git branch -d --force old-master

这将使配置文件更改以匹配重命名的分支。

你也可以用脏的方式来做,这不会更新配置文件。这是在上面的罩下发生的事情。。。

mv -i .git/refs/new-master .git/refs/master
git checkout master

通过以下方式将分支重命名为主分支:

git branch -M branch_name master

根据我的理解,您可以将当前分支分支到现有分支。本质上,这将用当前分支中的所有内容覆盖master:

git branch -f master HEAD

完成后,通常可以推送本地主分支,可能还需要此处的force参数:

git push -f origin master

没有合并,没有长命令。简单地分支和推送——但,是的,这将改写主分支的历史,所以若你们在一个团队中工作,你们必须知道你们在做什么。




或者,我发现您可以将任何分支推送到任何远程分支,因此:

# This will force push the current branch to the remote master
git push -f origin HEAD:master

# Switch current branch to master
git checkout master

# Reset the local master branch to what's on the remote
git reset --hard origin/master

只需访问gitlab或github网站,查找设置即可。

然后在设置下查找存储库。

在那里找到默认分支,展开它,您可以获得重命名它或将其更改为其他分支的选项。

我在gitlab尝试过,结果成功了。