如何同步本地和远程仓库
在 GitHub 上合并拉取请求 (PR) 并在远程存储库(源)上进行 3 次提交后,我执行拉取以更新我的本地存储库(主)。不知怎的,事情出了问题,标准 CI 测试在本地失败了。我重置了本地存储库以忽略 3 次提交
git reset --hard SHA
,并恢复了 PR 之前的本地状态。然后,为了单独检查提交,我显式执行了拉取
git fetch origin
git merge SHA-commit1
git merge SHA-commit2
git merge SHA-commit3
,并达到了与远程相同的(功能)状态,所有 CI 测试都正常。
只是为了完整起见:我在远程端有另一个 PR 提案,该提案尚未合并(包含一些错误)。
虽然我的本地和远程存储库现在几乎相同,但该命令
git status origin
显示
On branch main
Your branch and 'origin/main' have diverged,
and have 3 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
我想知道是否有一种简单的方法来同步两个存储库(我担心建议的 git pull 会具有与以前相同的效果)。预先感谢您的任何提示。
After a merged pull request (PR) on GitHub with 3 commits on the remote repository (origin), I performed a pull to update my local repository (main). Somehow the things went wrong, with the standard CI tests failing locally. I reset the local repository to ignore the 3 commits using
git reset --hard SHA
and regained the local status before the PR. Then, to check the commits individually, I performed the pull explicitly using
git fetch origin
git merge SHA-commit1
git merge SHA-commit2
git merge SHA-commit3
and arrived to the same (functional) status as on remote, with all CI tests OK.
Just for completeness: I have on the remote side another PR proposal, which is not yet merged (containing some errors).
Although, my local and remote repositories are now practically the same, the command
git status origin
shows
On branch main
Your branch and 'origin/main' have diverged,
and have 3 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
I wonder if there is an easy way to sync the two repositories (I am afraid the suggested git pull would have the same effect as before). Thanks in advance for any hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答
要放弃任何本地更改并将沙箱置于与原始状态相同的状态,请运行:(
假设:您正在分支
main
上工作。)进一步评论
当您不确定发生了什么时在沙箱中,您应该使用一个工具来直观地查看您的提交图。图形化的有很多,但这里有一个简单的命令行:
关键是让它显示图形、分支名称(本地和远程)以及所有分支。
该命令将准确地显示远程和本地的差异。然后,您可以决定是否要保留本地更改并使用合并或变基,或者放弃本地更改并使用硬重置。
另请注意:我真的不喜欢 git pull 。事实上,我从来不使用它。它与
git fetch
后跟git merge
相同,但盲目地进行合并,无论这是一个好主意还是坏主意。我更喜欢 1) 获取,2) 运行显示图表的 git log 命令,然后 3) 决定我是否真的想要合并或变基或重置或诸如此类。The short answer
To discard any local changes and put your sandbox in the same state as origin, run:
(Assumption: you're working on branch
main
.)Further comments
When you're not sure what's going on in your sandbox, you should use a tool to visually look at your commit graph. There are many graphical ones, but here's a simple command line one:
The key is to make it display the graph, the branch names (local and remote), and all the branches.
This command will show you exactly how the remote and local have diverged. And then, you can decide if you want to keep your local changes and use a merge or rebase, or discard your local changes and use a hard reset.
Another note: I'm really not a fan of
git pull
. In fact, I never use it. It's the same asgit fetch
followed bygit merge
, but blindly does the merge whether it's a good idea or not. I prefer to 1) fetch, 2) run that git log command showing the graph, and then 3) decide if I actually want to merge or rebase or reset or whatnot.