在检查了上一个提交后,如何返回到最新的提交?

发布于 2024-08-24 23:31:15 字数 159 浏览 5 评论 0原文

我有时会查看一些以前版本的代码来检查或测试。我已经看到了有关如果我想修改以前的提交该怎么做的说明——但假设我不做任何更改。在我完成例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

如果没有 2024-08-31 23:31:15

如果您知道要返回的提交是某个分支的头部,或者已标记,那么您可以

git checkout branchname

使用 git reflog 来查看其他提交的 HEAD(或任何其他引用) ) 过去曾指出过。


编辑添加:

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

git checkout -

切换回原来的位置最后一次结账之前。这是因为类似于 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

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.

偷得浮生 2024-08-31 23:31:15

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.

流殇 2024-08-31 23:31:15

刚刚遇到这个问题,需要添加一些内容

要转到最近的提交:

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

说明:

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

注意:

这将导致分离的头(因为我们直接签出到提交)。可以通过使用 sed 提取分支名称来避免这种情况,如下所述。


转到最近提交的分支:

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

说明:

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

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

注意:

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


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

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

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

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 --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 hash
git checkout $(...) use output of subshell as argument for checkout

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.


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 --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:

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

E.g. create the alias git top for that command.

吾家有女初长成 2024-08-31 23:31:15
git reflog //find the hash of the commit that you want to checkout
git checkout <commit number>>
git reflog //find the hash of the commit that you want to checkout
git checkout <commit number>>
纵山崖 2024-08-31 23:31:15

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

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

悲念泪 2024-08-31 23:31:15

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

git checkout master
git checkout branchname

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

git checkout master
git checkout branchname
狼性发作 2024-08-31 23:31:15

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

git checkout master

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

git checkout master
瀞厅☆埖开 2024-08-31 23:31:15

显示所有分支并提交
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

提笔落墨 2024-08-31 23:31:15

对于 git 版本 >=2.33.0

git switch -d -

允许您检出之前检出的提交。因此,您可以通过在两次提交之间切换来来回切换。请注意,-d 标志允许您在分离状态下的提交之间冲浪。

For git versions >=2.33.0

git switch -d -

allows 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.

不醒的梦 2024-08-31 23:31:15

如果您有一个与 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...

や莫失莫忘 2024-08-31 23:31:15

您只需执行git pull originbranchname即可。它将再次获取最新的提交。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文