Git:如何使用stash -p来隐藏特定的文件?

s5a0g9ez  于 2022-11-20  发布在  Git
关注(0)|答案(3)|浏览(155)

我正在尝试找出如何在许多未提交的更改中隐藏两个特定的文件。
这个很有希望的答案,在Git?修改过的多个文件中只隐藏一个文件,并没有显示使用情况,我也很难解决这个问题。
下面的代码不起作用,手册页也没有什么帮助(它似乎是在讨论终端输出,而不是实际的stashing)。

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
dxpyg8gm

dxpyg8gm1#

用途

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,?]?

块是git-diff产生的一致的行差。要选择一个文件,你必须d取消添加块,只要你到达了那个文件,然后你可以从那个文件中添加a ll个块。
你也可以用y es来选择一个块。如果这个块看起来太大了,你甚至可以s分割它。也可以e编辑当前的块。
可以在不同的git命令中使用--patch-选项(例如stashcommitadd)。
下面是对--patch-函数的详细解释,我从developers documentation中获取了该函数:

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

如果你对更舒适的方式感兴趣,你也可以看看git gui工具,比如LazyGitgitui,我用后者把我的日常工作分解成原子提交。

ttvkxqim

ttvkxqim2#

在最新版本的Git(〉v2.13)中,您可以使用

git stash push -- <pathspec>

例如,如果您登台要隐藏得特定文件,则可以使用此命令专门仅隐藏这些文件:

git stash push -- example/file/path/and/file.html

您可以通过使用globbing模式将特定文件隐藏在路径中,从而保存一些键入操作

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

您将只需要暂存您要隐藏的文件,因为任何其他暂存文件都将与特定文件一起隐藏,但在隐藏后仍将保持暂存状态,因此如果您git stash pop,您将遇到冲突。

sauutmhj

sauutmhj3#

使用不带参数的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,?]?

不幸的是,它不可能选择文件,只能选择块。

相关问题