gpt4 book ai didi

Git:如何使用 stash -p 来存储特定文件?

转载 作者:IT王子 更新时间:2023-10-29 00:40:05 25 4
gpt4 key购买 nike

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

这个非常有希望的答案,Stash only one file out of multiple files that have changed with Git? , 没有显示用法,我在计算时遇到了麻烦。

以下内容不起作用,手册页也不是很有帮助(它似乎在谈论终端输出,而不是实际存储)。我想 stash 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 stash --patch

git 然后会为你可能的提交中的每个 hunk 显示如下所示的对话框:

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拒绝添加 hunk,然后您可以从该文件添加a所有 hunk。

您也可以通过用 yes 回答问题来选择一个 hunk。如果大块头看起来太大,您甚至可以s拆分它。也可以e编辑当前的 hunk。


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

这是我从 developers documentation 中抓取的 --patch 函数的详细解释:

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 .我使用后者将我的日常工作拆分为原子提交。

关于Git:如何使用 stash -p 来存储特定文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com