Git 推送 &波普德?即,结帐最后状态

发布于 2024-10-19 13:36:10 字数 189 浏览 5 评论 0原文

我正在编写一个 Bash 脚本,我想签出一个标签,然后签回到我开始的地方。

我尝试了 git co HEAD@{1},但是当从 master 开始时,这让我回到了 master 的提交 SHA,但头分离了。

有没有类似 pushd & 的东西?用于 Git 的 popd

I'm writing a Bash script, and I want to checkout a tag and then checkout back to where I started.

I tried git co HEAD@{1}, but when starting at master, that takes me back to the commit SHA of master but with a detatched head.

Is there something like pushd & popd for Git?

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

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

发布评论

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

评论(4

感情旳空白 2024-10-26 13:36:10

git checkout @{-1} 可以缩写为 git checkout -

来自联机帮助页:

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

git checkout @{-1} which can be abbreviated to git checkout -.

From the manpage:

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}".

金橙橙 2024-10-26 13:36:10

编辑:如果您不想像 Pushd/popd 那样保留显式历史记录,那么 wnoise 的建议将会起作用。如果您这样做(并且不希望普通的checkout影响您的LRU):

我不知道有什么可以开箱即用,但它并不难破解沿着这些思路将一些东西结合在一起。如果将名为 git-foo 的文件添加到 PATH,您将获得一个新的 git foo 命令。因此, git-pushd 可能看起来像:

#!/bin/bash

SUBDIRECTORY_OK=1
. $(git --exec-path)/git-sh-setup

git symbolic-ref HEAD | sed s_refs/heads/__ >> $GIT_DIR/.pushd
git checkout "$@"

和 git-popd :

#!/bin/bash

SUBDIRECTORY_OK=1
. $(git --exec-path)/git-sh-setup

REF=$(head -n1 $GIT_DIR/.pushd)

[ -n "$REF" ] || die "No refs to pop"
git checkout "$REF" && sed -i -e '1d' $GIT_DIR/.pushd

EDIT: wnoise's suggestion will work if you don't want to keep an explicit history the way pushd/popd does. If you do (and don't want an ordinary checkout to affect your LRU):

I don't know of anything that will do what you want out of the box, but it's not to hard to hack together something along those lines. If you add a file named git-foo to your PATH, you get a new git foo command. So, git-pushd could look like:

#!/bin/bash

SUBDIRECTORY_OK=1
. $(git --exec-path)/git-sh-setup

git symbolic-ref HEAD | sed s_refs/heads/__ >> $GIT_DIR/.pushd
git checkout "$@"

And git-popd:

#!/bin/bash

SUBDIRECTORY_OK=1
. $(git --exec-path)/git-sh-setup

REF=$(head -n1 $GIT_DIR/.pushd)

[ -n "$REF" ] || die "No refs to pop"
git checkout "$REF" && sed -i -e '1d' $GIT_DIR/.pushd
青丝拂面 2024-10-26 13:36:10

您还可以使用别名和 /tmp/gitStack 作为临时文件来跟踪堆栈。

查看我创建的主要别名:

[alias]
  pushd         = "!f() { _BRANCH=$(git symbolic-ref head); _BRANCH=${_BRANCH#'refs/heads/'}; echo $_BRANCH >> /tmp/gitStack; git checkout $@;}; f "
  pop           = "!f() { _BRANCH=$(tail -1 /tmp/gitStack); git checkout ${_BRANCH%*:}; head -n+1 /tmp/gitStack > /tmp/_gitStack; mv /tmp/_gitStack /tmp/gitStack; }; f"
  pop-clear     = "!f() { rm /tmp/gitStack; }; f"
  pop-list      = "!f() { cat /tmp/gitStack 2> /dev/null; }; f"

You can also do it with aliases and /tmp/gitStack as a temporary file to track your stack.

Checkout the main aliases I've made:

[alias]
  pushd         = "!f() { _BRANCH=$(git symbolic-ref head); _BRANCH=${_BRANCH#'refs/heads/'}; echo $_BRANCH >> /tmp/gitStack; git checkout $@;}; f "
  pop           = "!f() { _BRANCH=$(tail -1 /tmp/gitStack); git checkout ${_BRANCH%*:}; head -n+1 /tmp/gitStack > /tmp/_gitStack; mv /tmp/_gitStack /tmp/gitStack; }; f"
  pop-clear     = "!f() { rm /tmp/gitStack; }; f"
  pop-list      = "!f() { cat /tmp/gitStack 2> /dev/null; }; f"
御弟哥哥 2024-10-26 13:36:10

在脚本中,首先保存当前分支(如 这个答案):

branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)"     # detached HEAD
branch_name=${branch_name##refs/heads/}

然后去签出你想要的标签,

git checkout -b tag_branch tag_name

在该分支上做你想做的事情,然后再次签出旧分支:

git checkout $branch_name

就是这样。

In your script, first save the current branch (like written in this answer):

branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)"     # detached HEAD
branch_name=${branch_name##refs/heads/}

then go and checkout the tag you want to

git checkout -b tag_branch tag_name

Do what you want to do on that branch, then checkout the old branch again:

git checkout $branch_name

That's it.

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