Git:如何使用 stash -p 命令来暂存特定文件?

24

我正试图想出如何在众多未提交的更改中隐藏两个特定的文件。

这个非常有前途的答案(Git中如何仅隐藏多个已更改文件中的一个文件?)没有展示用法,我正在努力弄清楚它。

以下代码无法正常工作,手册页并没有提供太多帮助(它似乎只讨论终端输出,而不是实际的隐藏)。我想要隐藏application.confplugins.sbt,然后提交其他所有内容。

app (master)$ git status
On branch master
Your branch is ahead of 'origin/master' by 29 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   views/mobile/blog.scala.html
        modified:   views/mobile/slideshow.scala.html
        modified:   ../public/css/mobile/styles.css

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   ../conf/application.conf

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:   ../project/plugins.sbt

app (master)$ git stash -p ../conf/application.conf ../project/plugins.sbt 

usage: git stash list [<options>]
   or: git stash show [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
                       [-u|--include-untracked] [-a|--all] [<message>]]
   or: git stash clear
3个回答

39

使用

git stash --patch

接下来,git会针对您潜在提交中的每个块显示如下对话框:

diff --git files over files
index e69de29..ac4f3b3 100644
--- a/file.txt
+++ b/file.txt
@@ -0,0 +1 @@
+you did awesome stuff!
Stash this hunk [y,n,q,a,d,/,e,?]?

"hunk"是指一组连续的行的差异,就像git-diff所生成的那样。如果要选择单个文件,则必须在到达该文件之前拒绝添加差异,然后可以添加该文件中的所有差异。

您还可以通过回答“yes”来选择单个差异。如果差异似乎太大,甚至可以将其拆分。当前差异还可以进行编辑。


使用--patch选项在不同的git命令(例如stashcommitadd)上都是可行的。

这是从开发人员文档中摘抄的--patch功能的详细说明:

This lets you choose one path out of a 'status' like selection.
After choosing the path, it presents the diff between the index
and the working tree file and asks you if you want to stage
the change of each hunk.  You can select one of the following
options and type return:

   y - stage this hunk
   n - do not stage this hunk
   q - quit; do not stage this hunk or any of the remaining ones
   a - stage this hunk and all later hunks in the file
   d - do not stage this hunk or any of the later hunks in the file
   g - select a hunk to go to
   / - search for a hunk matching the given regex
   j - leave this hunk undecided, see next undecided hunk
   J - leave this hunk undecided, see next hunk
   k - leave this hunk undecided, see previous undecided hunk
   K - leave this hunk undecided, see previous hunk
   s - split the current hunk into smaller hunks
   e - manually edit the current hunk
   ? - print help

如果你对更舒适的处理方式感兴趣,你也可以看看像LazyGitgitui这样的 git 图形界面工具。我使用后者将我的日常工作拆分为原子提交。


了解这样的东西使得你的日常工作变得更加容易。 - Savvy

14
在最新版本的Git(> v2.13)中,您可以使用命令将特定文件暂存:
git stash push -- <pathspec>

例如,如果您暂存特定的文件,您可以使用此命令专门将只有这些文件进行暂存:
git stash push -- example/file/path/and/file.html

你可以使用通配符模式将路径中特定的文件存储起来,这样可以节省一些打字工作。

git stash push -- **/path/**

你只需暂存需要隐藏的文件,因为其他已经暂存的文件会随着特定文件一起被隐藏,但在隐藏后仍保留暂存状态,所以如果你执行git stash pop操作,可能会出现冲突。


为什么这不是最佳答案? - Samir K
现在似乎不需要使用 --。为了明确起见,可以使用此存储多个文件,例如 git stash push file1.c file2.c - Pierz
这对我来说没有问题,不需要使用push。 例如:git stash -- **/path/** - Chevindu Wickramathilaka

7

使用git stash -p命令,不加参数。然后您可以交互式地选择要存储的更改:

diff --git a/test b/test
index acbd8ae..662d47a 100644
--- a/test
+++ b/test
@@ -10,6 +10,7 @@ test
(...)
+    test
(...)
Stash this hunk [y,n,q,a,d,/,e,?]?

很遗憾,只能选择代码块,无法选择文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接