如何 git stash 特定文件?

发布于 2024-10-29 00:10:40 字数 699 浏览 1 评论 0 原文

如何存储特定文件,而将当前修改的其他文件从我要保存的存储中保留出来?

例如,如果 git status 给了我这个:

younker % gst      
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# 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:   app/controllers/cart_controller.php
#   modified:   app/views/cart/welcome.thtml
#
no changes added to commit (use "git add" and/or "git commit -a")

并且我只想存储 app/views/cart/welcome.thtml,我该怎么做?像这样的东西(但这当然行不通):

git stash save welcome_cart app/views/cart/welcome.thtml

How can I stash a specific file leaving the others currently modified out of the stash I am about to save?

For example, if git status gives me this:

younker % gst      
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# 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:   app/controllers/cart_controller.php
#   modified:   app/views/cart/welcome.thtml
#
no changes added to commit (use "git add" and/or "git commit -a")

and I only want to stash app/views/cart/welcome.thtml, how would I do that? Something like (but of course this does not work):

git stash save welcome_cart app/views/cart/welcome.thtml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

蝶…霜飞 2024-11-05 00:10:40

编辑:从 git 2.13 开始,有一个命令可以将特定路径保存到存储:git stash push 。例如:

git stash push -m welcome_cart app/views/cart/welcome.thtml

旧答案:

您可以使用git stash --patch(或git stash -p)来做到这一点 - 您将输入交互模式中,您将看到每个已更改的块。使用 n 跳过您不想存储的文件,使用 y 当您遇到要存储的文件时使用 q退出并留下剩下的帅哥。 a 会将显示的块和其余块存储在该文件中。

这不是最用户友好的方法,但如果您确实需要它,它可以完成工作。

EDIT: Since git 2.13, there is a command to save a specific path to the stash: git stash push <path>. For example:

git stash push -m welcome_cart app/views/cart/welcome.thtml

OLD ANSWER:

You can do that using git stash --patch (or git stash -p) -- you'll enter interactive mode where you'll be presented with each hunk that was changed. Use n to skip the files that you don't want to stash, y when you encounter the one that you want to stash, and q to quit and leave the remaining hunks unstashed. a will stash the shown hunk and the rest of the hunks in that file.

Not the most user-friendly approach, but it gets the work done if you really need it.

酒中人 2024-11-05 00:10:40

我通常添加到我不想隐藏的索引更改,然后使用 --keep-index 选项进行隐藏。

git add app/controllers/cart_controller.php
git stash --keep-index
git reset

最后一步是可选的,但通常是您需要的。它从索引中删除更改。


警告
正如评论中所述, git stash --keep-index 将所有内容推送到存储中,包括暂存的和未暂存的。 --keep-index 只是在存储完成后保留索引。当您稍后弹出存储时,这可能会导致合并冲突。

I usually add to index changes I don't want to stash and then stash with --keep-index option.

git add app/controllers/cart_controller.php
git stash --keep-index
git reset

The last step is optional, but usually, you want it. It removes changes from the index.


Warning
As noted in the comments, git stash --keep-index pushes everything onto the stash, both staged and unstaged. The --keep-index just leaves the index alone after the stash is done. This can cause merge conflicts when you later pop the stash.

等风也等你 2024-11-05 00:10:40

存储一个文件:

git stash -- filename.txt

要在命令中给出一条消息而不是在提示时输入消息,请在文件部分之前添加 -m,例如 git stash -m "stash-message" -- filename1.txt

对于 存储多个文件:

git stash -m "stash-message" -- filename1.txt filename2.txt…

For stashing one file:

git stash -- filename.txt

To give a message in the command rather than enter it when prompted, add -m before the file part, e.g. git stash -m "stash-message" -- filename1.txt

For stashing more than one file:

git stash -m "stash-message" -- filename1.txt filename2.txt…
留蓝 2024-11-05 00:10:40

要添加到 svick 的答案中,-m 选项只是将一条消息添加到您的存储中,并且完全是可选的。因此,该命令

git stash push [paths you wish to stash]

完全有效。例如,如果我只想将更改存储在 src/ 目录中,我可以运行

git stash push src/

To add to svick's answer, the -m option simply adds a message to your stash, and is entirely optional. Thus, the command

git stash push [paths you wish to stash]

is perfectly valid. So for instance, if I want to only stash changes in the src/ directory, I can just run

git stash push src/
心在旅行 2024-11-05 00:10:40

如果您使用 Visual Studio 代码,则有一种更简单的方法来存储选定的文件。

  1. 确保您已在 VSCode 中安装 GitLens 扩展程序
  2. 转到“源代码管理”选项卡
  3. 选择要存储的文件
  4. 右键单击它,你会看到很多选择。单击存储更改

在此处输入图像描述

  1. 现在它会要求您添加一些隐藏消息。添加易于理解的消息并按 Enter 键。

输入图像描述这里

If you are using visual studio code there is a simpler way to stash selected files.

  1. Make sure you have installed GitLens extension in VSCode
  2. Go to Source Control tab
  3. Select files those you want to stash
  4. Right click on it, you will see many options. Click on Stash Changes

enter image description here

  1. Now it will ask you to add some stash message. Add understandable message and hit enter.

enter image description here

鱼忆七猫命九 2024-11-05 00:10:40

简短而简单的解决方案:

git stash -- filename.ext

在您的情况下 git stash -- app/views/cart/welcome.thtml

Short and Simple solution:

git stash -- filename.ext

in your case git stash -- app/views/cart/welcome.thtml

送你一个梦 2024-11-05 00:10:40
  • git 状态
    (确保您的更改可以隐藏)

src/config/bl.go

src/config/dl.go

如果您只想存储 dl.go

  • git stash push -m "< 输入您的消息,例如:恢复的 dl 文件> " src/config/dl.go

显示您的stash list

  • git stash list

然后签出您的分支并应用 stash

  • git stash apply stash{0}
  • git status
    (make sure your changes that can stash)

src/config/bl.go

src/config/dl.go

If you want to stash only dl.go

  • git stash push -m "< enter your msg eg:dl files reverted> " src/config/dl.go

Show your stash list

  • git stash list

Then checkout your branch and apply stash

  • git stash apply stash{0}
煮酒 2024-11-05 00:10:40

从 git 2.35.0 开始,您可以使用 --staged | 存储暂存的更改。 -S 标志。

例如:

git stash --staged

--已上演

此选项仅对推送和保存命令有效。

仅存储当前正在进行的更改。这与基本的 git commit 类似,只是状态被提交到存储而不是当前分支。

--patch 选项优先于该选项。

https://git-scm .com/docs/git-stash/2.35.0#Documentation/git-stash.txt---staged

这个新功能的好处在于,它不仅可以存储特定的未跟踪文件,而且还可以可以将代码更改的特定部分隐藏在跟踪文件中;当您使用 --patch | 将它们添加到舞台时-p 标志。

Since git 2.35.0 you can stash staged changes using --staged | -S flag.

For instance:

git stash --staged

--staged

This option is only valid for push and save commands.

Stash only the changes that are currently staged. This is similar to basic git commit except the state is committed to the stash instead of current branch.

The --patch option has priority over this one.

https://git-scm.com/docs/git-stash/2.35.0#Documentation/git-stash.txt---staged

The nice part about this new feature is that not only it's possible to stash specific untracked files, but also it's possible to stash specific part of the code changes in tracked files; when you add those to stage using --patch | -p flag.

千柳 2024-11-05 00:10:40

如果您可以使用 GIT GUI 客户端,那么自 2020 年 5 月起 Fork 就可以无缝地完成此操作。GIF部分隐藏功能将比任何文字更好地显示这一点:git-fork 中部分存储功能的 GIF

请注意,Fork(这对 Google 来说是一个很难理解的名字!)不是免费软件,评估期后费用为 50 美元,但您可以像对待 WinRAR 或 WinZip 一样忽略弹出窗口。

If you're OK with using a GIT GUI client, Fork can pretty seamlessly do this as of May 2020. A GIF of the partial stash functionality will show this better than any words: GIF of partial stash functionality in git-fork

Note that Fork (which is a difficult name to Google for!) is not free software and costs $50 after the evaluation period, but you can just ignore the popups like you do for WinRAR or WinZip.

枕梦 2024-11-05 00:10:40

@svick 发布了一个很好的答案。我想隐藏所有 .java 文件并保持 build.gradle 不变,所以我运行:

git stash push *.java

@svick has posted a great answer. I wanted to stash all my .java files and leave build.gradle untouched so I ran:

git stash push *.java
踏月而来 2024-11-05 00:10:40

我首选的方法(我认为最简单的方法)很简单:

git stash --

git stash -- path/to/directory/file.py< /代码>

My preferred method (the easiest in my opinion) is simply:

git stash -- <path/to/directory>

or

git stash -- path/to/directory/file.py

茶花眉 2024-11-05 00:10:40
  1. 暂存您不想隐藏的更改。
  2. 存储剩余的未暂存文件:
$ git stash save <give_it_a_name> --keep-index

现在已存储未暂存的文件。
查看包含您命名的存储的存储列表:

$ git stash list

stash@{0}: On mybranch: WIP220412-1119am
stash@{1}: On mybranch: WIP220312-749am

要恢复存储的文件:

$ git stash apply stash@{<index_of_saved_stash>}
$ git stash apply stash@{0}

存储在 WIP220412-1119am 中的更改现已恢复。
并且存储列表也保留下来,(而不是“git stash pop”,您可以通过这种方式保留列表。)

  1. stage the changes you do NOT want to stash.
  2. stash the remaining unstaged files with:
$ git stash save <give_it_a_name> --keep-index

The unstaged files are now stashed.
See the stash list with your named stash:

$ git stash list

stash@{0}: On mybranch: WIP220412-1119am
stash@{1}: On mybranch: WIP220312-749am

To restore the stashed files:

$ git stash apply stash@{<index_of_saved_stash>}
$ git stash apply stash@{0}

The changes stashed in WIP220412-1119am are now restored.
And the stash list remains as well, (instead of "git stash pop", you can retain the list this way.)

失与倦" 2024-11-05 00:10:40

我认为使用 git stash push 会很好!它还支持模式。

  • 例如。 git stash pushwelcome.*ml 将存储任何以 welcome. 开头并以 ml 结尾的文件。这适合你。

I think using git stash push <path> will be nice! it also supports patterns.

  • eg. git stash push welcome.*ml will stash any start with welcome. and end with ml files. This is suitable for you.
万劫不复 2024-11-05 00:10:40

在 VS-Code 的“源代码管理”选项卡中,按住 Shift 键,然后选择要存储的文件,然后右键单击并选择“存储更改”选项。

In Source Control tab of vs-code, hold shift key and then select the files you want to stash, then right click and choose stash changes option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文