【发布时间】:2018-07-04 21:16:43
【问题描述】:
假设
我最近添加或未添加到索引中的更改。现在我正在挑选一个特定的提交,而不在我的 HEAD 上创建一个新的提交......
git cherry-pick -n <commit>
如何从索引中删除精选更改?我可以做一个
git reset HEAD
但我必须重做之前添加的所有更改。
目的
如果有人进行了 stash,则无法将 stash 推送到遥控器。当前的 WIP 无法从另一个系统上的远程提取以使用。所以我编写了 shell 函数来模拟 git-stash,除了我为每个 stash 使用分支。
apply 或 pop 通常会将隐藏的更改应用于 WIP,但不会应用于当前索引。当我使用樱桃挑选来应用存储分支中的更改时,所有这些更改都将添加到索引中,之后我需要将它们从索引中删除。
编辑 (2018-01-29)
我阅读了@torek 的回答并理解了它。尽管如此,我还是喜欢分享我以前使用过的 bash 函数。
function git-stash {
local gitbranch="$( git branch | grep \* )"
local currentbranch="$( [ "${gitbranch}" == "* (HEAD"* ] && echo "${gitbranch}" | cut -d ' ' -f5 | cut -d ')' -f1 || echo "${gitbranch}" | cut -d ' ' -f2- )"
local stashname="stash/$( date +%s )"
git stash save -u ${stashname}
git checkout -b ${stashname}
git stash pop
git add .
[ ${1} ] && git commit -m "WIP: "$1 || git commit -m "WIP"
git checkout ${currentbranch}
}
function git-stash-apply {
local stashbranches="$( git branch | grep stash/ | cut -d ' ' -f3- | sort -r )"
local stashbranches=(${stashbranches[@]})
local lateststashbranch="${stashbranches[0]}"
git cherry-pick -n "${lateststashbranch}"
}
function git-stash-pop {
local stashbranches="$( git branch | grep stash/ | cut -d ' ' -f3- | sort -r )"
local stashbranches=(${stashbranches[@]})
local lateststashbranch="${stashbranches[0]}"
git cherry-pick -n "${lateststashbranch}"
git branch -D "${lateststashbranch}"
git push origin :"${lateststashbranch}"
}
这还不是一个合适的解决方案,更不用说stash pop 中缺少的错误处理。
【问题讨论】:
标签: git git-stash git-reset git-cherry-pick git-rm