这是我经常使用的:
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
请注意,最好不要对本地 master/develop 分支进行更改,而是将任何更改签出到另一个分支,分支名称前有更改类型,例如feat/
、chore/
、fix/
等。因此,您只需要拉取更改,而不是从 master 推送任何更改。其他人贡献的其他分支也是如此。因此,仅当您碰巧将更改提交到其他人已提交的分支并需要重置时,才应使用上述内容。否则,将来避免推送到其他人推送到的分支,而是通过签出的分支签出并推送到所述分支。
如果您想将本地分支重置为上游分支中的最新提交,那么到目前为止对我有用的是:
检查您的遥控器,确保您的上游和来源是您所期望的,如果不符合预期,请使用git remote add upstream <insert URL>
,例如,您从中分叉的原始 GitHub 存储库和/或git remote add origin <insert URL of the forked GitHub repo>
.
git remote --verbose
git checkout develop;
git commit -m "Saving work.";
git branch saved-work;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force
在 GitHub 上,您还可以签出与本地分支同名的分支,以便将工作保存在那里,尽管如果原始开发与本地保存的工作分支具有相同的更改,则不需要这样做。我以开发分支为例,但它可以是任何现有的分支名称。
git add .
git commit -m "Reset to upstream/develop"
git push --force origin develop
然后,如果您需要在有任何冲突的情况下将这些更改与另一个分支合并,保留开发中的更改,请使用:
git merge -s recursive -X theirs develop
使用时
git merge -s recursive -X ours develop
保留 branch_name 的冲突更改。否则使用带有git mergetool
.
连同所有的变化:
git commit -m "Saving work.";
git branch saved-work;
git checkout develop;
git fetch upstream develop;
git reset --hard upstream/develop;
git clean -d --force;
git add .;
git commit -m "Reset to upstream/develop";
git push --force origin develop;
git checkout branch_name;
git merge develop;
请注意,您可以使用提交哈希、其他分支名称等代替上游/开发。使用诸如 Oh My Zsh 之类的 CLI 工具检查您的分支是否为绿色,表示没有任何内容可提交并且工作目录是干净的(由git status
) 确认或验证。请注意,与上游开发相比,这实际上可能会添加提交,如果提交自动添加了任何内容,例如 UML 图、许可证头等,因此在这种情况下,如果需要,您可以将更改origin develop
拉到upstream develop
.