强制更新后的 Git 拉取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9813816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:16:46  来源:igfitidea点击:

Git pull after forced update

gitgit-pull

提问by iblue

I just squashed some commits with git rebaseand did a git push --force(which is evil, I know).

我只是压扁了一些提交git rebase并做了一个git push --force(我知道这是邪恶的)。

Now the other software engineers have a different history and when they do a git pull, Git will merge. Is there a way to fix this, except doing a rm my-repo; git clone [email protected]:my-repo.git?

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

I need something like the opposite of git push --force, but git pull --forcedid not give the intended results.

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

回答by AD7six

To receive the new commits

接收新的提交

git fetch

Reset

重启

You can reset the commit for a local branch using git reset.

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

To change the commit of a local branch:

要更改本地分支的提交:

git reset origin/master --hard

Be careful though, as the documentation puts it:

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

Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

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

If you want to actually keep whatever changes you've got locally - do a --softreset instead. Which will update the commit history for the branch, but not change any files in the working directory (and you can then commit them).

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

Rebase

变基

You can replay your local commits on top of any other commit/branch using git rebase:

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

git rebase -i origin/master

This will invoke rebase in interactive mode where you can choose how to apply each individual commit that isn't in the history you are rebasing on top of.

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

If the commits you removed (with git push -f) have already been pulled into the local history, they will be listed as commits that will be reapplied - they would need to be deleted as part of the rebase or they will simply be re-included into the history for the branch - and reappear in the remote history on the next push.

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

Use the help git command --helpfor more details and examples on any of the above (or other) commands.

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

回答by Tom Prats

This won't fix branches that already have the code you don't want in them (see below for how to do that), but if they had pulled some-branch and now want it to be clean (and not "ahead" of origin/some-branch) then you simply:

这不会修复已经包含您不想要的代码的分支(有关如何执行此操作,请参见下文),但是如果他们拉了一些分支并且现在希望它是干净的(而不是“领先”于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

Note: You can combine these all by putting && between them

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

Note2: Florian mentioned this in a comment, but who reads comments when looking for answers?

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

Note3: If you have contaminated branches, you can create new ones based off the new "dumb branch" and just cherry-pick commits over.

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

Ex:

前任:

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

Now feature-new is your branch without the extra (possibly bad) commits!

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

回答by herman

Pull with rebase

拉动变基

A regular pull is fetch + merge, but what you want is fetch + rebase. This is an option with the pullcommand:

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

git pull --rebase