简体   繁体   English

如何使用 git 加载项目的倒数第二个提交版本?

[英]How do I load the second to last committed version of my project using git?

Suppose that I made some changes and did this:假设我进行了一些更改并执行了以下操作:

git add -A
git commit -m "comment commit_1"

Now I made more changes and did this again:现在我进行了更多更改并再次执行此操作:

git add -A
git commit -m "comment commit_2"

Now, I basically want to discard commit_2 and start modifying my project again at the point of commit_1.现在,我基本上想放弃 commit_2 并在 commit_1 点再次开始修改我的项目。

How do I do this?我该怎么做呢?

If you don't mind losing the revisions at all (as if they never happened at all), you can do:如果您根本不介意丢失修订版(就好像它们根本没有发生一样),您可以这样做:

git reset --hard HEAD~

If you want to keep the current history and get a new version on top of what you have where you get rid of the changes of the revision, you can do:如果您想保留当前历史记录并在摆脱修订更改的地方获得新版本,您可以执行以下操作:

git checkout HEAD~
git reset --soft the-branch
git commit -m "Taking back changes from the last revision"
# And if you like the result of this:
git branch -f the-branch
git checkout the-branch

There you go.你去吧。

Now, I basically want to discard commit_2 and start modifying my project again at the point of commit_1.现在,我基本上想放弃 commit_2 并在 commit_1 点再次开始修改我的项目。

If you literally want to discard commit_2 , then it may be that you can use如果您真的想丢弃commit_2 ,那么您可能可以使用

git reset --hard HEAD^

(Note that at the time I wrote this, another post incorrectly told you to use HEAD~2 ; that would discard both commits, which is not what was asked.) (请注意,在我写这篇文章的时候,另一篇文章错误地告诉你使用HEAD~2 ;这会丢弃两个提交,这不是所要求的。)

There are caveats to using commands that remove commits from a branch.使用从分支中删除提交的命令有一些注意事项。

The first is that you could lose the changes you made in affected commits.第一个是您可能会丢失在受影响的提交中所做的更改。 That may seem a strange warning - you asked how to discard the changes - but realizing that mistakes happen, it's cause to be careful with these commands.这似乎是一个奇怪的警告 - 您询问了如何放弃更改 - 但意识到错误发生时,需要小心这些命令。 (Just to clarify - if you did something like this accidentally, the changes would not be immediately lost forever; the reflogs offer a little safety net.) (只是为了澄清 - 如果您不小心做了这样的事情,更改不会立即永远丢失;reflogs 提供了一个小安全网。)

Other issues arise if your repo has remotes and you've pushed the commit(s) in question to any of the remotes.如果您的 repo 有遥控器并且您已将有问题的提交推送到任何遥控器,则会出现其他问题。 It doesn't sound like that's an issue here, but if it is then there is more you should consider before removing commits from a branch.这听起来不像这里的问题,但如果是,那么在从分支中删除提交之前,您应该考虑更多。

But if you've looked it over and determined that you really do just want to discard the commits, the above should work.但是,如果您仔细查看并确定您真的只想放弃提交,则上述方法应该可行。 Do note, HEAD^ is just one of the possible expressions that refers to the second-to-last commit.请注意, HEAD^只是引用倒数第二次提交的可能表达式之一。 At the moment it may be the simplest thing to use, but it won't always refer to that particular commit.目前它可能是最简单的使用方法,但它并不总是指那个特定的提交。 HEAD^ really means "the commit which is the parent of the currently-checked-out commit". HEAD^真正的意思是“作为当前签出提交的父提交的提交”。

If you're on the master branch, then master^ would also work - and would not depend on what's currently checked out.如果您在master分支上,那么master^也可以工作 - 并且不依赖于当前签出的内容。 Even that changes whenever the master branch moves;即使master分支移动时也会发生变化; if for some reason you need an expression that would continue to refer to the particular commit, you could create a tag如果由于某种原因您需要一个继续引用特定提交的表达式,您可以创建一个标签

git tag mytag master^

and then mytag would work;然后mytag就可以工作了; or you could look up the commit ID of the commit in question (though it won't be a user-friendly name)或者您可以查找相关提交的提交 ID(尽管它不会是用户友好的名称)

But I digress...但我离题了...

This is different from - and more drastic than - merely loading the 2nd-to-last commit as the question title suggests.这不同于 - 并且比 - 仅仅像问题标题所暗示的那样加载倒数第二次提交。 To simply update your working tree with the 2nd-to-last commit's state, you could要简单地使用倒数第二次提交的状态更新您的工作树,您可以

git checkout HEAD^

This moves the HEAD (ie changes what's checked out) and updates the index and work tree, but it still keeps the commit on master in case you need it again later.这会移动HEAD (即更改已检出的内容)并更新索引和工作树,但它仍将提交保留在master ,以防您以后再次需要它。 However, it also puts you in a state called "detached head", meaning that git isn't really expecting you to create new commits from here.然而,它也会让你处于一种称为“分离头”的状态,这意味着 git 并不真正期望你从这里创建新的提交。

If you want to both keep the old commit around, and be in a state where you can add commits on top of commit_1 , then you need to create a new branch.如果您想保留旧提交,并处于可以在commit_1之上添加提交的commit_1 ,那么您需要创建一个新分支。 If you did the previous checkout command, you could then如果您执行了之前的结帐命令,则可以

git checkout -b mybranch

and new changes could be committed to the new branch while leaving the original commit_2 on master .并且可以将新更改提交到新分支,同时将原始commit_2留在master Or, you could put a branch (or tag) on commit_2 and then move master back to commit_1 .或者,您可以在commit_2上放置一个分支(或标签),然后将master移回commit_1

git checkout master
git branch mybranch
git reset --hard HEAD^

That may seem like a confusing number of options;这似乎是一个令人困惑的选项; but really that's because each one serves a somewhat different purpose, and only someone involved in your project is likely to have all the context to decide which makes sense.但实际上这是因为每个人都有不同的目的,只有参与您的项目的人才有可能拥有所有上下文来决定哪个有意义。

So tl;dr - it sounds like you might be looking for git reset , but just doing that alone is only suitable if you really intend to permanently discard the changes from commit_2 .所以 tl;dr -听起来您可能正在寻找git reset ,但仅当您真的打算永久丢弃来自commit_2的更改时才适合单独这样做。 If you really would want to preserve them for future reference (But maybe just weren't sure if that's an option) then there are ways to do that, too.如果您真的想保留它们以备将来参考(但也许只是不确定这是否是一种选择),那么也有办法做到这一点。

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

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