git 签出之前的提交后如何返回到最新的提交?

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

How to get back to the latest commit after checking out a previous commit?

gitversion-control

提问by Leo Alekseyev

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 logno longer shows me the SHA of the latest commit.

我有时会查看一些以前版本的代码来检查或测试。如果我想修改以前的提交,我已经看到了如何做的说明——但假设我没有做任何更改。在我完成 eg 之后,我git checkout HEAD^如何回到分支的顶端?..git log不再向我显示最新提交的 SHA。

回答by Phil Miller

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 reflogto see what other commits your HEAD (or any other ref) has pointed to in the past.

您还可以使用git reflog查看您的 HEAD(或任何其他参考)过去指出的其他提交。



Edited to add:

编辑添加:

In newer versions of Git, if you only ran git checkoutor something else to move your HEADonce, you can also do

在较新版本的 Git 中,如果您只运行git checkout或其他东西来移动HEAD一次,您也可以这样做

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.

切换回上次结帐之前的位置。这是由于与 shell 习惯用法的类比,即cd -返回到先前所在的任何工作目录。

回答by Bruce Wells

git checkout master

git结帐大师

master is the tip, or the last commit. gitkwill only show you up to where you are in the tree at the time. git reflogwill show all the commits, but in this case, you just want the tip, so git checkout master.

master 是提示,或最后一次提交。 gitk只会显示您当时在树中的位置。 git reflog将显示所有提交,但在这种情况下,您只需要提示,因此 git checkout master。

回答by 816-8055

Came across this question just now and have something to add

刚刚遇到这个问题,有什么要补充的

To go to the most recent commit:

要转到最近的提交:

git checkout $(git log --branches -1 --pretty=format:"%H")

Explanation:

解释:

git log --branchesshows log of commits from all local branches
-1limit to one commit → most recent commit
--pretty=format:"%H"format to only show commit hash
git checkout $(...)use output of subshell as argument for checkout

git log --branches显示来自所有本地分支的提交日志
-1限制为一次提交 → 最近的提交
--pretty=format:"%H"格式仅显示提交哈希
git checkout $(...)使用 subshel​​l 的输出作为结帐的参数

Note:

笔记:

This will result in a detached head though (because we checkout directly to the commit). This can be avoided by extracting the branch name using sed, explained below.

这将导致分离头(因为我们直接结帐到提交)。这可以通过使用 提取分支名称来避免sed,解释如下。



To go to the branch of the most recent commit:

转到最近提交的分支:

git checkout $(git log --branches -1 --pretty=format:'%D' | sed 's/.*, //g')

Explanation:

解释:

git log --branchesshows log of commits from all local branches
-1limit to one commit → most recent commit
--pretty=format:"%D"format to only show ref names
| sed 's/.*, //g'ignore all but the last of multiple refs (*)
git checkout $(...)use output of subshell as argument for checkout

git log --branches显示来自所有本地分支的提交日志
-1限制为一次提交 → 最近的提交
--pretty=format:"%D"格式仅显示引用名称
| sed 's/.*, //g'忽略除多个引用中的最后一个之外的所有引用 (*)
git checkout $(...)使用 subshel​​l 的输出作为检出的参数

*) HEAD and remote branches are listed first, local branches are listed last in alphabetically descending order, so the one remaining will be the alphabetically first branch name

*) HEAD 和远程分支列在最前面,本地分支按字母降序列在最后,所以剩下的将是按字母顺序排列的第一个分支名称

Note:

笔记:

This will always only use the (alphabetically) first branch name if there are multiple for that commit.

如果该提交有多个,这将始终仅使用(按字母顺序排列)第一个分支名称。



Anyway, I think the best solution would just be to display the ref names for the most recent commit to know where to checkout to:

无论如何,我认为最好的解决方案就是显示最近提交的引用名称,以了解在哪里结帐:

git log --branches -1 --pretty=format:'%D'

E.g. create the alias git topfor that command.

例如git top,为该命令创建别名。

回答by tanascius

Have a look at the graphical GUI ... gitkit shows all commits. Sometimes it is easier to work graphical ... ^^

看看图形 GUI ……gitk它显示了所有提交。有时图形工作更容易...... ^^

回答by hothead1000

You can use one of the following git command for this:

您可以为此使用以下 git 命令之一:

git checkout master
git checkout branchname

回答by abdesselam

show all branches and commit
git log --branches --oneline

显示所有分支并提交
git log --branches --oneline

show last commit
git log --branches -1 --oneline

显示最后一次提交
git log --branches -1 --oneline

show before last commit
git log --branches -2 --oneline

在上次提交之前显示
git log --branches -2 --oneline

回答by Atif Majeed

git reflog //find the hash of the commit that you want to checkout
git checkout <commit number>>

回答by ConorR

If you have a branch different than master, one easy way is to check out that branch, then check out master. Voila, you are back at the tip of master. There's probably smarter ways...

如果你有一个不同于 master 的分支,一个简单的方法是检查那个分支,然后检查 master。瞧,你又回到了大师的尖端。可能有更聪明的方法......

回答by Ankit Singh

You can simply do git pull origin branchname. It will fetch the latest commit again.

你可以简单地做git pull origin branchname。它将再次获取最新的提交。

回答by Hymanie Xu

If your latest commit is on the master branch, you can simply use

如果您的最新提交在 master 分支上,您可以简单地使用

git checkout master