【问题标题】:Git: how to use stash -p to stash specific files?Git:如何使用 stash -p 来存储特定文件?
【发布时间】:2023-03-11 04:02:01
【问题描述】:

我试图弄清楚如何在许多未提交的更改中存储两个特定文件。

这个非常有希望的答案Stash only one file out of multiple files that have changed with 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

【问题讨论】:

    标签: git git-stash


    【解决方案1】:

    使用

    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 产生的连贯的行差异。要选择单个文件,您必须在到达该文件时向decline 添加帅哥,然后您可以从该文件中添加all 帅哥。

    您还可以通过yes 回答问题来选择一个大块头。如果大块看起来太大,你甚至可以split 它。也可以edit 当前大块。


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

    这是--patch-function的详细解释,我从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 工具,例如 LazyGit。我用这个把我的日常工作分成原子提交。

    【讨论】:

      【解决方案2】:

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

      暂存您想要存储的特定文件
      git stash push -- <pathspec>
      

      例如,如果您暂存您想要存储的特定文件,您可以使用此命令专门存储这些文件:

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

      您可以通过使用通配模式将特定文件存储在路径中来节省一些输入

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

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

      【讨论】:

      • 为什么这不是最佳答案?
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 2023-02-08
        • 2011-08-09
        • 2013-12-29
        • 2021-07-23
        • 2017-10-18
        • 1970-01-01
        相关资源
        最近更新 更多