简体   繁体   English

来自远程分支的 git 变基问题

[英]Problem with git rebase from remote branch

I'm working with some big project with million lines of code and on one day I've pushed a commit into my_branch .我正在处理一个包含数百万行代码的大型项目,有一天我将提交推送到my_branch Later I've made the additional change & pushed commit through the Web UI (GitLab UI to be precise).后来我通过 Web UI(准确地说是 GitLab UI)进行了额外的更改并推送了提交。 Now I want to pull changes from my_branch to my local env.现在我想将更改从my_branch我的本地环境中。

If I run git pull --rebase origin my_branch , it gives me a lot of rebase conflicts and leads to files that I have never touched.如果我运行git pull --rebase origin my_branch ,它会给我很多 rebase 冲突并导致我从未接触过的文件。 I have no idea how to merge them and thus resolve all the conflicts... I'm sure that nobody else pushed anything there.我不知道如何合并它们,从而解决所有的冲突......我确信没有其他人在那里推动任何东西。

How to overcome this situation?如何克服这种情况? What am I doing wrong here?我在这里做错了什么?

Thanks in advance!提前致谢!

One possible solution (and a safer one) is to rename your local branch (and unset its upstream), then fetch the remote one and after that merge one into another locally.一种可能的解决方案(也是一种更安全的解决方案)是重命名您的本地分支(并取消设置其上游),然后获取远程分支,然后在本地将一个分支合并到另一个分支中。

Well, you should save contents of your current branch:好吧,您应该保存当前分支的内容:

git checkout my_branch
git checkout -b by_branch_saved

Then return to my_branch and make hard reset:然后返回 my_branch 并进行硬重置:

git checkout my_branch 
git reset --hard my_branch <latest_stable_version_without_your_changes>

After all this you are able to execute rebase without problems.毕竟,您可以毫无问题地执行变基。

git checkout trunk
git branch -D my_branch
git pull
git checkout my_branch

Re-creation helped me: I just deleted my local branch and pulled changes again from the trunk.重新创建帮助了我:我刚刚删除了我的本地分支并再次从主干中提取了更改。 Then I switched to my_branch .然后我切换到my_branch

That's all.就这样。 Thanks to everyone!谢谢大家!

You are getting conflicts in files you have not touched, but others have.您在没有接触过但其他人接触过的文件中遇到了冲突。 Rebasing remerges your local commits against the remote branch.变基将您的本地提交重新合并到远程分支。 Any changes you have merged must be merged again.您合并的任何更改都必须再次合并。 If you have many commits locally, and you do not push them, they build up over time.如果您在本地有许多提交,并且您不推送它们,它们会随着时间的推移而累积。 Rebasing becomes harder and harder.变基变得越来越难。

When you have a longer running task, create a new branch from my_branch .当您有更长的运行任务时,请从my_branch创建一个新分支。 Do normal merges instead of rebasing.做正常的合并而不是变基。 This prevents you from having to remerge the same changes over and over.这可以防止您不得不一遍又一遍地重新合并相同的更改。 When it comes time to merge your personal branch back in to my_branch use a squash-merge to keep the commit history clean:当需要将您的个人分支合并回my_branch时,请使用 squash-merge 来保持提交历史记录干净:

git checkout my_branch
git pull
git checkout -b your_branch
git commit
git commit
git fetch
git merge origin/my_branch
git commit
git checkout my_branch
git pull
git merge --squash your_branch
git push origin HEAD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM