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


当前回答

解决方案

本地更改:

file_A(已修改)未暂存file_B(已修改)未暂存file_C(已修改)未暂存

要创建仅包含file_C上的更改的存储“my_stash”:

1. git add file_C
2. git stash save --keep-index temp_stash
3. git stash save my_stash
4. git stash pop stash@#{1}

完成。


解释

将file_C添加到临时区域创建一个名为“temp_stash”的临时存储,并将更改保存在file_C上只使用file_C上的更改创建所需的存储(“my_stash”)将“temp_stash”(file_A和file_B)中的更改应用于本地代码并删除存储

您可以在步骤之间使用gitstatus来查看发生了什么。

其他回答

使用SourceTree,这可以通过3个步骤轻松完成。

暂时提交所有你不想隐藏的东西。Git添加所有其他内容,然后将其隐藏。通过运行git reset来弹出临时提交,在临时提交之前进行提交。

这一切都可以在SourceTree中在几秒钟内完成,您只需单击要添加的文件(甚至单个行)。添加后,只需将它们提交到临时提交。接下来,单击复选框以添加所有更改,然后单击“隐藏”以隐藏所有更改。在隐藏的更改不碍事的情况下,浏览一下提交列表,并在临时提交之前记录提交的哈希值,然后运行“git reset hash_b4_temp_commit”,这基本上就像通过将分支重置为提交之前的提交来“弹出”提交。现在,只剩下不想隐藏的内容。

我也想做同样的事情,但当我再次思考时,我意识到我不想为了保存一个文件而麻烦重重。对我来说,把它复制到其他地方并添加一些注释来提醒我为什么要把它保存在那里会容易得多。

不要误解我的意思:有很多原因可以解释为什么你只想隐藏一个文件,而且总的来说,使用版本控制软件总是最好的做法。但还是要确保你没有浪费时间。在我的情况下,我只想保留一个文件并放弃所有其他更改,然后在切换到新分支后将其弹出。所以cp/mv足够好。

如果您只想隐藏一些已更改的文件,只需在Stage中添加其他文件,然后执行git stash push--keep index

它将隐藏所有未暂存的更改文件

类似的情况。做了承诺,意识到这不好。

git commit -a -m "message"
git log -p

根据这些回答,这对我有帮助。

# revert to previous state, keeping the files changed
git reset HEAD~
#make sure it's ok
git diff
git status
#revert the file we don't want to be within the commit
git checkout specs/nagios/nagios.spec
#make sure it's ok
git status
git diff
#now go ahead with commit
git commit -a -m "same|new message"
#eventually push tu remote
git push
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