Git命令保存一个藏匿而不修改工作树? [英] Git command to save a stash without modifying working tree?

查看:133
本文介绍了Git命令保存一个藏匿而不修改工作树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想用git命令来保存一个存储而不修改我的工作树,作为一个轻量级的备份,可以安全地从任何git重置,或者我可能做的任何事情来搞砸我的索引。基本上,git stash save&& git stash的功能相当于应用,除非工作副本从未被触及,因为这可能会导致某些文本编辑器/ IDE的胡思乱想。



  git update-ref refs / stash`git存储创建类似这样的东西正在接近我想要的东西,但不是很完美: Stash message`

这在功能上起作用,但是我遇到的问题是没有存储消息即使实际的隐藏提交确实在我的消息中有,我也会在git存储列表中出现。考虑到存储容量有多大,存储消息非常重要。

解决方案

感谢Charles的提示,我鞭打了一个bash脚本来做我想要的东西(我遇到的问题只是一个别名)。它需要一个可选的存储消息,就像git存储保存一样。

 #!/ bin / sh 
#如果没有提供,它将使用由git stash生成的默认消息。
#git-stash-snap
#将工作树的快照保存到存储中,而无需修改工作树。
#第一个参数(可选)是隐藏消息。
if [-n$ 1];然后
git update-ref -m$ 1refs / stash$(git stash create \$ 1 \)
else
HASH =`git stash create`
MESSAGE =`git log --no-walk --pretty =tformat:% - s$ HASH`
git update-ref -m$ MESSAGErefs / stash$ HASH
fi

编辑:正如下面的评论中指出的,将此脚本保存为 git-stash-snap 你的路径中的某处足以通过输入 git stash-snap 来调用它。



这里的好处是,即使您使用此方法删除存储,您仍然可以使用悬挂的git log [commit-hash]来查看存储消息提交!



编辑:自git 2.6.0以来,您可以将 - create-reflog 添加到 update-ref ,然后 git stash list 即使 git stash 以前没有使用过。



编辑:Git引入了一个新的存储子命令,名为 stash push ,所以我更新了我的建议,将这个脚本从 git-stash-push 命名为 git-stash-snap


I have been wanting to use a git command that saves a stash without modifying my working tree, as a lightweight backup that's safe from any git resets or whatever I might do to screw up my index. Basically the functional equivalent of "git stash save && git stash apply" except that the working copy is never touched, since this can make certain text editors/IDE's cranky.

Something like this is approaching what I want, but not quite:

git update-ref refs/stash `git stash create "Stash message"`

This works functionally, but the issue I'm having is that no stash message shows up in "git stash list" even though the actual stash commit does have my message in it. Considering how large a stash can get, stash messages are pretty important.

解决方案

Thanks to Charles' tip, I whipped up a bash script to do exactly what I wanted (I was running into issues implementing this as only an alias). It takes an optional stash message just like git stash save. If none is supplied it will use the default message generated by git stash.

#!/bin/sh
#
# git-stash-snap
# Save snapshot of working tree into the stash without modifying working tree.
# First argument (optional) is the stash message.
if [ -n "$1" ]; then
        git update-ref -m "$1" refs/stash "$(git stash create \"$1\")"
else
        HASH=`git stash create`
        MESSAGE=`git log --no-walk --pretty="tformat:%-s" "$HASH"`
        git update-ref -m "$MESSAGE" refs/stash "$HASH"
fi

Edit: As pointed out in a comment below, saving this script as git-stash-snap somewhere in your path is sufficient to be able to invoke it by typing git stash-snap.

The nice thing here is that even if you drop a stash made with this method, you will still be able to see the stash message using git log [commit-hash] of the dangling commit!

Edit: since git 2.6.0 you can add --create-reflog to update-ref and then git stash list will show this even if git stash was not used before.

Edit: Git has introduced a new stash subcommand called stash push so I have updated my recommendation for naming this script from git-stash-push to git-stash-snap.

这篇关于Git命令保存一个藏匿而不修改工作树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆