如何重命名 git stash?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25931026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I rename a git stash?
提问by mikemaccana
I have a stash with an incorrect name. I would like to fix the name so it's accurate.
我有一个名字不正确的藏匿处。我想修正这个名字,让它准确无误。
How can I rename a stash?
如何重命名存储?
回答by qzb
Let's assume your stash list looks like this:
假设您的存储列表如下所示:
$ git stash list
stash@{0}: WIP on master: Add some very important feature
stash@{1}: WIP on master: Fix some silly bug
First, you must remove stash entry which you want to rename:
首先,您必须删除要重命名的存储条目:
$ git stash drop stash@{1}
Dropped stash@{1} (af8fdeee49a03d1b4609f294635e7f0d622e03db)
Now just add it again with new message using sha of commit returned after dropping:
现在只需使用删除后返回的提交 sha 再次添加新消息:
$ git stash store -m "Very descriptive message" af8fdeee49a03d1b4609f294635e7f0d622e03db
And that's it:
就是这样:
$ git stash list
stash@{0}: Very descriptive message
stash@{1}: WIP on master: Add some very important feature
This solution requires git 1.8.4 or later, and yes, it works with dirty working directory too.
此解决方案需要 git 1.8.4 或更高版本,是的,它也适用于脏工作目录。
回答by Julien Carsique
Unless you do it manually or contribute an improvement to Git, you can use an alias:
除非您手动执行此操作或对 Git 做出改进,否则您可以使用别名:
git config --global alias.stash-rename '!_() { rev=$(git rev-parse ) && git stash drop || exit 1 ; git diff-index --quiet HEAD; s=$?; [ $s != 0 ] && git stash save "tmp stash from stash-rename"; git stash apply $rev && shift && git stash save "$@" && [ $s != 0 ] && git stash pop stash@{1}; }; _'
Usage: "git stash-rename <stash> [save options] [<message>]
"
用法:" git stash-rename <stash> [save options] [<message>]
"
With [save options]
any option of git stash save
: [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all]
使用以下[save options]
任何选项git stash save
:[-p|--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all]
Example:
例子:
$ git stash list
stash@{0}: On master: Pep8 format
stash@{1}: On master: co other than master with local changes
stash@{2}: On master: tests with deployAtEnd
# Let's say I want to rename the stash@{2} adding an issue reference:
$ git stash-rename stash@{2} NXP-13971-deployAtEnd
$ git stash list
stash@{0}: On master: NXP-13971-deployAtEnd
stash@{1}: On master: Pep8 format
stash@{2}: On master: co other than master with local changes
That will work even if you have local unstaged changes :)
即使您有本地未暂存的更改,这也将起作用:)
EDIT 2016/02/22
编辑 2016/02/22
Simplified script, credits to qzb, https://stackoverflow.com/a/35549615/515973
简化脚本,归功于qzb,https: //stackoverflow.com/a/35549615/515973
git config --global alias.stash-rename '!_() { rev=$(git rev-parse ) && git stash drop || exit 1 ; git stash store -m "" $rev; }; _'
Usage: "git stash-rename <stash> [<message>]
"
用法:" git stash-rename <stash> [<message>]
"
回答by Sandra
It's very simple. First, undo the last stash with:
这很简单。首先,使用以下命令撤消最后一次存储:
git stash pop
After this, yo can save the stash with a customized name in this way:
在此之后,您可以通过这种方式使用自定义名称保存存储:
git stash save "your explanatory name"
I hope it useful for you. :)
我希望它对你有用。:)
回答by Tino
For the benefit of the reader, here is an extension to the currently accepted and correct answer.
为了读者的利益,这里是对当前接受的正确答案的扩展。
If you not only want to correct the stash message and also want to correct the commit message of the stash, such that
如果您不仅要更正 stash 消息,还想更正 stash 的提交消息,例如
git stash list
and
和
git log --oneline -1 stash
both agree to what is shown, you need a bit more. There might be a better way to do it, but this recipe here is easy to understand, I hope.
双方都同意显示的内容,您需要多一点。可能有更好的方法来做到这一点,但我希望这里的这个食谱很容易理解。
To be able to do git commit --amend
you need to be on the TIP of a branch. Hence the solution is:
为了能够做到这一点,git commit --amend
您需要在分支机构的 TIP 上。因此解决方案是:
git checkout -b scratch stash@{1}
git stash drop stash@{1}
git commit --amend -m "$MESSAGE"
git stash store -m "$MESSAGE" HEAD
git checkout master
git branch -D scratch
Explained:
解释:
- Create a new (not yet existing) "scratch" branch from the "stash in question" and switch to it
- Remove the old stash. This is safe, as we still have this on the branch.
- Use
git commit --amend
to replace the commit message, this changing the SHA of the "stash in question" - Store the stash, based on the qzb's answer
- Switch back (which assumes you came from "master") and cleanup
- 从“有问题的藏匿处”创建一个新的(尚不存在的)“scratch”分支并切换到它
- 删除旧的藏匿处。这是安全的,因为我们仍然在分支上有这个。
- 使用
git commit --amend
更换提交信息,这改变了“有问题藏匿”的SHA - 根据qzb 的回答存储存储
- 切换回来(假设您来自“master”)并清理
Drawbacks:
缺点:
This switches branches temporarily. So this recipe can only be applied when
git status --porcelain
is clean (read: does not output anything)It renumbers the stashes, so the changed stash becomes
stash@{0}
You need to enter the
$MESSAGE
twice or use some environment variable (in the example:MESSAGE
)You need to find an unused branch name
这会暂时切换分支。所以这个配方只能在
git status --porcelain
干净时应用(阅读:不输出任何东西)它对藏匿处重新编号,因此更改后的藏匿处变为
stash@{0}
您需要输入
$MESSAGE
两次或使用一些环境变量(在本例中:MESSAGE
)您需要找到一个未使用的分支名称
There are ways to do this without switching branches, but this is beyond the scope of this answer.
有一些方法可以在不切换分支的情况下做到这一点,但这超出了本答案的范围。
Example
例子
git init scratch
cd scratch
for a in A B C D; do date >$a; git add $a; git commit -m $a; done
for a in X Y; do echo $a > Z; git stash save --all; done
git log --oneline --graph --decorate --all; git stash list
Output
输出
*-. e0e281b (refs/stash) WIP on master: 8bdcc32 D
|\ \
| | * 4d62f52 untracked files on master: 8bdcc32 D
| * 096f158 index on master: 8bdcc32 D
|/
* 8bdcc32 (HEAD, master) D
* c84c659 C
* 49bb2da B
* b1852c6 A
stash@{0}: WIP on master: 8bdcc32 D
stash@{1}: WIP on master: 8bdcc32 D
Now without changing commit (note: the SHA in following will be different at your side):
现在无需更改提交(注意:以下 SHA 在您身边会有所不同):
git stash drop stash@{1}
git stash store -m ...changed... 2fbf9007dfdfb95ae269a19e13b8b9ca3e24181c
git log --oneline --graph --decorate --all; git stash list
Output
输出
*-. 2fbf900 (refs/stash) WIP on master: 8bdcc32 D
|\ \
| | * 246dc5c untracked files on master: 8bdcc32 D
| * 80c5ea0 index on master: 8bdcc32 D
|/
* 8bdcc32 (HEAD, master) D
* c84c659 C
* 49bb2da B
* b1852c6 A
stash@{0}: ...changed...
stash@{1}: WIP on master: 8bdcc32 D
As you can see, stash@{0}
still is shown as 2fbf900 (refs/stash) WIP on master: 8bdcc32 D
in git log
. If you look carefully, you will see, that several commits have changed SHA. This is due to how stashes are handled (parents are included of the SHA, and stashes have their stashes as parent).
正如你所看到的,stash@{0}
仍然显示为2fbf900 (refs/stash) WIP on master: 8bdcc32 D
在git log
。如果你仔细观察,你会发现,有几次提交改变了 SHA。这是由于 stash 的处理方式(父项包含在 SHA 中,并且 stashes 将其 stashes 作为父项)。
Fix that:
修复:
git checkout -b scratch stash
git stash drop
git commit --amend -m ...changed...
git stash store -m ...changed... HEAD
git checkout master
git branch -D scratch
git log --oneline --graph --decorate --all; git stash list
Output
输出
*-. 4d55186 (refs/stash) ...changed...
|\ \
| | * 246dc5c untracked files on master: 8bdcc32 D
| * 80c5ea0 index on master: 8bdcc32 D
|/
* 8bdcc32 (HEAD, master) D
* c84c659 C
* 49bb2da B
* b1852c6 A
stash@{0}: ...changed...
stash@{1}: WIP on master: 8bdcc32 D
As you also can see, refs/stash
has a changed SHA, too.
正如您所看到的,refs/stash
SHA 也发生了变化。
回答by A1ternat1ve
I don't think it is possible to do so. There has been a proposal for stash renaming,but it has not been implemented yet.
我认为这是不可能的。有一个关于 stash 重命名的提议,但尚未实施。
My general idea is:
Implement a new
git reflog update
command that updates the message associated with a specific reflog entry. To do this, a newupdate_reflog_ent()
function (in reflog.c) would change the message associated with the specific reflog entry to update. Anupdate_reflog()
function would usefor_each_reflog_ent()
withupdate_reflog_ent
to actually do the change.A
git stash rename
command would then only need to callgit reflog update
with the appropriate ref and new message.
我的总体思路是:
实施一个新
git reflog update
命令来更新与特定 reflog 条目关联的消息。为此,一个新update_reflog_ent()
函数(在reflog.c 中)将更改与特定 reflog 条目关联的消息以进行更新。一个update_reflog()
函数将使用for_each_reflog_ent()
withupdate_reflog_ent
来实际进行更改。然后,
git stash rename
命令只需要git reflog update
使用适当的引用和新消息进行调用。
Or you could, of course, pop the stash and do a git stash save [message]
或者,当然,你可以弹出藏匿处并做一个 git stash save [message]
回答by Radon Rosborough
Here is a modified version of Julien's aliasthat lets you properly deal with the On <branch>
prefix usually prepended to stash names:
这是Julien 别名的修改版本,可让您正确处理On <branch>
通常添加到存储名称的前缀:
git config --global alias.stash-rename '!_() { newmsg="" && stash=${2:-"stash@{0}"} && newbranch="" && sha=$(git rev-parse "$stash") && olddesc="$(git stash list --format=%gs -1 "$stash")" && newdesc="$(if [[ "$newbranch" = "." ]]; then echo "$newmsg"; else if [[ -n "$newbranch" ]]; then echo "On $newbranch: $newmsg"; else if [[ "$olddesc" =~ ":" ]]; then echo "$(echo "$olddesc" | cut -f1 -d":"): $newmsg"; else echo "$newmsg"; fi; fi; fi)" && git stash drop "$stash" > /dev/null || exit 1; git stash store -m "$newdesc" "$sha" && git stash list; }; _'
Syntax:
句法:
git stash-rename <new-name> [<stash> [<new-branch-name> | .]]
Example usage:
用法示例:
repo[master] % touch tmp && git add tmp && git stash save first
Saved working directory and index state On master: first
HEAD is now at bd62064 Initial commit
repo[master] % touch tmp && git add tmp && git stash save second
Saved working directory and index state On master: second
HEAD is now at bd62064 Initial commit
repo[master] % git stash list
stash@{0}: On master: second
stash@{1}: On master: first
repo[master] % git stash-rename renamed
stash@{0}: On master: renamed
stash@{1}: On master: first
repo[master] % git stash-rename also-renamed stash@{1}
stash@{0}: On master: also-renamed
stash@{1}: On master: renamed
repo[master] % git stash-rename branch-changed stash@{0} new-branch
stash@{0}: On new-branch: branch-changed
stash@{1}: On master: renamed
repo[master] % git stash-rename branch-name-persists
stash@{0}: On new-branch: branch-name-persists
stash@{1}: On master: renamed
repo[master] % git stash-rename no-branch stash@{0} .
stash@{0}: no-branch
stash@{1}: On master: renamed
repo[master] % git stash-rename renamed
stash@{0}: renamed
stash@{1}: On master: renamed
repo[master] % git stash-rename readd-branch stash@{0} develop
stash@{0}: On develop: readd-branch
stash@{1}: On master: renamed
Most of the command is for parsing the arguments and figuring out what should be done to the branch name. The git
tools used are as follows:
大多数命令用于解析参数并确定应该对分支名称做什么。使用的git
工具如下:
git rev-parse <stash>
to find the SHA of the stash.git stash list --format=%gs -1 <stash>
to find the reflog subjectof the stash. Note that this is different from the commit messageof the stash, which is not changed by this command. The reflog subject is what shows up ingit stash list
, and you can change the reflog subject without changing the hashes of the commits associated with the stashes. However, you can always find the original commit message, so don't usegit stash-rename
to remove sensitive information!git stash drop <stash>
to drop the old referenceto the stash (but we still have the SHA, so it's not lost).git stash store -m <new-message> <sha>
to save a new referenceto the stash with the same commit information but a different reflog subject.git stash list
to list the stashes after the operation is finished. Note that new stashes are always pushed to the beginning of the list. It would be necessary to re-push allthe stashes before the stash of interest in order to restore its original position.
git rev-parse <stash>
找到存储的SHA。git stash list --format=%gs -1 <stash>
找到stash的reflog 主题。请注意,这与 stash的提交消息不同,该消息不会被此命令更改。reflog 主题显示在 中git stash list
,您可以更改 reflog 主题,而无需更改与存储关联的提交的哈希值。但是,您始终可以找到原始提交消息,因此请勿git stash-rename
用于删除敏感信息!git stash drop <stash>
删除对stash的旧引用(但我们仍然有 SHA,所以它不会丢失)。git stash store -m <new-message> <sha>
使用相同的提交信息但不同的reflog 主题保存对 stash的新引用。git stash list
在操作完成后列出隐藏。请注意,新的 stash 总是被推到列表的开头。有必要在感兴趣的藏匿处之前重新推送所有藏匿处,以恢复其原始位置。
回答by yoel neuman
Simplest way: pop your stash with git stash pop then save it again with git stash save your-name
最简单的方法:用 git stash pop 弹出你的 stash 然后用 git stash save your-name 再次保存它