我有一堆名字不正确的东西。我想修改一下名字,让它更准确。
我如何重命名收藏?
我有一堆名字不正确的东西。我想修改一下名字,让它更准确。
我如何重命名收藏?
当前回答
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)。
其他回答
为了读者的利益,这里是对目前公认的正确答案的扩展。
如果您不仅想更正存储消息,还想更正存储的提交消息,例如
git stash list
and
git log --oneline -1 stash
双方都同意所展示的内容,你需要更多一点。也许有更好的方法,但我希望这个食谱很容易理解。
为了能够做git提交-修改,你需要在一个分支的尖端。因此,解决方案是:
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
解释道:
创建一个新的(还不存在)“刮”分支从“藏匿在问题”,并切换到它 把旧的藏起来。这是安全的,因为树枝上还有这个。 使用git commit -amend来替换提交消息,这将改变“问题中的stash”的SHA 根据qzb的回答,把钱存起来 切换回去(假设你来自“master”)和清理
缺点:
这会临时切换分支。所以这个配方只能应用于git状态-瓷器是干净的(阅读:不输出任何东西) 它重新编号存储,因此更改后的存储变成了stash@{0} 您需要输入$MESSAGE两次或使用一些环境变量(在本例中为MESSAGE) 您需要找到一个未使用的分支名称
有一些方法可以在不切换分支的情况下做到这一点,但这超出了本文的范围。
例子
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
输出
*-. 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
现在不改变commit(注意:下面的SHA在你这边会不一样):
git stash drop stash@{1}
git stash store -m ...changed... 2fbf9007dfdfb95ae269a19e13b8b9ca3e24181c
git log --oneline --graph --decorate --all; git stash list
输出
*-. 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
正如你所看到的,在git日志中,stash@{0}仍然显示为master: 8bdcc32 D上的2fbf900 (refs/stash) WIP。如果仔细观察,就会发现有几个提交更改了SHA。这与处理存储的方式有关(SHA包括父存储,并且存储将它们的存储作为父存储)。
修复:
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
输出
*-. 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
你也可以看到,refs/stash也改变了SHA。
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)。
最简单的方法:弹出你的stash与git stash流行,然后保存它再次与git stash保存你的名字
下面是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 pop
在这之后,你可以保存一个自定义的名称,以这种方式:
git stash save "your explanatory name"
希望对大家有用。:)