Git:可以在主分支中创建一个分支,忽略任何合并冲突吗?

发布于 2024-12-11 01:49:46 字数 199 浏览 0 评论 0原文

除了 master 之外,我还有一个带有一个分支的 git 存储库。我想让那个分支成为主分支——完全覆盖主分支。显然我可以进行合并,但这会导致许多冲突,当我知道我总是想要来自附加分支而不是主分支的文件时,解决它们似乎需要做很多工作。

是否可以将一个分支转换为主分支,或者进行合并并告诉 git 始终优先选择一个分支中的文件而不是另一个分支?

提前致谢!

I've got a git repo with one branch, in addition to the master. I want to make that one branch the master -- overwriting the master completely. Obviously I could do a merge, but this will result in many conflicts, and it seems like a lot of work to resolve them when I know I ALWAYS want the file from the additional branch, not the master.

Is it possible to convert a branch to master, or to do a merge and tell git to always favor the file from one branch over the other?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-12-18 01:49:46

您可以使用重置命令一次性完成此操作:

git checkout master
git reset --hard topicBranch

硬重置将使当前分支指向给定的提交。这将丢弃 master 上尚未在主题分支上的任何提交;它们不会被合并。

对于变基的相同警告也适用于此处:如果您已经将主分支推送到与其他开发人员共享的任何存储库,请不要执行此操作。

You can do this in one shot with the reset command:

git checkout master
git reset --hard topicBranch

A hard reset will make the current branch point to the given commit. This will throw away any commits on the master that are not already on the topic branch; they will not be merged.

The same warning for rebases applies here as well: don't do this if you've already push the master branch to any repository shared with other developers.

时光是把杀猪刀 2024-12-18 01:49:46

如果您是访问此存储库的唯一开发人员,并且它不与其他开发人员共享,则通过创建新分支很容易做到这一点。在开始之前,请确保您的存储库是干净的(所有修改都已签入当前分支)。

备份旧的 master 分支:

git checkout master
git branch oldMaster

删除旧的 master 并创建新的 master:

git checkout topicBranch
git branch -D master
git branch master

如果托管在远程服务器上的存储库上,您将需要更新远程:

git push --force remoteName master:master

重要:如果其他开发人员已从您的存储库中拉取,此方法将下次他们从您的存储库中提取或您推送到他们的存储库时,会为他们生成错误。

If you are the only developer accessing this repository and it is not shared with other developers, this is pretty easy to do by creating new branches. Before you begin, make sure that your repository is clean (all modifications have been checked into the current branch).

Make a backup of your old master branch:

git checkout master
git branch oldMaster

Delete old master and create new master:

git checkout topicBranch
git branch -D master
git branch master

If hosting on the repository on a remote server, you will need to update the remote:

git push --force remoteName master:master

IMPORTANT: If other developers have been pulling from your repository, this method will generate errors for them the next time they pull from your repo or you push to their repo.

ζ澈沫 2024-12-18 01:49:46

我仍然不是 git 方面的专家,但看起来你想要 rebase 命令:

git checkout branch_name
git rebase master
git checkout master

看起来这会在分支与主分支分离的位置自动将所有更改合并到主分支中,然后在这些更改之上将当前更改重新应用到主分支(因此您不必合并)。

当然,如果您只是想完全覆盖主存储库,我认为如果您使用强制选项,您应该能够将该分支推送到主存储库上。像这样的事情:

git push --force origin branch_name

然后你的远程将拥有主分支中分支的所有更改,你可以将其拉出来......

git checkout master
git pull --force origin

I'm still not an expert in git, but it looks like you want the rebase command:

git checkout branch_name
git rebase master
git checkout master

Looks like this will auto-merge all the changes into your master at the point where you're branch diverged from master, then re-apply the current changes to master ontop of those changes (so you won't have to merge).

Of course if you just wanted to completely overwrite the master, I think you should just be able to push that branch onto the master repository if you use the force option. Something like this:

git push --force origin branch_name

Then your remote will have all the changes from your branch in the master branch, and you can pull it back out...

git checkout master
git pull --force origin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文