我该如何保存/应用一个有名字的git储藏(stash)? 我不想在 git stash list
中查找它的索引号。我尝试了 git stash save "my_stash_name"
,但这只会更改储藏的描述,对应的 git apply "my_stash_name"
是无效的。
使用以下命令可以自定义注释 Stash。
PS D:\git-example> git stash -m "your comment"
PS D:\git-exapmle> git stash list
stash@{0}: On master: first stash
stash@{1}: On master: second stash
我们可以选择任何一个存档,但是我们必须通过stash@{x}这个存档,下面我将选择第二个存档,即1号存档。
PS D:\git-example> git stash pop 1
git stash pop 1
和 git stash apply stash@{1}
有微小的不同,https://www.theserverside.com/video/Compare-git-stash-pop-and-git-stash-apply-for-file-restores#:~:text=The%20key%20difference%20between%20git,then%20deletes%20the%20applied%20stash。 - Safrygit stash apply stash^{/<regex>}
不起作用(它实际上并没有搜索stash列表,参见接受的答案下的评论)。git stash list
以查找最近的第一个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"
,但我不确定这是否会干扰正则表达式定界符(欢迎提出建议)。
sapply = "!f() { git stash apply \"$(git stash list | awk -F: --posix -vpat=\"$*\" \"$ 0 ~ pat {print $ 1; exit}\")\"; }; f"
git sapply "<正则表达式>"
编辑:我坚持使用原始解决方案,但我明白为什么大多数人更喜欢Etan Reisner的版本(上面)。所以只是为了记录:
sapply = "!f() { git stash apply \"$(git stash list | grep -E \"$*\" | awk \"{ print $ 1; }\" | sed -n \"s/://;1p\")\"; }; f"
awk -F: '{print $1}'
可以消除对sed的需求。另外,为什么要将其包装在一个函数中呢?而且使用 awk -F: -vpat="$*" '$0 ~ pat {print $1}'
还可以省去grep。虽然模式可能需要稍微不同的引用方式。 - Etan Reisner{print $1; exit}
修改为在匹配到第一行后退出。 - Etan Reisner这篇答案受到Klemen Slavič的很大启发。我原本只是想在被接受的答案中发表评论,但我还没有足够的声望 :(
您还可以添加一个git别名来查找stash引用,并将其用于其他别名的显示、应用、删除等操作。
[alias]
sgrep = "!f() { ref=$(git --no-pager stash list | grep "$1" | cut -d: -f1 | head -n1); echo ${ref:-<no_match>}; }; f"
sshow = "!f() { git stash show $(git sgrep "$1") -p; }; f"
sapply = "!f() { git stash apply $(git sgrep "$1"); }; f"
sdrop = "!f() { git stash drop $(git sgrep "$1"); }; f"
ref=$( ... ); echo ${ref:-<no_match>};
模式的原因是为了避免返回空字符串,否则 sshow、sapply 和 sdrop 将会定位到最新的存储区而不是像预期的那样失败。别名 这可能是 Unix-like 系统中更直接的语法,不需要封装在函数中。 将以下内容添加到 ~/.gitconfig 文件的 [alias] 节点下。
sshow = !sh -c 'git stash show stash^{/$*} -p' -
sapply = !sh -c 'git stash apply stash^{/$*}' -
ssave = !sh -c 'git stash save "${1}"' -
用法: sapply 正则表达式
例子: git sshow MySecretStash
末尾的连字符表示从标准输入获取输入。
NAME="$1"
if [[ -z "$NAME" ]]; then echo "usage: gitapply [name]"; exit; fi
git stash apply $(git stash list | grep "$NAME" | cut -d: -f1)
使用方法:
gitapply foo
...其中foo是您想要的stash名称的子字符串。
git stash save NAME
来保存。#!/usr/bin/env ruby
#git-stash-pick by Dan Rosenstark
# can take a command, default is apply
command = ARGV[0]
command = "apply" if !command
ARGV.clear
stashes = []
stashNames = []
`git stash list`.split("\n").each_with_index { |line, index|
lineSplit = line.split(": ");
puts "#{index+1}. #{lineSplit[2]}"
stashes[index] = lineSplit[0]
stashNames[index] = lineSplit[2]
}
print "Choose Stash or ENTER to exit: "
input = gets.chomp
if input.to_i.to_s == input
realIndex = input.to_i - 1
puts "\n\nDoing #{command} to #{stashNames[realIndex]}\n\n"
puts `git stash #{command} #{stashes[realIndex]}`
end
git stash save
已被弃用,推荐使用git stash push
。 - wranvaud这里是我为社区设置的别名:wip
和 wip-apply
。
当你使用git 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"
使用PowerShell来完成这项任务有以下步骤:
<#
.SYNOPSIS
Restores (applies) a previously saved stash based on full or partial stash name.
.DESCRIPTION
Restores (applies) a previously saved stash based on full or partial stash name and then optionally drops the stash. Can be used regardless of whether "git stash save" was done or just "git stash". If no stash matches a message is given. If multiple stashes match a message is given along with matching stash info.
.PARAMETER message
A full or partial stash message name (see right side output of "git stash list"). Can also be "@stash{N}" where N is 0 based stash index.
.PARAMETER drop
If -drop is specified, the matching stash is dropped after being applied.
.EXAMPLE
Restore-Stash "Readme change"
Apply-Stash MyStashName
Apply-Stash MyStashName -drop
Apply-Stash "stash@{0}"
#>
function Restore-Stash {
[CmdletBinding()]
[Alias("Apply-Stash")]
PARAM (
[Parameter(Mandatory=$true)] $message,
[switch]$drop
)
$stashId = $null
if ($message -match "stash@{") {
$stashId = $message
}
if (!$stashId) {
$matches = git stash list | Where-Object { $_ -match $message }
if (!$matches) {
Write-Warning "No stashes found with message matching '$message' - check git stash list"
return
}
if ($matches.Count -gt 1) {
Write-Warning "Found $($matches.Count) matches for '$message'. Refine message or pass 'stash{@N}' to this function or git stash apply"
return $matches
}
$parts = $matches -split ':'
$stashId = $parts[0]
}
git stash apply ''$stashId''
if ($drop) {
git stash drop ''$stashId''
}
}
git stash apply
命令也可以用于除了 stash@{0}
之外的其他引用。因此,您可以使用普通的标签来获取持久名称。这样做的好处是,您不会意外地使用 git stash drop
或 git stash pop
命令。
您可以像这样定义别名 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
git stash push -m stashname
是当前语法。git stash save stashname
已被弃用。 - SherylHohmangit bottle name
,然后稍后使用git apply name.patch
,如果您想要更详细的解释和使用示例,请参见我的答案。 - GrayedFox