简体   繁体   English

Git合并冲突问题。 git rebase或git pull --rebase?

[英]Git merge conflict issue. git rebase or git pull --rebase?

I was working on a project by myself and got the merge conflict error. 我自己一个项目,但发生合并冲突错误。 Here's what happened. 这是发生了什么事。

  • First, I created a repository for the project 首先,我为项目创建了一个存储库
  • I've been updating the repo alone without any collaborators 我没有任何合作者就一直在更新仓库
  • I've been updating the changes by git add . 我一直在通过git add更新更改。 -> git commit -> git push origin master. -> git commit-> git push原始主机。
  • There's only one branch, master. 师父只有一个分支 (origin/master.) The project exists only in the repository and my machine. (来源/母版。)项目仅存在于存储库和我的机器中。

Few days ago, I tried to do my usual update but encountered a merge conflict error. 几天前,我尝试进行常规更新,但遇到合并冲突错误。 I think it is because I've added readme.md and other stuff to the repository (in github) and committed some changes locally but not pushing it to the repo. 我认为这是因为我已将readme.md和其他内容添加到存储库(在github中),并在本地提交了一些更改,但未将其推送到存储库中。 Still not clear why it happened though. 仍然不清楚为什么会发生。

What I want to do is, the code in the machine (local changes) is up to date. 我想做的是,机器中的代码(本地更改)是最新的。 I don't care about the changes occurred in the repo because it's outdated. 我不在乎回购中发生的更改,因为它已经过时了。 Just want to update the repository with the changes committed locally . 只想使用本地提交的更改来更新存储库

I was researching about this and it seems like git rebase or git pull --rebase are the possible options. 我正在对此进行研究,似乎git rebasegit pull --rebase是可能的选择。 In this case, which one will be more suitable for me? 在这种情况下,哪一个更适合我? Are those even the right for me? 那些甚至适合我吗?

Thank you 谢谢

You need to do a 你需要做一个

git fetch

first as you have updated the readme.me remotely. 首先,因为您已远程更新了readme.me。 once you fetch the remote change, your git status will show that your branch has diverged. 一旦获取了远程更改,您的git状态将显示您的分支已经偏离。 In order to resolve that, you do a 为了解决这个问题,您可以

git rebase

this command will run your current local commit on top of changes on remote- without creating a merge commit. 该命令将在远程更改的基础上运行当前的本地提交,而无需创建合并提交。 Now if you look at git status again, you will see your branch (local) is X commits ahead of remote. 现在,如果您再次查看git status,您将看到分支(本地)是X提交在远程前面。 At this stage, you can do a git push to remote master ie origin/master, or any other branch for that sake. 在此阶段,您可以为此目的将git push到远程master,即origin / master或任何其他分支。 Hope this helps. 希望这可以帮助。

If you don't care about remote changes at all and would like to overwrite remote changes, do a 如果您根本不关心远程更改,并且想要覆盖远程更改,请执行

git push -f

In your case you need to bring your local branch up-to-date with remote repo branch. 在您的情况下,您需要使本地分支与远程repo分支保持最新。 You should use git pull --rebase to do so, as it applies your local changes on top of remote changes. 您应该使用git pull --rebase这样做,因为它将本地更改应用于远程更改。 Check this SO answer explaining the difference between git pull --rebase and git rebase 检查此SO答案以解释git pull --rebasegit rebase之间的区别

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

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