如何在我的分支中只存储多个已更改文件中的一个?


当前回答

我没有找到我需要的答案,这很简单:

git add -A
git reset HEAD fileThatYouWantToStash
git commit -m "committing all but one file"
git stash

这只存储了一个文件。

其他回答

git add .                           //stage all the files
git reset <pathToFileWillBeStashed> //unstage file which will be stashed
git stash                           //stash the file(s)
git reset .                         // unstage all staged files
git stash pop                       // unstash file(s)

将以下代码保存到一个文件中,例如,名为stash。用法是stash<filename_regex>。参数是文件完整路径的正则表达式。例如,要隐藏a/b/c.txt、隐藏a/b/c.txt或隐藏.*/c.txt等。

$ chmod +x stash
$ stash .*.xml
$ stash xyz.xml

要复制到文件中的代码:

#! /usr/bin/expect --
log_user 0
set filename_regexp [lindex $argv 0]

spawn git stash -p

for {} 1 {} {
  expect {
    -re "diff --git a/($filename_regexp) " {
      set filename $expect_out(1,string)
    }
    "diff --git a/" {
      set filename ""
    }
    "Stash this hunk " {
      if {$filename == ""} {
        send "n\n"
      } else {
        send "a\n"
        send_user "$filename\n"
      }
    }
    "Stash deletion " {
      send "n\n"
    }
    eof {
      exit
    }
  }
}
git stash push -p -m "my commit message"

-p让我们选择应该隐藏的大块;也可以选择整个文件。

系统将提示您对每个大块执行一些操作:

   y - stash this hunk
   n - do not stash this hunk
   q - quit; do not stash this hunk or any of the remaining ones
   a - stash this hunk and all later hunks in the file
   d - do not stash 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 stash--patch[file]。

这将提示:Stash This hunk[y,n,q,a,d,j,j,g,/,e,?]?。只需键入一个(将此大块和所有后续大块存储在文件中),就可以了。

当git stash-p(或git add-p with stash--keep index)太麻烦时,我发现使用diff、checkout和apply更容易:

仅“隐藏”特定文件/dir:

git diff path/to/dir > stashed.diff
git checkout path/to/dir

然后

git apply stashed.diff