繁体   English   中英

强制更新后的 Git 拉取

[英]Git pull after forced update

我只是用git rebase压缩了一些提交并执行了git push --force (我知道这是邪恶的)。

现在其他软件工程师有不同的历史,当他们执行git pull ,Git 将合并。 有没有办法解决这个问题,除了做一个rm my-repo; git clone git@example.org:my-repo.git rm my-repo; git clone git@example.org:my-repo.git

我需要类似于git push --force的相反内容,但是git pull --force没有给出预期的结果。

接收新的提交

git fetch

重启

您可以使用git reset本地分支的提交。

要更改本地分支的提交:

git reset origin/main --hard

不过要小心,正如文档所说:

重置索引和工作树。 自 <commit> 以来对工作树中跟踪文件的任何更改都将被丢弃。

如果您想真正保留您在本地所做的任何更改 - 请改为执行--soft reset 。 这将更新分支的提交历史记录,但不会更改工作目录中的任何文件(然后您可以提交它们)。

变基

您可以使用git rebase在任何其他提交/分支之上重放本地提交:

git rebase -i origin/main

这将在交互模式下调用 rebase,您可以在其中选择如何应用不在您正在 rebase 的历史记录中的每个单独提交。

如果您删除的提交(使用git push -f )已经被拉入本地历史记录,它们将被列为将被重新应用的提交 - 它们需要作为 rebase 的一部分被删除,否则它们将被重新包含在分支的历史记录中 - 并在下次推送时重新出现在远程历史记录中。

使用 help git command --help获取有关上述(或其他)命令的更多详细信息和示例。

拉动变基

常规拉取是 fetch + merge,但您想要的是 fetch + rebase。 这是pull命令的一个选项:

git pull --rebase

这不会修复已经包含您不想要的代码的分支(请参阅下文了解如何执行此操作),但是如果他们拉了一些分支并且现在希望它是干净的(而不是“领先”于origin/some-branch)然后你只需:

git checkout some-branch   # where some-branch can be replaced by any other branch
git branch base-branch -D  # where base-branch is the one with the squashed commits
git checkout -b base-branch origin/base-branch  # recreating branch with correct commits

注意:您可以通过在它们之间放置 && 来组合所有这些

注2:Florian 在评论中提到了这一点,但谁在寻找答案时阅读评论?

注意3:如果你有受污染的分支,你可以基于新的“哑分支”创建新的分支,然后挑选提交。

前任:

git checkout feature-old  # some branch with the extra commits
git log                   # gives commits (write down the id of the ones you want)
git checkout base-branch  # after you have already cleaned your local copy of it as above
git checkout -b feature-new # make a new branch for your feature
git cherry-pick asdfasd   # where asdfasd is one of the commit ids you want
# repeat previous step for each commit id
git branch feature-old -D # delete the old branch

现在 feature-new 是你的分支,没有额外的(可能是坏的)提交!

要将远程存储库中的更改提取到本地存储库中:

git fetch

重命名跟踪分支:

git branch -m <old-branch-name> <new-name>

创建一个新的本地分支跟踪远程分支:

git checkout -b <old-branch-name>

单线解决方案

下面的命令将拉取并强制更新

git pull --rebase

暂无
暂无

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

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