git 签出旧提交并使其成为新提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3380805/
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 08:50:43  来源:igfitidea点击:

Checkout old commit and make it a new commit

git

提问by huggie

On Git, say I mess up my commits, and I want to make the version 3 commits ago as the new version. If I do git checkout xxxx, it creates a new branch and it seems like I can only merge it? Could I make this the new "master version"?

在 Git 上,假设我搞砸了我的提交,我想将之前的版本 3 提交作为新版本。如果我这样做git checkout xxxx,它会创建一个新分支,似乎我只能合并它?我可以把它变成新的“主版本”吗?

I want:

我想要:

A-B-C-D-E

to become

成为

A-B-C-D-E-F

where F has exactly the same content as C

其中 F 与 C 的内容完全相同

If I use git revert xxxxinstead, it seems like it definitely will have conflicts and I need to manually resolve it.

如果我git revert xxxx改为使用,它似乎肯定会发生冲突,我需要手动解决它。

What I really want is just make the old commit at some point the new commit, regardless of what's in my working directory or the latest commit.

我真正想要的是在某个时间点将旧提交变为新提交,而不管我的工作目录或最新提交中的内容。

How would I go about doing this?

我该怎么做呢?

回答by svick

git rm -r .
git checkout HEAD~3 .
git commit

After the commit, files in the new HEADwill be the same as they were in the revision HEAD~3.

提交后,新文件中的文件HEAD将与修订版中的文件相同HEAD~3

回答by Michael Mrozek

It sounds like you just want to reset to C; that is make the tree:

听起来您只想重置为 C;那就是制作树:

A-B-C

美国广播公司

You can do that with reset:

你可以这样做reset

git reset --hard HEAD~3

(Note: You said three commits ago so that's what I wrote; in your example C is only two commits ago, so you might want to use HEAD~2)

(注意:你说三个提交前,所以这就是我写的;在你的例子中,C 只有两个提交前,所以你可能想要使用HEAD~2



You can also use revertif you want, although as far as I know you need to do the reverts one at a time:

revert如果需要,您也可以使用,但据我所知,您需要一次进行一次还原:

git revert HEAD     # Reverts E
git revert HEAD~2   # Reverts D

That will create a new commit F that's the same contents as D, and G that's the same contents as C. You can rebaseto squash those together if you want

这将创建与 D 相同内容的新提交 F,与 C 相同内容的 G。rebase如果您愿意,可以将它们压缩在一起

回答by Nick Weisser

eloone did it file by file with

eloone 一个文件一个文件

git checkout <commit-hash> <filename>

but you could checkout all files more easily by doing

但是您可以通过执行更轻松地检出所有文件

git checkout <commit-hash> .

回答by eloone

This is exactly what I wanted to do. I was not sure of the previous command git cherry-pick C, it sounds nice but it seems you do this to get changes from another branch but not on same branch, has anyone tried it?

这正是我想做的。我不确定之前的命令git cherry-pick C,听起来不错,但似乎您这样做是为了从另一个分支获取更改而不是在同一个分支上,有人尝试过吗?

So I did something else which also worked : I got the files I wanted back from the old commit file by file

所以我做了一些同样有效的事情:我从旧的提交文件一个文件中得到了我想要的文件

git checkout <commit-hash> <filename>

ex : git checkout 08a6497b76ad098a5f7eda3e4ec89e8032a4da51 file.css

前任 : git checkout 08a6497b76ad098a5f7eda3e4ec89e8032a4da51 file.css

-> this takes the files as they were from the old commit

-> 这将采用旧提交中的文件

Then I did my changes. And I committed again.

然后我做了我的改变。我又犯了。

git status (to check which files were modified)
git diff (to check the changes you made)
git add .
git commit -m "my message"

I checked my history with git log, and I still have my history along with my new changes made from the old files. And I could push too.

我检查了我的历史记录git log,我仍然保留着我的历史记录以及我对旧文件所做的新更改。我也可以推。

Note that to go back to the state you want you need to put the hash of the commit before the unwanted changes. Also make sure you don't have uncommitted changes before you do that.

请注意,要返回到您想要的状态,您需要将提交的哈希值放在不需要的更改之前。在执行此操作之前,还要确保您没有未提交的更改。

回答by Adrian Parsons

git cherry-pick C

git cherry-pick C

where C is the commit hash for C. This applies the old commit on top of the newest one.

其中 C 是 C 的提交哈希。这将旧提交应用到最新提交之上。

回答by haydenmuhl

The other answers so far create new commits that undo what is in older commits. It is possible to go back and "change history" as it were, but this can be a bit dangerous. You should onlydo this if the commit you're changing has not been pushed to other repositories.

到目前为止,其他答案会创建新的提交,以撤消旧提交中的内容。有可能返回并“改变历史”,但这可能有点危险。当您正在更改的提交尚未推送到其他存储库时,应执行此操作。

The command you're looking for is git rebase --interactive

您正在寻找的命令是 git rebase --interactive

If you want to change HEAD~3, the command you want to issue is git rebase --interactive HEAD~4. This will open a text editor and allow you to specify which commits you want to change.

如果要更改 HEAD~3,则要发出的命令是git rebase --interactive HEAD~4. 这将打开一个文本编辑器,并允许您指定要更改的提交。

Practice on a different repository before you try this with something important. The man pages should give you all the rest of the information you need.

在你尝试一些重要的东西之前,先在不同的存储库上练习。手册页应该为您提供所需的所有其他信息。