我经常开始在某个分支上工作(例如 new-feature
),然后被优先级更高的东西打断(例如 high-prio
)。大多数时候,我对 new-feature
的更改是不完整的,因此我不想做一个完整的提交¹,所以我在 checkout 新功能之前 git stash
它们高优先级
分支。这将创建一个与 new-feature
关联的新存储:
stash@{0}: WIP on new-feature: <sha1 and message of previous commit>
现在,我可以完成关于high-prio
的工作。完成后,我再次检查 new-feature
并且(因为已经有一段时间了)经常忘记有一个与该分支相关联的存储。这会产生问题,因为我可能会重新做我已经在 stash 上完成的工作,或者我必须稍后 merge/重新设置这些更改。
是否有配置设置提醒我现有的存储,或者更好的是,在再次检查 new-feature
后自动将存储 pop 回我的工作树?
¹ 执行“WIP”-提交然后压缩更改不是一个好的解决方案,因为我想始终保持 origin/new-feature
与本地 new-feature 同步
。
最佳答案
您可以使用 git post-checkout像这样 Hook :
将以下文本添加到 .git/hooks/post-checkout
#!/bin/bash
echo "Don't forget to git stash pop the following:"
git stash list
或
#!/bin/bash
branch=$(git branch | sed -n '/\* /s///p')
last_stash_on_branch=$(git stash list --grep="WIP on ${branch}:" | head -1 | cut -d: -f1)
if [[ -n "${last_stash_on_branch}" ]]; then
git stash pop --index ${last_stash_on_branch}
fi
让它可执行
chmod +x .git/hooks/post-checkout
现在,当您 checkout 任何分支时,您的 checkout 后脚本有望提醒您检查您的存储
关于git - 当切换回相应的分支时,Git 可以提醒我或自动应用存储吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53049088/