且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在检出以前的提交后返回到最新的提交?

更新时间:2022-10-19 13:47:38

如果你知道你想返回的提交是某个分支的头部,或标记,那么你可以只是

  git checkout branchname 

您也可以使用 git reflog 来查看您的HEAD(或其他任何ref)指向的其他提交过去。






编辑添加:

版本的Git,如果你只运行一次 git checkout 或别的东西来移动你的 HEAD ,你也可以执行

  git checkout  -  

切换回最后一次结帐前的位置。这是出于类似shell的习惯用法 cd - 的动机,可以返回到之前的任何工作目录。


I sometimes check out some previous version of the code to examine or test. I have seen instructions on what to do if I wish to modify previous commits -- but suppose I make no changes. After I've done e.g. git checkout HEAD^, how do I get back to the tip of the branch?.. git log no longer shows me the SHA of the latest commit.

If you know the commit you want to return to is the head of some branch, or is tagged, then you can just

git checkout branchname

You can also use git reflog to see what other commits your HEAD (or any other ref) has pointed to in the past.


Edited to add:

In newer versions of Git, if you only ran git checkout or something else to move your HEAD once, you can also do

git checkout -

to switch back to wherever it was before the last checkout. This was motivated by the analogy to the shell idiom cd - to go back to whatever working directory one was previously in.