rec*_*lic 3 git git-pull git-merge git-merge-conflict
我有一个包含 3 次提交的本地存储库:
A -> B -> C
Run Code Online (Sandbox Code Playgroud)
远程仓库还有另外 3 个提交:
A -> B -> D
Run Code Online (Sandbox Code Playgroud)
如何git pull通过丢弃所有本地提交来解决分歧?之后的本地仓库git pull --some-magic应该是:
A -> B -> D
Run Code Online (Sandbox Code Playgroud)
请注意,这git reset HEAD~1 --hard是行不通的,因为我的脚本不知道必须恢复多少提交以避免冲突。
我正在编写一个脚本来在 github 和 gitlab 存储库之间同步。其中之一是只读镜像。我的脚本只是尝试git pull --force从 origin1 到git pushorigin2。
但是,如果有人更改了 的历史记录origin1,git pull我的脚本中的 将会失败并显示错误消息。解决该问题的唯一方法是删除整个本地存储库,并git clone从stretch执行。浪费带宽而且速度很慢!
您不应该使用 来执行此操作git pull,因为它git pull是为不同的目的而设计的:结合工作。但你不想合并工作;你想镜像他们最新的提交。
To achieve that, simply run git fetch to obtain their latest commit, then use git reset --hard to reset your local branch to their latest commit. The only tricky part is that git reset --hard demands the identity of their latest commit. That's available to you: if you know that your local branch name is main because their branch name is main, your own Git will have updated your origin/main, so you can use git reset --hard origin/main.
Alternatively, you can use the upstream setting of the current branch:
git reset --hard @{upstream}
Run Code Online (Sandbox Code Playgroud)
Note that this will throw out both committed and uncommitted work, so be careful with it.