如何保存/应用带有名称的存储?我不想在git存储列表中查找它的索引号。我尝试了git存储保存“my_stash_name”,但这只会更改存储描述,相应的git-apply“my_stash_name”不起作用。
当前回答
如果您只是在寻找一种轻量级的方法来保存当前工作副本的部分或全部更改,然后稍后再重新应用它们,请考虑使用修补程序文件:
# save your working copy changes
git diff > some.patch
# re-apply it later
git apply some.patch
时不时地,我会想我是否应该为此使用储藏室,然后我会看到上面的疯狂,我对自己的所作所为感到满意:)
其他回答
这是使用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''
}
}
此处显示更多详细信息
我不认为有什么方法可以通过名字来获取一个隐藏的东西。
我已经创建了一个bash函数来实现它。
#!/bin/bash
function gstashpop {
IFS="
"
[ -z "$1" ] && { echo "provide a stash name"; return; }
index=$(git stash list | grep -e ': '"$1"'$' | cut -f1 -d:)
[ "" == "$index" ] && { echo "stash name $1 not found"; return; }
git stash apply "$index"
}
用法示例:
[~/code/site] on master*
$ git stash push -m"here the stash name"
Saved working directory and index state On master: here the stash name
[~/code/site] on master
$ git stash list
stash@{0}: On master: here the stash name
[~/code/site] on master
$ gstashpop "here the stash name"
我希望这有帮助!
所以,我不知道为什么在这个话题上会有如此多的恐慌。我可以用push和不推荐的save来命名git stash,也可以使用正则表达式通过apply将其拉回:
使用名称应用的Git存储方法
$ git stash push -m "john-hancock"
$ git stash apply stash^{/john-hancock}
如前所述,save命令已被弃用,但它仍然有效,因此您可以在无法通过推送调用更新它们的旧系统上使用它。与push命令不同,save不需要-m开关。
// save is deprecated but still functional
$ git stash save john-hancock
这是Git 2.2和Windows 10。
视觉证明
这里有一个漂亮的动画GIF演示了这个过程。
事件顺序
GIF运行速度很快,但如果你看,过程如下:
ls命令显示目录中的4个文件touch example.html添加第五个文件git stash push-m“john hancock”-a(-a包括未跟踪的文件)ls命令在stash之后显示4个文件,这意味着stash和隐式硬重置工作正常git stash apply stash ^{/john hancock}运行ls命令列出了5个文件,显示example.html文件已返回,这意味着git stash apply命令有效。
这有道理吗?
坦率地说,我不确定这种方法的好处是什么。给储藏物起个名字是有价值的,但不包括检索。也许编写搁置和取消搁置过程的脚本会有所帮助,但只需按名称弹出一个存储库还是要容易得多。
$ git stash pop 3
$ git stash apply 3
这看起来比正则表达式容易得多。
这里的聚会迟到了,但如果使用VSCode,一种快速的方法是打开命令调色板(CTRL/CMD+SHIFT+P)并键入“Pop-Stash”,您将能够按名称检索存储,而无需使用git CLI
使用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
我喜欢能够看到储藏物的名称并进行选择。此外,我使用Zshell,坦率地说,我不知道如何使用上面的一些Bash别名;)
注意:正如凯文所说,你应该使用标签和樱桃采摘代替。
推荐文章
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?