有没有办法 git checkout 以前的分支?

发布于 2024-12-01 17:37:17 字数 204 浏览 1 评论 0原文

我有点想要相当于 git 的 cd - 。如果我在分支 master 中并签出 foo,我希望能够输入类似 git checkout - 的内容以返回到 < code>master,并且能够再次键入它以返回到foo

有这样的事情存在吗?实施起来会不会很困难?

I sort of want the equivalent of cd - for git. If I am in branch master and I checkout foo, I would love to be able to type something like git checkout - to go back to master, and be able to type it again to return to foo.

Does anything like this exist? Would it be hard to implement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

病毒体 2024-12-08 17:37:17

来自 1.6.2 发行说明

@{-1} 是一种引用您所在的最后一个分支的方法。这是
不仅在需要对象名称的地方被接受,而且在任何有对象名称的地方都被接受
分支名称是预期的,并且就像您键入分支名称一样。
例如 git Branch --track mybranch @{-1}git merge @{-1}
git rev-parse --symbolic-full-name @{-1} 将按预期工作。

git checkout -git checkout @{-1} 的简写。

要查看之前结帐的列表:

<代码>i=0;而 [ $? -eq 0];我=$((i+1));回显-n“$i。”; git rev-parse --symbolic-full-name @{-$i} 2> /dev/null;完成

这个 Bash 一行脚本并不完美,但它应该适用于大多数情况。请注意,有时数字可能会跳过。

提示:您可以将其作为函数添加到.bashrc中。

From the release notes for 1.6.2

@{-1} is a way to refer to the last branch you were on. This is
accepted not only where an object name is expected, but anywhere a
branch name is expected and acts as if you typed the branch name.
E.g. git branch --track mybranch @{-1}, git merge @{-1}, and
git rev-parse --symbolic-full-name @{-1} would work as expected.

and

git checkout - is a shorthand for git checkout @{-1}.

To see the list of previous checkouts:

i=0; while [ $? -eq 0 ]; do i=$((i+1)); echo -n "$i. "; git rev-parse --symbolic-full-name @{-$i} 2> /dev/null; done

This Bash one-liner script is not perfect but it should work for most cases. Note that sometimes the number may skip.

Tip: You can add it to .bashrc as a function.

岁月静好 2024-12-08 17:37:17

现在最简单的方法是:

git checkout -

... 这是以下的别名:

git checkout @{-1}

git checkout minus

如果您想了解更多信息,我在这里写了整篇文章:签出 Git 中的上一个分支

The simplest way of doing this nowadays is:

git checkout -

... which is an alias of:

git checkout @{-1}

git checkout minus

If you want to know more about this, I wrote an entire article about it here: Checkout The Previous Branch In Git.

国产ˉ祖宗 2024-12-08 17:37:17

Git 版本 2.23 引入了 git switch 命令,您可以使用它来执行此操作(以及更多操作)。引用官方文档:

切换到指定分支。工作树和索引被更新以匹配分支。所有新提交都将添加到此分支的提示中。

在您的具体情况下,您可以发出 git switch - 返回到您之前所在的分支。您可以再次执行相同的命令以返回到第一个分支。

该命令不太令人困惑且对初学者友好,因为它解决了常见的 使用 git checkout 时出现的混乱

Git version 2.23 introduced the git switch command which you can use to do that (and more). Quoting the official documentation:

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

In your specific case you can issue git switch - to go back to the branch you were previously on. You can execute the same command again to return to the first branch.

This command is less confusing and friendly-to-beginners as it addresses a common confusion that arises when using git checkout.

渔村楼浪 2024-12-08 17:37:17

正如 @Karl 所指出的,并来自 git checkout 手册:

作为一种特殊情况,“@{-N}”语法用于检查第 N 个最后分支
脱离分支(而不是分离)。您还可以指定 - 这是
与“@{-1}”同义。

因此, git checkout -git checkout @{-1} 在这种情况下都可以工作

我认为最接近的是使用 git reflog并解析最新的从branch1移动到branch2git checkoutbranch1

As @Karl points out and from git checkout manual:

As a special case, the "@{-N}" syntax for the N-th last branch checks
out the branch (instead of detaching). You may also specify - which is
synonymous with "@{-1}".

So both git checkout - and git checkout @{-1} would work in this case

Closest I believe is using the git reflog and parse the latest moving from branch1 to branch2 and git checkout branch1

隱形的亼 2024-12-08 17:37:17

我以同样的想法提出这个问题来检查我以前的分支。我在 Mac 中使用 ohmyz。下面的命令帮助了我。

$ gco -
$ git checkout -

I landed to this question with the same thought to checkout my previous branch. I'm using ohmyz in Mac. Below command helped me.

$ gco -
$ git checkout -
故笙诉离歌 2024-12-08 17:37:17

只需在前面的答案中添加一些更多细节即可了解 git checkout @{-N} 的工作机制。它会遍历 reflog 来检查结账历史记录,因此,如果您想自己实现类似的功能,您应该能够解析 git reflog 的输出,查找 checkout:线。您可以在 git 源 sha1_name.c 中检查实现,特别是函数 interpret_nth_prior_checkout

Just adding some more detail to the previous answers to understand the mechanism by which git checkout @{-N} works. It walks the reflog to inspect the checkout history, so if you wanted to implement something similar on your own you should be able to parse the output of git reflog looking for checkout: lines. You can check the implementation in the git source sha1_name.c, specifically the function interpret_nth_prior_checkout.

够钟 2024-12-08 17:37:17

以下是 Git 文档中描述其他答案给出的 git checkout -git checkout @{-1} 解决方案的部分的指针:

  • 当指定任何命令的 Git 修订版, @{-},例如 @{-1}意思是“在当前分支/提交之前签出的n个分支/提交”。 git checkout 的文档 重申:“您可以使用 @{-N} 语法来引用使用 git checkout 操作签出的最后 N 个分支/提交。”

  • 对于 git checkout 的参数,“您还可以指定 '-”,它与'@{-1}'。”

Here are pointers to the parts of Git’s documentation that describe the git checkout - and git checkout @{-1} solutions given by the other answers:

  • When specifying a Git revision for any command, @{-<n>}, e.g. @{-1} means “the nth branch/commit checked out before the current one.” The documentation for git checkout <branch> reiterates: “You can use the @{-N} syntax to refer to the N-th last branch/commit checked out using git checkout operation.”

  • For the <branch> argument of git checkout, “you may also specify ‘-’ which is synonymous to ‘@{-1}’.”

旧街凉风 2024-12-08 17:37:17

最流行的解决方案是:

git checkout @{-N}

其中 N - 在结帐历史记录中向后移动的分支的步骤计数。

The most popular solution is:

git checkout @{-N}

Where N - step count of the branches to move back on the checkout history.

爱已欠费 2024-12-08 17:37:17

请参阅 git chekoutgit switch 有不同的用途。它更像是 typescriptjavascript。请参阅 javascripttypescript 的一部分,类似地,git switch 具有 git checkout 的一些功能。

git checkout 主要用于提交和时间轴移动之间的切换。它也可以做 git switch 可以做的所有事情。

git switch 专门用于分支之间的切换,并完成与分支相关的所有工作。

所以更好用

git switch -

See git chekout and git switch have different purpose. Its more like typescript and javascript. See javascript is a part of typescript similarly git switch has some of the capabilities of git checkout.

git checkout is majorly used for switch between commit and travelling in timeline. It can also do all the things which git switch can.

git switch was specifically made for switch between branches and do all the work related to the branches.

So better to use

git switch -
夜声 2024-12-08 17:37:17

就我而言,我已从 master 切换到 gh-pages,这导致我的所有组件消失并被文件名“static”和其他文件替换。

git checkout -m master 帮助了

in my case I had switched from master to gh-pages which cause all my components to disappear and get replaced by a file names "static" and others.

git checkout -m master helped

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