如何保存/应用带有名称的存储?我不想在git存储列表中查找它的索引号。我尝试了git存储保存“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
我们可以选择任何一个储藏物,我们必须传递储藏物@{x},下面我选择第二个储藏物是1。
PS D:\git-example> git stash pop 1
其他回答
不幸的是,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 push-m aNameForYourStash来保存它。然后使用git stash list来了解要应用的存储的索引。然后使用git stash pop--index 0弹出存储并应用它。
注意:我使用的是git版本2.21.0.windows.1
这是使用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 save stashname
git stash apply stash^{/stashname}
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
我们可以选择任何一个储藏物,我们必须传递储藏物@{x},下面我选择第二个储藏物是1。
PS D:\git-example> git stash pop 1
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?
- 我如何从现有回购的分支创建一个新的GitHub回购?