在检查了上一个提交后,如何返回到最新的提交?
我有时会查看一些以前版本的代码来检查或测试。我已经看到了有关如果我想修改以前的提交该怎么做的说明——但假设我不做任何更改。在我完成例如 git checkout HEAD^
后,如何返回分支的提示?.. git log
不再向我显示最新的 SHA犯罪。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您知道要返回的提交是某个分支的头部,或者已标记,那么您可以
使用 git reflog 来查看其他提交的 HEAD(或任何其他引用) ) 过去曾指出过。
编辑添加:
在较新版本的 Git 中,如果您只运行 git checkout 或其他东西来移动您的 HEAD 一次,您也可以
切换回原来的位置最后一次结账之前。这是因为类似于 shell 习惯用法
cd -
返回到之前所在的工作目录。If you know the commit you want to return to is the head of some branch, or is tagged, then you can just
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 yourHEAD
once, you can also doto 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.git checkout master
master 是提示,或者是最后一次提交。 gitk 只会显示您当时在树中的位置。 git reflog 将显示所有提交,但在这种情况下,您只需要提示,因此 git checkout master。
git checkout master
master is the tip, or the last commit. gitk will only show you up to where you are in the tree at the time. git reflog will show all the commits, but in this case, you just want the tip, so git checkout master.
刚刚遇到这个问题,需要添加一些内容
要转到最近的提交:
说明:
git log --branches
显示日志来自所有本地分支的提交-1
限制一次提交 → 最近一次提交--pretty=format:"%H"
格式仅显示提交哈希git checkout $(...)
使用子shell的输出作为签出的参数注意:
这将导致分离的头(因为我们直接签出到提交)。可以通过使用
sed
提取分支名称来避免这种情况,如下所述。转到最近提交的分支:
说明:
git log --branches
显示所有本地分支的提交日志-1
限制一次提交 → 最近一次提交--pretty=format:"%D"
格式仅显示参考名称<代码>| sed 's/.*, //g' 忽略多个引用中除最后一个之外的所有引用 (*)
git checkout $(...)
使用 subshell 的输出作为 checkout 的参数*) HEAD 和远程分支首先列出,本地分支按字母降序排列在最后,所以剩下的一个将是按字母顺序排列的第一个分支名称
注意:
如果该提交有多个分支,则始终仅使用(按字母顺序排列)的第一个分支名称。
无论如何,我认为最好的解决方案就是显示最近提交的引用名称,以了解在哪里签出:
例如,为该命令创建别名 git top 。
Came across this question just now and have something to add
To go to the most recent commit:
Explanation:
git log --branches
shows log of commits from all local branches-1
limit to one commit → most recent commit--pretty=format:"%H"
format to only show commit hashgit checkout $(...)
use output of subshell as argument for checkoutNote:
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.To go to the branch of the most recent commit:
Explanation:
git log --branches
shows log of commits from all local branches-1
limit 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*) 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
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:
E.g. create the alias
git top
for that command.看一下图形 GUI ...
gitk
它显示了所有提交。有时图形化工作更容易......^^Have a look at the graphical GUI ...
gitk
it shows all commits. Sometimes it is easier to work graphical ... ^^您可以为此使用以下 git 命令之一:
You can use one of the following git command for this:
如果您的最新提交位于 master 分支上,您可以简单地使用
If your latest commit is on the master branch, you can simply use
显示所有分支并提交
git log --branches --oneline
显示最后一次提交
git log --branches -1 --oneline
显示上次提交之前
git log --branches -2 --oneline
show all branches and commit
git log --branches --oneline
show last commit
git log --branches -1 --oneline
show before last commit
git log --branches -2 --oneline
对于
git
版本 >=2.33.0允许您检出之前检出的提交。因此,您可以通过在两次提交之间切换来来回切换。请注意,
-d
标志允许您在分离状态下的提交之间冲浪。For
git
versions >=2.33.0allows you check out to the previously checked out commit. So, you can go back-and-forth by switching between two commits. Please notice that
-d
flag allows you to surf among commits in a detached state.如果您有一个与 master 不同的分支,一种简单的方法是检查该分支,然后检查 master。瞧,你又回到了大师的尖端。可能有更聪明的方法......
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...
您只需执行
git pull originbranchname
即可。它将再次获取最新的提交。You can simply do
git pull origin branchname
. It will fetch the latest commit again.