如何仅存储已更改的多个文件中的一个文件?

发布于 2024-09-05 19:59:12 字数 28 浏览 5 评论 0原文

如何在我的分支上仅存储多个更改的文件之一?

How do I stash only one of the multiple changed files on my branch?

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

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

发布评论

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

评论(30

痴者 2024-09-12 19:59:12
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 push -p -m "my commit message"

-p let's you select the hunks that should be stashed; whole files can be selected as well.

You'll be prompted with a few actions for each hunk:

   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
格子衫的從容 2024-09-12 19:59:12

免责声明:以下答案适用于 git 2.13 之前的 git。对于 git 2.13 及更高版本,请查看下面的另一个答案


警告

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


这将隐藏您之前未添加的所有内容。只需git add你想要保留的东西,然后运行它。

git stash --keep-index

例如,如果您想将旧提交拆分为多个变更集,可以使用以下过程:

  1. git rebase -i
  2. 将某些更改标记为edit.
  3. git reset HEAD^
  4. git add <您想要在此更改中保留的文件>
  5. git stash --keep-index
  6. 根据需要修复问题。不要忘记 git add 任何更改。
  7. git commit
  8. git stash pop
  9. 根据需要从 #5 开始重复。
  10. git rebase --继续

Disclaimer: the following answer is for git before git 2.13. For git 2.13 and over, check out another answer further down.


Warning

As noted in the comments, this puts everything into 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.


This will stash everything that you haven't previously added. Just git add the things you want to keep, then run it.

git stash --keep-index

For example, if you want to split an old commit into more than one changeset, you can use this procedure:

  1. git rebase -i <last good commit>
  2. Mark some changes as edit.
  3. git reset HEAD^
  4. git add <files you want to keep in this change>
  5. git stash --keep-index
  6. Fix things up as necessary. Don't forget to git add any changes.
  7. git commit
  8. git stash pop
  9. Repeat, from #5, as necessary.
  10. git rebase --continue
九命猫 2024-09-12 19:59:12

从 Git 2.13(2017 年第 2 季度)开始,您可以使用 git 存储推送

git stash push [-m <message>] [--] [<pathspec>...]

pathspec被赋予'git stash push'时,新的stash仅记录与pathspec匹配的文件的修改状态
有关详细信息,请参阅“存储对特定文件的更改”。

简化示例:

 git stash push path/to/file

测试用例此功能显示了更多选项:

test_expect_success 'stash with multiple pathspec arguments' '
    >foo &&
    >bar &&
    >extra &&
    git add foo bar extra &&

    git stash push -- foo bar &&   

    test_path_is_missing bar &&
    test_path_is_missing foo &&
    test_path_is_file extra &&

    git stash pop &&
    test_path_is_file foo &&
    test_path_is_file bar &&
    test_path_is_file extra

原始答案(如下,2010 年 6 月)是关于手动选择您想要隐藏的内容。

Casebash 评论:

这个(stash --patch原始解决方案)很好,但我经常修改很多文件,所以使用补丁很烦人

bukzor答案(已投票,2011 年 11 月)提出了一个更实用的解决方案,基于上
git add + git stash --keep-index
去看看并投票他的答案,这应该是官方的答案(而不是我的)。

关于该选项, chhh 在评论中指出了替代工作流程:

你应该在这样的存储之后“git reset --soft”来恢复清晰的暂存:
为了达到原始状态 - 这是一个清晰的暂存区域,并且只有一些选择的未暂存修改,可以轻柔地重置索引以获取(无需像您 - bukzor - 那样提交任何内容)。


(原始答案 2010 年 6 月:手动存储)

然而,git stash save --patch 可以让您实现您想要的部分存储:

使用--patch,您可以从 HEAD 和要隐藏的工作树之间的差异中交互地选择块。
存储条目的构造使其索引状态与存储库的索引状态相同,并且其工作树仅包含您交互选择的更改。然后,所选更改将从工作树中回滚。

但是,这将保存完整索引(这可能不是您想要的,因为它可能包括已索引的其他文件)和部分工作树(可能看起来像您想要隐藏的工作树)。

git stash --patch --no-keep-index

可能更合适。


如果 --patch 不起作用,手动过程可能是:

对于一个或多个文件,中间解决方案是:

  • 将它们复制到 Git 存储库之外
    (实际上,eleotlecram 提出了一个有趣的替代方案)
  • git stash
  • 将它们复制回来
  • git stash # 这次,只保存您想要的文件
  • git stash pop stash@{1} # 重新应用所有文件修改
  • git checkout -- afile # 在进行任何本地修改之前,将文件重置为 HEAD 内容

在这个相当繁琐的过程结束时,您将只得到隐藏一个或多个文件。


正如 mirekphd评论

如果有一些未跟踪的文件,git stash push --include-untracked 也会包含这些文件。

Since Git 2.13 (Q2 2017), you can stash individual files, with git stash push:

git stash push [-m <message>] [--] [<pathspec>...]

When pathspec is given to 'git stash push', the new stash records the modified states only for the files that match the pathspec
See "Stash changes to specific files" for more.

Simplified example:

 git stash push path/to/file

The test case for this feature shows a few more options off:

test_expect_success 'stash with multiple pathspec arguments' '
    >foo &&
    >bar &&
    >extra &&
    git add foo bar extra &&

    git stash push -- foo bar &&   

    test_path_is_missing bar &&
    test_path_is_missing foo &&
    test_path_is_file extra &&

    git stash pop &&
    test_path_is_file foo &&
    test_path_is_file bar &&
    test_path_is_file extra

The original answer (below, June 2010) was about manually selecting what you want to stash.

Casebash comments:

This (the stash --patch original solution) is nice, but often I've modified a lot of files so using patch is annoying

bukzor's answer (upvoted, November 2011) suggests a more practical solution, based on
git add + git stash --keep-index.
Go see and upvote his answer, which should be the official one (instead of mine).

About that option, chhh points out an alternative workflow in the comments:

you should "git reset --soft" after such a stash to get your clear staging back:
In order to get to the original state - which is a clear staging area and with only some select un-staged modifications, one could softly reset the index to get (without committing anything like you - bukzor - did).


(Original answer June 2010: manual stash)

Yet, git stash save --patch could allows you to achieve the partial stashing you are after:

With --patch, you can interactively select hunks from in the diff between HEAD and the working tree to be stashed.
The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively. The selected changes are then rolled back from your worktree.

However that will save the full index (which may not be what you want since it might include other files already indexed), and a partial worktree (which could look like the one you want to stash).

git stash --patch --no-keep-index

might be a better fit.


If --patch doesn't work, a manual process might:

For one or several files, an intermediate solution would be to:

  • copy them outside the Git repo
    (Actually, eleotlecram proposes an interesting alternative)
  • git stash
  • copy them back
  • git stash # this time, only the files you want are stashed
  • git stash pop stash@{1} # re-apply all your files modifications
  • git checkout -- afile # reset the file to the HEAD content, before any local modifications

At the end of that rather cumbersome process, you will have only one or several files stashed.


As noted by mirekphd in the comments:

If there are some untracked files, git stash push --include-untracked will also include those.

清风不识月 2024-09-12 19:59:12

使用git stash push,如下所示:

git stash push [--] [<pathspec>...]

例如:

git stash push -- my/file.sh

此功能自 2017 年春季发布的 Git 2.13 起可用。

Use git stash push, like this:

git stash push [--] [<pathspec>...]

For example:

git stash push -- my/file.sh

This is available since Git 2.13, released in spring 2017.

筑梦 2024-09-12 19:59:12

git stash -p (或 git add -pstash --keep-index)太麻烦时,我发现更容易使用 diffcheckoutapply

仅“存储”特定文件/目录:

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

然后

git apply stashed.diff

When git stash -p (or git add -p with stash --keep-index) would be too cumbersome, I found it easier to use diff, checkout and apply:

To "stash" a particular file/dir only:

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

Then afterwards

git apply stashed.diff
时光是把杀猪刀 2024-09-12 19:59:12

如果您不想指定包含隐藏更改的消息,请在双破折号后传递文件名。

$ git stash -- filename.ext

如果它是未跟踪/新文件,您必须先暂存它。

此方法适用于 git 版本 2.13+

If you do not want to specify a message with your stashed changes, pass the filename after a double-dash.

$ git stash -- filename.ext

If it's an untracked/new file, you will have to stage it first.

This method works in git versions 2.13+

遮了一弯 2024-09-12 19:59:12

假设您有 3 个文件

a.rb
b.rb
c.rb

,并且您只想存储 b.rb 和 c.rb,但不想存储 a.rb,

您可以执行以下操作

# commit the files temporarily you don't want to stash
git add a.rb
git commit -m "temp" 

# then stash the other files
git stash save "stash message"

# then undo the previous temp commit
git reset --soft HEAD^
git reset

,您就完成了!
HTH。

Let's say you have 3 files

a.rb
b.rb
c.rb

and you want to stash only b.rb and c.rb but not a.rb

you can do something like this

# commit the files temporarily you don't want to stash
git add a.rb
git commit -m "temp" 

# then stash the other files
git stash save "stash message"

# then undo the previous temp commit
git reset --soft HEAD^
git reset

And you are done!
HTH.

执妄 2024-09-12 19:59:12

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

它将存储所有 >取消暂存更改的文件

If you want to stash only some of changed files, simply just Add other files in the Stage, Then execute git stash push --keep-index

It will stash all unstaged changed files

秋意浓 2024-09-12 19:59:12

另一种方法是:

# 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 stash 作为“某处”(因此,最后有一个额外的步骤:删除您放入存储中的更改,因为您也将它们移开了)。

Another way to do this:

# 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">

I came up with this after I (once again) came to this page and didn't like the first two answers (the first answer just doesn't answer the question and I didn't quite like working with the -p interactive mode).

The idea is the same as what @VonC suggested using files outside the repository, you save the changes you want somewhere, remove the changes you don't want in your stash, and then re-apply the changes you moved out of the way. However, I used the git stash as the "somewhere" (and as a result, there's one extra step at the end: removing the cahnges you put in the stash, because you moved these out of the way as well).

森林迷了鹿 2024-09-12 19:59:12

您可以简单地执行此操作:

git stash push "filename"

或使用可选消息

git stash push -m "Some message" "filename"

You can simply do this:

git stash push "filename"

or with an optional message

git stash push -m "Some message" "filename"
稍尽春風 2024-09-12 19:59:12

更新(2/14/2015) - 我稍微重写了脚本,以更好地处理冲突的情况,现在应该以未合并的冲突而不是 .rej 文件的形式呈现。


我经常发现与 @bukzor 的方法相反的做法更直观。也就是说,暂存一些更改,然后仅存储那些暂存的更改。

不幸的是,git 不提供 git stash --only-index 或类似的功能,所以我编写了一个脚本来执行此操作。

#!/bin/sh

# first, go to the root of the git repo
cd `git rev-parse --show-toplevel`

# create a commit with only the stuff in staging
INDEXTREE=`git write-tree`
INDEXCOMMIT=`echo "" | git commit-tree $INDEXTREE -p HEAD`

# create a child commit with the changes in the working tree
git add -A
WORKINGTREE=`git write-tree`
WORKINGCOMMIT=`echo "" | git commit-tree $WORKINGTREE -p $INDEXCOMMIT`

# get back to a clean state with no changes, staged or otherwise
git reset -q --hard

# Cherry-pick the index changes back to the index, and stash.
# This cherry-pick is guaranteed to succeed
git cherry-pick -n $INDEXCOMMIT
git stash

# Now cherry-pick the working tree changes. This cherry-pick may fail
# due to conflicts
git cherry-pick -n $WORKINGCOMMIT

CONFLICTS=`git ls-files -u`
if test -z "$CONFLICTS"; then
    # If there are no conflicts, it's safe to reset, so that
    # any previously unstaged changes remain unstaged
    #
    # However, if there are conflicts, then we don't want to reset the files
    # and lose the merge/conflict info.
    git reset -q
fi

您可以将上述脚本保存为路径中某处的 git-stash-index ,然后可以将其调用为 git stash-index

# <hack hack hack>
git add <files that you want to stash>
git stash-index

现在,存储包含一个新条目,其中仅包含您暂存的更改,并且您的工作树仍然包含任何未暂存的更改。

在某些情况下,工作树的更改可能取决于索引的更改,因此当您存储索引更改时,工作树的更改会发生冲突。在这种情况下,您将遇到常见的未合并冲突,可以使用 git merge/git mergetool/etc 来解决。

Update (2/14/2015) - I've rewritten the script a bit, to better handle the case of conflicts, which should now be presented as unmerged conflicts rather than .rej files.


I often find it more intuitive to do the inverse of @bukzor's approach. That is, to stage some changes, and then stash only those staged changes.

Unfortunately, git doesn't offer a git stash --only-index or similar, so I whipped up a script to do this.

#!/bin/sh

# first, go to the root of the git repo
cd `git rev-parse --show-toplevel`

# create a commit with only the stuff in staging
INDEXTREE=`git write-tree`
INDEXCOMMIT=`echo "" | git commit-tree $INDEXTREE -p HEAD`

# create a child commit with the changes in the working tree
git add -A
WORKINGTREE=`git write-tree`
WORKINGCOMMIT=`echo "" | git commit-tree $WORKINGTREE -p $INDEXCOMMIT`

# get back to a clean state with no changes, staged or otherwise
git reset -q --hard

# Cherry-pick the index changes back to the index, and stash.
# This cherry-pick is guaranteed to succeed
git cherry-pick -n $INDEXCOMMIT
git stash

# Now cherry-pick the working tree changes. This cherry-pick may fail
# due to conflicts
git cherry-pick -n $WORKINGCOMMIT

CONFLICTS=`git ls-files -u`
if test -z "$CONFLICTS"; then
    # If there are no conflicts, it's safe to reset, so that
    # any previously unstaged changes remain unstaged
    #
    # However, if there are conflicts, then we don't want to reset the files
    # and lose the merge/conflict info.
    git reset -q
fi

You can save the above script as git-stash-index somewhere on your path, and can then invoke it as git stash-index

# <hack hack hack>
git add <files that you want to stash>
git stash-index

Now the stash contains a new entry that only contains the changes you had staged, and your working tree still contains any unstaged changes.

In some cases, the working tree changes may depend on the index changes, so when you stash the index changes, the working tree changes have a conflict. In this case, you'll get the usual unmerged conflicts that you can resolve with git merge/git mergetool/etc.

无需解释 2024-09-12 19:59:12

由于在 Git 中创建分支很简单,您只需创建一个临时分支并将各个文件签入其中即可。

Since creating branches in Git is trivial you could just create a temporary branch and check the individual files into it.

青瓷清茶倾城歌 2024-09-12 19:59:12

以防万一您实际上是在使用 git stash 时放弃更改(并且不真正使用 git stash 暂时存储它),在这种情况下您可以使用

git checkout -- <file>

[< strong>注意]

git stash 只是分支和执行操作的更快、更简单的替代方案。

Just in case you actually mean discard changes whenever you use git stash (and don't really use git stash to stash it temporarily), in that case you can use

git checkout -- <file>

[NOTE]

That git stash is just a quicker and simple alternative to branching and doing stuff.

溺孤伤于心 2024-09-12 19:59:12

将以下代码保存到文件中,例如,名为 stash。用法是stash。参数是文件完整路径的正则表达式。例如,要存储 a/b/c.txt、stash a/b/c.txtstash .*/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
    }
  }
}

Save the following code to a file, for example, named stash. Usage is stash <filename_regex>. The argument is the regular expression for the full path of the file. For example, to stash a/b/c.txt, stash a/b/c.txt or stash .*/c.txt, etc.

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

Code to copy into the file:

#! /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
    }
  }
}
冷默言语 2024-09-12 19:59:12

VonC 将文件复制到 Git 存储库外部的“中间”解决方案的问题是,您会丢失路径信息,这使得稍后将一堆文件复制回来会有些麻烦。

A 发现使用 tar (类似的工具可能会这样做)而不是复制更容易:

  • tar cvf /tmp/stash.tar path/to/some/file path/to/some/other/file (... etc.)
  • git checkout path/to/some/file path/to/some/other/file
  • git stash
  • tar xvf /tmp/stash.tar
  • 等(参见 VonC 的“中间”建议)

The problem with VonC's `intermediate' solution of copying files to outside the Git repo is that you lose path information, which makes copying a bunch of files back later on somewhat of a hassle.

A find it easier to use tar (similar tools will probably do) instead of copy:

  • tar cvf /tmp/stash.tar path/to/some/file path/to/some/other/file (... etc.)
  • git checkout path/to/some/file path/to/some/other/file
  • git stash
  • tar xvf /tmp/stash.tar
  • etc. (see VonC's `intermediate' suggestion)
冷月断魂刀 2024-09-12 19:59:12

我会使用 git stash save --patch 。我不认为交互性很烦人,因为其中有一些选项可以将所需的操作应用于整个文件。

I would use git stash save --patch. I don't find the interactivity to be annoying because there are options during it to apply the desired operation to entire files.

绝不服输 2024-09-12 19:59:12

目前(2024 年),Git 使用新的 --staged 选项提供了一种更简单的方法来实现此目的:

git add <some-files>                  # pick (stage) the files to stash
git stash save --staged 'my stash'    # stash only staged

令我惊讶的是,在所有答案中没有人提供最新 Git 版本的解决方案(>= 2.35)了解如何仅暂存许多未暂存更改中的特定文件。

Currently (2024), Git is providing a much simpler way to achieve this using the new --staged option:

git add <some-files>                  # pick (stage) the files to stash
git stash save --staged 'my stash'    # stash only staged

To my suprise, among all the answers nobody provided the solution for recent Git versions (>= 2.35) on how to stage only specific files out of many unstaged changes.

墨离汐 2024-09-12 19:59:12

有时,在提交之前,我在分支上进行了不相关的更改,并且我想将其移动到另一个分支并单独提交(例如 master)。我这样做:

git stash
git checkout master
git stash pop
git add <files that you want to commit>
git commit -m 'Minor feature'
git stash
git checkout topic1
git stash pop
...<resume work>...

注意第一个 stash & stash pop 可以被消除,您可以在签出时将所有更改转移到 master 分支,但前提是不存在冲突。此外,如果您要为部分更改创建新分支,您将需要存储。

假设没有冲突并且没有新分支,您可以简化它:

git checkout master
git add <files that you want to commit>
git commit -m 'Minor feature'
git checkout topic1
...<resume work>...

甚至不需要存储......

Sometimes I've made an unrelated change on my branch before I've committed it, and I want to move it to another branch and commit it separately (like master). I do this:

git stash
git checkout master
git stash pop
git add <files that you want to commit>
git commit -m 'Minor feature'
git stash
git checkout topic1
git stash pop
...<resume work>...

Note the first stash & stash pop can be eliminated, you can carry all of your changes over to the master branch when you checkout, but only if there are no conflicts. Also if you are creating a new branch for the partial changes you will need the stash.

You can simplify it assuming no conflicts and no new branch:

git checkout master
git add <files that you want to commit>
git commit -m 'Minor feature'
git checkout topic1
...<resume work>...

Stash not even needed...

无法言说的痛 2024-09-12 19:59:12

使用 SourceTree 只需 3 个步骤即可轻松完成此操作。

  1. 暂时提交您不想隐藏的所有内容。
  2. Git 添加其他所有内容,然后将其隐藏。
  3. 通过运行 git reset 弹出临时提交,将目标定位在临时提交之前。

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

This can be done easily in 3 steps using SourceTree.

  1. Temporarily commit everything you don't want stashed.
  2. Git add everything else, then stash it.
  3. Pop your temporary commit by running git reset, targetting the commit before your temporary one.

This can all be done in a matter of seconds in SourceTree, where you can just click on the files (or even individual lines) you want to add. Once added, just commit them to a temporary commit. Next, click the checkbox to add all changes, then click stash to stash everything. With the stashed changes out of the way, glance over at your commit list and note the hash for the commit before your temporary commit, then run 'git reset hash_b4_temp_commit', which is basically like "popping" the commit by resetting your branch to the commit right before it. Now, you're left with just the stuff you didn't want stashed.

沫尐诺 2024-09-12 19:59:12

这里的每个答案都如此复杂......

这个“隐藏”怎么样:

git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash

这将文件更改弹出:

git apply /tmp/stash.patch

与隐藏一个文件并将其弹出回来的行为完全相同。

Every answer here is so complicated...

What about this to "stash":

git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash

This to pop the file change back:

git apply /tmp/stash.patch

Exact same behavior as stashing one file and popping it back in.

讽刺将军 2024-09-12 19:59:12

我已经查看了此主题以及许多类似主题的答案和评论。请注意,以下命令都不正确,无法存储任何特定的跟踪/未跟踪文件

  • git stash -p (--patch): select手动汉克斯,排除未跟踪的文件
  • git stash -k (--keep-index):存储所有跟踪/未跟踪的文件并将它们保存在工作目录
  • git stash -u (--include- untracked): stash 所有跟踪/未跟踪的文件
  • git stash -p (--patch) -u (--include-untracked): invalid command

目前最合理的方法存储任何特定的跟踪/未跟踪文件的方法是:

  • 临时提交您不想存储的文件
  • 添加并存储
  • 弹出临时提交

< strong>我在回答另一个问题时为此过程编写了一个简单的脚本,并且有步骤在此处执行 SourceTree 中的过程

I've reviewed answers and comments for this and a number of similar threads. Be aware that none of the following commands are correct for the purpose of being able to stash any specific tracked/untracked files:

  • git stash -p (--patch): select hunks manually, excluding untracked files
  • git stash -k (--keep-index): stash all tracked/untracked files and keep them in the working directory
  • git stash -u (--include-untracked): stash all tracked/untracked files
  • git stash -p (--patch) -u (--include-untracked): invalid command

Currently, the most reasonable method to be able to stash any specific tracked/untracked files is to:

  • Temporarily commit the files you don't want to stash
  • Add and stash
  • Pop the temporary commit

I wrote a simple script for this procedure in an answer to another question, and there are steps for performing the procedure in SourceTree here.

余罪 2024-09-12 19:59:12

当您尝试在两个分支之间切换时,就会发生这种情况。

尝试使用“git add filepath”添加文件。

稍后执行这一行

git stash --keep-index

When you try to switch between two branches, this situation occurs.

Try to add the files using "git add filepath".

Later execute this line

git stash --keep-index

起风了 2024-09-12 19:59:12

解决方案

本地更改:

  • 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}

完成。


解释

  1. file_C添加到暂存区域
  2. 创建一个名为“temp_stash”的临时存储并保留file_C上的更改
  3. 创建所需的存储(“my_stash”),仅对file_C进行更改
  4. 应用“temp_stash”中的更改(file_A 和 file_B)在本地代码上并删除存储

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

Solution

Local changes:

  • file_A (modified) not staged
  • file_B (modified) not staged
  • file_C (modified) not staged

To create a stash "my_stash" with only the changes on file_C:

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}

Done.


Explanation

  1. add file_C to the staging area
  2. create a temporary stash named "temp_stash" and keep the changes on file_C
  3. create the wanted stash ("my_stash") with only the changes on file_C
  4. apply the changes in "temp_stash" (file_A and file_B) on your local code and delete the stash

You can use git status between the steps to see what is going on.

昵称有卵用 2024-09-12 19:59:12

要存储单个文件,请使用 git stash --patch [file] 。

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

To stash a single file use git stash --patch [file].

This is going to prompt: Stash this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?. Just type a (stash this hunk and all later hunks in the file) and you're fine.

殊姿 2024-09-12 19:59:12

类似的情况。做了承诺并意识到这是不行的。

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

Similar situation. Did commit and realized it's not ok.

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

Based on the answers this helped me.

# 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
往日 2024-09-12 19:59:12

在这种情况下,我git add -p(交互式),git commit -m blah,然后在必要时隐藏剩下的内容。

In this situation I git add -p (interactive), git commit -m blah and then stash what's left if necessary.

痴梦一场 2024-09-12 19:59:12

我不知道如何在命令行上执行此操作,只能使用 SourceTree。假设您已更改文件 A,并在文件 B 中有两个更改块。如果您只想将第二个块存储在文件 B 中并保持其他所有内容不变,请执行以下操作:

  1. 暂存所有内容
  2. 对工作副本执行更改,撤消所有更改文件 A 中的更改。(例如,启动外部 diff 工具并使文件匹配。)
  3. 使文件 B 看起来好像只应用了第二次更改。 (例如,启动外部差异工具并撤消第一个更改。)
  4. 使用“保留分阶段更改”创建存储。
  5. 取消一切
  6. 已完成!

I don't know how to do it on command line, only using SourceTree. Lets say you have changed file A, and have two change hunks in file B. If you want to stash only the second hunk in file B and leave everything else untouched, do this:

  1. Stage everything
  2. Perform changes to your working copy that undo all the changes in file A. (e.g. launch external diff tool and make files match.)
  3. Make file B look as if only second change is applied to it. (e.g. launch external diff tool and undo first change.)
  4. Create a stash using "Keep staged changes".
  5. Unstage everything
  6. Done!
世态炎凉 2024-09-12 19:59:12
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)
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)
寄离 2024-09-12 19:59:12

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

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

这仅存储一个文件。

I found no answer to be what I needed and that is as easy as:

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

This stashes exactly one file.

友欢 2024-09-12 19:59:12

一种复杂的方法是首先提交所有内容:

git add -u
git commit // creates commit with sha-1 A

重置回原始提交,但从新提交中检出 the_one_file:

git reset --hard HEAD^
git checkout A path/to/the_one_file

现在您可以存储 the_one_file:

git stash

通过将提交的内容保存在文件系统中同时重置回原始提交进行清理:

git reset --hard A
git reset --soft HEAD^

是的,有点尴尬……

One complicated way would be to first commit everything:

git add -u
git commit // creates commit with sha-1 A

Reset back to the original commit but checkout the_one_file from the new commit:

git reset --hard HEAD^
git checkout A path/to/the_one_file

Now you can stash the_one_file:

git stash

Cleanup by saving the committed content in your file system while resetting back to the original commit:

git reset --hard A
git reset --soft HEAD^

Yeah, somewhat awkward...

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