如何保存/应用带有名称的存储?我不想在git存储列表中查找它的索引号。我尝试了git存储保存“my_stash_name”,但这只会更改存储描述,相应的git-apply“my_stash_name”不起作用。
当前回答
不幸的是,git stash apply stash ^{/<regex>}不起作用(它实际上没有搜索存储列表,请参阅接受答案下的注释)。
下面是通过正则表达式搜索git stash列表以查找第一个(最近的)stash@{<n>},然后将其传递给git stash<command>的替换项:
# standalone (replace <stash_name> with your regex)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
# ~/.gitconfig
[alias]
sshow = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"
sapply = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"
# usage:
$ git sshow my_stash
myfile.txt | 1 +
1 file changed, 1 insertion(+)
$ git sapply my_stash
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
请注意,将返回正确的结果代码,以便您可以在其他脚本中使用这些命令。使用以下命令运行命令后,可以验证这一点:
echo $?
请小心变量扩展漏洞,因为我不确定--grep=$1部分。它可能是--grep=“$1”,但我不确定这是否会干扰正则表达式分隔符(我愿意接受建议)。
其他回答
自2.15.x/2.16起,git stash save已被弃用,您可以使用git stass push-m“message”
您可以这样使用:
git stash push-m“消息”
其中“message”是你的留言。
为了检索存储,您可以使用:git存储列表。这将输出如下列表,例如:
stash@{0}: On develop: perf-spike
stash@{1}: On develop: node v10
然后,只需使用apply为其提供存储@{index}:
git stash apply 1
工具书类git存储手册页
这里是我的社区别名:wip和wip适用。当您获取wip时,您还存储了未跟踪的文件,并返回到以前的提交状态。
git config --global alias.wip '!f() { git stash save $1 -u ; }; f'
git config --global alias.wip-apply '!f() { temp=$(git stash list | cut -d ':' -f 3 | grep -n -w $1 | cut -d ':' -f 1) ; stashnum=$((temp-1)) ; stashname=$(echo stash@{$stashnum}) ; git stash apply $stashname ; }; f'
用法:
git wip "featureA"
git wip-apply "featureA"
不幸的是,git stash apply stash ^{/<regex>}不起作用(它实际上没有搜索存储列表,请参阅接受答案下的注释)。
下面是通过正则表达式搜索git stash列表以查找第一个(最近的)stash@{<n>},然后将其传递给git stash<command>的替换项:
# standalone (replace <stash_name> with your regex)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
# ~/.gitconfig
[alias]
sshow = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"
sapply = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"
# usage:
$ git sshow my_stash
myfile.txt | 1 +
1 file changed, 1 insertion(+)
$ git sapply my_stash
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
请注意,将返回正确的结果代码,以便您可以在其他脚本中使用这些命令。使用以下命令运行命令后,可以验证这一点:
echo $?
请小心变量扩展漏洞,因为我不确定--grep=$1部分。它可能是--grep=“$1”,但我不确定这是否会干扰正则表达式分隔符(我愿意接受建议)。
这个呢?
git stash save stashname
git stash apply stash^{/stashname}
gitstash-apply还可以与stash@{0}以外的其他ref一起使用。因此,您可以使用普通标记来获取持久名称。这还有一个好处,那就是你不会不小心把git藏起来或者把git隐藏起来。
因此,您可以这样定义别名pstash(又名“持久存储”):
git config --global alias.pstash '!f(){ git stash && git tag "$1" stash && git stash drop; }; f'
现在,您可以创建标记的存储:
git pstash x-important-stuff
并照常显示和应用:
git stash show x-important-stuff
git stash apply x-important-stuff
推荐文章
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌