我有一系列的文件在一个stash (stash{0}
) 中,我想要仅仅使用这些文件中的某些部分/块(通常称为交互模式)使用git apply
命令。
这是可能的吗?
我看到可以执行:
git stash save -p 'Stash name'
但似乎无法实现
git stash apply -p 'Stash name'
你知道实现它的方法吗?
我有一系列的文件在一个stash (stash{0}
) 中,我想要仅仅使用这些文件中的某些部分/块(通常称为交互模式)使用git apply
命令。
这是可能的吗?
我看到可以执行:
git stash save -p 'Stash name'
但似乎无法实现
git stash apply -p 'Stash name'
你知道实现它的方法吗?
这是可能的吗?
git checkout -p stash@{0}
您可以将stash@{0}
中的0
替换为要应用的存储编号。
如果不确定要应用哪个存储编号,请使用git stash list
和git show -p stash@{n}
。
不要忘记在确定不再需要该存储时使用git stash drop stash@{n}
,因为git checkout
不会自动删除存储。
实际上,它们存储在.git/refs/stash
中,每个存储哈希值占据一行。
git checkout -p
尝试应用提交和当前工作区之间的整个差异。
在git stash的情况下,如果您尝试应用的stash是针对不同提交执行的,则git checkout -p stash@{n}
将尝试交互式地应用提交stash@{n}
和当前工作区提交之间的所有差异,包括它们不同的父提交。
例如,如果您正在尝试将保存在“许多提交之前”的stash应用于当前工作区,git checkout -p stash@{n}
将尝试不仅应用stash中的更改,还将尝试撤消基于stash的提交和当前提交之间发生的所有更改。
git checkout -p stash@{n}
将尝试同时应用当前提交和未来提交之间发生的所有其他更改,除了存储本身的更改。git checkout -p stash@{n}
获取存储将尝试还原当前提交和原始分支点之间的所有更改,并应用分支点和另一个分支之间的所有更改,除了存储中的更改)。
git checkout -p stash@{n}
时,请非常小心接受补丁。git checkout -p ...
之前,先执行git stash pop
,然后再执行git stash
。但是,如果您想要部分应用您的存储以避免冲突,则这并没有真正帮助。在这种情况下,请参见下面的解决方案4。git difftool
并仅“应用左侧”您感兴趣的更改:git difftool -d stash@{n}
比较整个存储区及其所有文件
git difftool stash@{n} -- path/to/file
比较单个文件
git checkout stash@{n}^ # notice the "^".
# Now you're in a detached head in the parent commit of the stash.
# It can be applied cleanly:
git stash apply stash@{n}
# Now save only the diffs you're interested in:
git stash -p
# remove the rest of the old stash
git checkout -- . # be careful or you could remove unrelated changes
# go back to the branch where you want to apply the smaller stash
git checkout <my previous branch>
# apply the smaller stash
git stash pop
我经常在 Git Bash 中做的事情是
git stash show -p 'stash@{0}' >tmp.patch
然后我编辑文件并删除我不想要的部分。 最后我说:
<tmp.patch git apply
或者
<tmp.patch patch -p1
然而,它不适用于二进制文件,但是对于它们也没有被接受的答案(使用checkout -p)。
一种可能的方法是重置索引,然后使用交互式添加
# 0. ensure there are no uncommitted changes
git status
# 1. apply a changeset as is
git stash apply stash@{n}
# ... fix or discard conflicts if any
# 2. reset the index
git reset
# 3. interactively add the required chunks (except new files)
git add -p
# 4. stash all other changes
git stash save --keep-index "comment"
# 4. or just discards all other changes in the working tree
git checkout-index -f -a
# 5. commit
git commit -m "comment"
# 0. ensure the working tree does not have unstaged changes
git status
# 1. apply a changeset as is
git stash apply stash@{n}
# ... fix or discard conflicts if any
# 2. interactively exclude the unneeded chunks from the index
git reset -p
# 3. stash all other changes
git stash save --keep-index "comment"
# 3. or just discards all other changes in the working tree
git checkout-index -f -a
# 4. commit
git commit -m "comment"
我认为没有一种方法可以按块(甚至按文件)应用更改。您需要应用存储,然后交互式地存储不想要的更改(使用git stash save -p
)。如果您担心冲突,您可以先存储任何未提交的更改,应用您的存储,存储任何有冲突的块,然后应用另一个存储。
git stash save
),然后在所需的分支上还原它,而不是交互式地应用我需要的代码块(使用 git stash apply
)。这似乎是最好的方法。 - Kamafeather