我有一堆名字不正确的东西。我想修改一下名字,让它更准确。
我如何重命名收藏?
我有一堆名字不正确的东西。我想修改一下名字,让它更准确。
我如何重命名收藏?
当前回答
下面是Julien的别名的修改版本,它可以让你正确地处理On <branch>前缀,通常用于存储名称:
git config --global alias.stash-rename '!_() { newmsg="$1" && stash=${2:-"stash@{0}"} && newbranch="$3" && 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; }; _'
语法:
git stash-rename <new-name> [<stash> [<new-branch-name> | .]]
使用示例:
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
该命令的大部分用于解析参数并确定应该对分支名称做什么。git使用的工具如下:
git rev-parse <stash> to find the SHA of the stash. git stash list --format=%gs -1 <stash> to find the reflog subject of the stash. Note that this is different from the commit message of the stash, which is not changed by this command. The reflog subject is what shows up in git 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 use git stash-rename to remove sensitive information! git stash drop <stash> to drop the old reference to the stash (but we still have the SHA, so it's not lost). git stash store -m <new-message> <sha> to save a new reference to 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 all the stashes before the stash of interest in order to restore its original position.
其他回答
我认为这是不可能的。有一个关于存储重命名的提议,但是还没有实现。
我的大意是: 实现一个新的git reflog update命令,更新与特定reflog条目关联的消息。为此,一个新的update_reflog_ent()函数(在reflog.c中)会将与特定reflog条目相关的消息更改为update。update_reflogg()函数将使用for_each_reflog_ent()和update_reflog_ent来实际执行更改。 git stash rename命令只需要使用适当的ref和new消息调用git reflog update。
当然,你也可以弹出隐藏,并做一个git隐藏保存[消息]
下面是Julien的别名的修改版本,它可以让你正确地处理On <branch>前缀,通常用于存储名称:
git config --global alias.stash-rename '!_() { newmsg="$1" && stash=${2:-"stash@{0}"} && newbranch="$3" && 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; }; _'
语法:
git stash-rename <new-name> [<stash> [<new-branch-name> | .]]
使用示例:
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
该命令的大部分用于解析参数并确定应该对分支名称做什么。git使用的工具如下:
git rev-parse <stash> to find the SHA of the stash. git stash list --format=%gs -1 <stash> to find the reflog subject of the stash. Note that this is different from the commit message of the stash, which is not changed by this command. The reflog subject is what shows up in git 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 use git stash-rename to remove sensitive information! git stash drop <stash> to drop the old reference to the stash (but we still have the SHA, so it's not lost). git stash store -m <new-message> <sha> to save a new reference to 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 all the stashes before the stash of interest in order to restore its original position.
这里有很多复杂的答案。我是这么说的:
首先让我们找到你的收藏索引:
git stash list
例如,现在将其应用于git stash apply {N}
git stash apply 2
现在可以用一条新消息保存更改
git stash push -m 'My descriptive stash message'
如果你想清理原来的存储,记得将索引增加1,因为新的存储会增加所有现有的索引(所以这里我们要N + 1)
git stash drop 3
除非你手动操作或对Git做出改进,否则你可以使用别名:
git config --global alias.stash-rename '!_() { rev=$(git rev-parse $1) && git stash drop $1 || 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}; }; _'
使用方法:"git stash-rename <stash>[保存选项][<消息>]"
使用[保存选项]任何git保存选项:[-p|——patch] [-k|——[no-]keep-index] [-q|——quiet] [-u|——include-untracked] [-a|——all]
例子:
$ 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
这将工作,即使你有本地的非阶段性变化:)
编辑2016/02/22
简化脚本,感谢qzb, https://stackoverflow.com/a/35549615/515973
git config --global alias.stash-rename '!_() { rev=$(git rev-parse $1) && git stash drop $1 || exit 1 ; git stash store -m "$2" $rev; }; _'
用法:"git stash-rename <stash>[<消息>]"
qzb的回答中描述的git stash store-方法只在两个地方之一更新了stash消息,导致许多git前端仍然显示旧消息。但是,可以创建一个提交,复制原始存储提交中的所有内容,但更改其消息。
Find the hashes for the stash commit's tree and parents: $ git show -s --pretty=raw stash@{0} commit f2adfc7bbebe852693ad8f6ac889e4923230c872 tree 8160d88c6e00e90fcfa183e09d2563f3cdfb304b parent a013bd8052d3260fbc95608ed69d0b9cfa0c77cb parent 5d5eb80dc03bea8ff2bdd38962a1259b7725d169 author ... committer ... Test stash Create a new commit with the same tree and parents but a different message: $ git commit-tree 8160d88c6e00e90fcfa183e09d2563f3cdfb304b \ -p a013bd8052d3260fbc95608ed69d0b9cfa0c77cb \ -p 5d5eb80dc03bea8ff2bdd38962a1259b7725d169 \ -m "Renamed stash" f2adfc7bbebe852693ad8f6ac889e4923230c872 Store this commit as a new stash $ git stash store \ -m "$(git show -s --format=%B f2adfc7bbebe852693ad8f6ac889e4923230c872)" \ f2adfc7bbebe852693ad8f6ac889e4923230c872
新的stash消息需要同时提供给git-commit-tree和git-stash-store,因为git将其存储在两个地方(commit和logs/refs/stash)。
注意,使用——keep-index创建的存储将有3个父节点,因此在这种情况下,您需要为git-commit-tree提供第三个父节点!
将此过程转换为一行程序以在git别名中使用将留给读者作为练习;-)。确保只使用管道命令(因此避免使用git-show和git-log)。