如何在我的分支中只存储多个已更改文件中的一个?
当前回答
另一种方法是:
# Save everything
git stash
# Re-apply everything, but keep the stash
git stash apply
git checkout <"files you don't want in your stash">
# Save only the things you wanted saved
git stash
# Re-apply the original state and drop it from your stash
git stash apply stash@{1}
git stash drop stash@{1}
git checkout <"files you put in your stash">
在我(再次)来到这个页面并不喜欢前两个答案(第一个答案只是不回答问题,我不太喜欢使用-p交互模式)之后,我想到了这个问题。
这一想法与@VonC建议的使用存储库外的文件相同,您可以将所需的更改保存在某个位置,删除存储库中不需要的更改,然后重新应用您移开的更改。然而,我使用了git隐藏作为“某处”(因此,最后还有一个额外的步骤:移除你放在隐藏中的cahnges,因为你也把它们移到了一边)。
其他回答
我也想做同样的事情,但当我再次思考时,我意识到我不想为了保存一个文件而麻烦重重。对我来说,把它复制到其他地方并添加一些注释来提醒我为什么要把它保存在那里会容易得多。
不要误解我的意思:有很多原因可以解释为什么你只想隐藏一个文件,而且总的来说,使用版本控制软件总是最好的做法。但还是要确保你没有浪费时间。在我的情况下,我只想保留一个文件并放弃所有其他更改,然后在切换到新分支后将其弹出。所以cp/mv足够好。
解决方案
本地更改:
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来查看发生了什么。
这里的每个答案都很复杂。。。
“藏起来”怎么样
git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash
这将弹出文件更改:
git apply /tmp/stash.patch
与隐藏一个文件并将其弹出完全相同的行为。
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-p(--patch):手动选择大块,不包括未跟踪的文件git stash-k(--keep index):隐藏所有跟踪/未跟踪的文件,并将它们保存在工作目录中git stash-u(--包括未跟踪的):隐藏所有已跟踪/未跟踪的文件git stash-p(--patch)-u(--include untracked):无效命令
目前,能够隐藏任何特定跟踪/未跟踪文件的最合理方法是:
暂时提交不想隐藏的文件添加并隐藏弹出临时提交
在回答另一个问题时,我为这个过程编写了一个简单的脚本,这里有SourceTree中执行该过程的步骤。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别