有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。

我做错了吗?我是否误解了git如何以其他方式简化我的过程?


当前回答

Git隐藏推送——自2.35以来已上演#

只保存当前暂存的更改。这类似于基本的git提交,除了状态提交到stash而不是当前分支。

OLD

对于最新的git,您可以使用——patch选项

git stash push --patch   # since 2.14.6

git stash save --patch   # for older git versions

git会询问你文件中的每一个变化是否要添加到stash中。 你只需回答y或n

老乌利希期刊指南 别名DOUBLE STASH:

git config --global alias.stash-staged '!bash -c "git stash --keep-index; git stash push -m "staged" --keep-index; git stash pop stash@{1}"'

现在您可以暂存文件,然后运行git stash- staging。 因此,您的阶段性文件将保存到stash。

如果您不想保留阶段性文件,并希望将它们移动到隐藏。然后你可以添加另一个别名并运行git move- staging:

git config --global alias.move-staged '!bash -c "git stash-staged;git commit -m "temp"; git stash; git reset --hard HEAD^; git stash pop"'

其他回答

Git stash -keep-index是一个很好的解决方案…除了在已删除的路径上无法正常工作,这已在Git 2.23(2019年第三季度)中得到修复

参见Thomas Gummerer (tummerer)提交b932f6a(2019年7月16日)。 (由Junio C Hamano - gitster -在commit f8aee85中合并,2019年7月25日)

stash: fix handling removed files with --keep-index git stash push --keep-index is supposed to keep all changes that have been added to the index, both in the index and on disk. Currently this doesn't behave correctly when a file is removed from the index. Instead of keeping it deleted on disk, **--keep-index currently restores the file.** Fix that behaviour by using 'git checkout' in no-overlay mode which can faithfully restore the index and working tree. This also simplifies the code. Note that this will overwrite untracked files if the untracked file has the same name as a file that has been deleted in the index.

我还没有见过这种不需要使用git stash的解决方案:

你甚至不需要使用git隐藏在所有。你可以使用一个专用的分支来解决这个问题(分支很便宜)。

实际上,你可以用几个连续的命令分别隔离非变化和阶段性变化,你可以把它们捆绑在一个git别名中:

创建并切换到一个新的分支,在那里你将分别提交阶段性和非阶段性的更改:参见这里

在任何时候,你都可以从创建的分支中选择-e一个提交,并将其应用到你想要的地方(-e更改其提交消息)。

当你不再需要它时,你可以删除这个“收藏分支”。你可能不得不使用-D选项来强制删除(而不是-D普通选项),因为该分支没有合并,git可能会认为如果你删除它会有丢失数据的风险。如果你没有选择删除前的提交,这是正确的:

git branch -D separated-stashes

您还可以为~/添加别名。Gitconfig,以便自动化此行为:

git config --global alias.bratisla '!git switch -c separated-stashes; git commit -m "staged changes"; git add -u; git commit -m "unstaged changes"; git switch -' # why this name ? : youtu.be/LpE1bJp8-4w

在“藏” 在“藏”



当然,您也可以使用两个连续的存储来实现相同的结果

正如在其他答案中所述,您可以使用git stash (-k|——keep-index)与其他命令组合使用一些方法来仅保存非阶段性或仅阶段性的更改。

我个人认为-k选项非常令人困惑,因为它存储了所有内容,但将阶段性更改保持在阶段性状态(这解释了为什么使用“——keep-index”)。而储存东西通常会把它移动到一个储存入口。使用-k,非暂存的更改通常会被存储,但暂存的更改只是复制到相同的暂存条目。


第0步:你的git状态中有两个东西:一个包含阶段性变化的文件,另一个包含非阶段性变化的文件。

第一步:保存非阶段性+阶段性变化,但将阶段性变化保留在索引中:

git stash -k -m "all changes"

-m "…"部分是可选的,git stash -k实际上是git stash push -k的别名(它不会远程推送任何东西,顺便说一句,不用担心),它接受一个-m选项来标记你的stash条目,以使其清晰(就像一个提交消息或标签,但对于一个stash条目)。它是已弃用的git存储保存的新版本。


步骤1bis(可选):

git stash

隐藏阶段性更改(仍在索引中)。 这一步对于下面的内容不是必需的,但是它表明,如果您愿意,您可以只将阶段性的更改放在存储条目中。 如果你使用这一行,你必须在继续第二步之前git stash (pop|apply) && git add -u。


第二步:

git commit -m "staged changes"

提交只包含从步骤0开始的阶段性更改,它包含与步骤1bis开始的隐藏条目相同的内容。


第三步:

git stash (pop|apply)

恢复第1步的存储。 注意,这个隐藏条目包含了所有内容,但是由于您已经提交了阶段性更改,所以这个隐藏只会添加从第0步开始的非阶段性更改。

注意:这里的“restore”并不是指“git restore”,这是一个不同的命令。


第四步:

git add -u

将弹出的存储的内容添加到索引中


第五步:

git commit -m "unstaged changes"

这里的“未分期”,与步骤2和步骤3注释中的“分期”一样,指的是步骤0。实际上,您正在登台并提交从步骤0开始的“登台更改”。


完成了! 您现在有两个独立的提交,其中包含从第0步开始的(un)阶段性更改。 您可能希望修改/重新设置它们以进行其他更改或重命名/删除/压缩它们。 取决于你对你的stash的堆栈做了什么(弹出或应用),你可能还想要git stash (drop|clear)它。你可以看到你用git stash (list|show)

我制作了一个脚本,只存储当前正在上演的内容,并保留其他所有内容。当我开始做太多不相关的改变时,这是非常棒的。简单地暂存与所需的提交和存储无关的内容。

(感谢bartjoomiej提供的起点)

#!/bin/bash

#Stash everything temporarily.  Keep staged files, discard everything else after stashing.
git stash --keep-index

#Stash everything that remains (only the staged files should remain)  This is the stash we want to keep, so give it a name.
git stash save "$1"

#Apply the original stash to get us back to where we started.
git stash apply stash@{1}

#Create a temporary patch to reverse the originally staged changes and apply it
git stash show -p | git apply -R

#Delete the temporary stash
git stash drop stash@{1}

在这个场景中,我更喜欢为每个问题创建新的分支。我使用了一个前缀temp/,所以我知道我可以稍后删除这些分支。

git checkout -b temp/bug1

准备修复bug1的文件并提交它们。

git checkout -b temp/bug2

然后,您可以根据需要从相应的分支中选择提交,并提交一个拉取请求。

是否绝对有必要同时处理多个bug ?这里的“立刻”指的是“同时对文件进行多个错误的编辑”。因为除非您绝对需要,否则我在您的环境中一次只处理一个错误。这样你就可以使用本地分支和重新建立基础,我发现这比管理复杂的存储/阶段要容易得多。

假设master在提交b,现在处理bug #1。

git checkout -b bug1

现在您在分支bug1上。做一些修改,提交,等待代码审查。这是本地的,所以你不会影响到其他人,而且从git差异中制作补丁应该很容易。

A-B < master
   \
    C < bug1

现在您正在处理bug2。用git checkout master返回master。创建一个新的分支,git checkout -b bug2。进行更改,提交,等待代码审查。

    D < bug2
   /
A-B < master
   \
    C < bug1

让我们假设在您等待审查时,其他人在master上提交了E & F。

    D < bug2
   /
A-B-E-F < master
   \
    C < bug1

当你的代码被批准后,你可以按照以下步骤将其重新设置为master:

git checkout bug1
git rebase master
git checkout master
git merge bug1

这将导致以下结果:

    D < bug2
   /
A-B-E-F-C' < master, bug1

然后你可以推送,删除你的本地bug1分支,然后你就可以走了。在您的工作空间中,一次只处理一个错误,但是通过使用本地分支,您的存储库可以处理多个错误。这样就避免了复杂的舞台舞蹈。

请在评论中回答ctote的问题:

那么,您可以回到为每个bug存储,并且一次只处理一个bug。至少这省去了分期问题。但尝试过之后,我个人觉得很麻烦。在git日志图中,存储有点混乱。更重要的是,如果你把事情搞砸了,你就无法挽回了。如果你有一个脏的工作目录,你弹出一个隐藏,你不能“撤消”这个弹出。搞砸已经存在的提交要困难得多。

那么,这就叫反应i。

当您将一个分支重新基于另一个分支时,您可以交互地进行(-i标志)。当您这样做时,您可以选择对每次提交做什么。Pro Git是一本很棒的书,它也是HTML格式的在线,并且有一个关于重基和压缩的不错的部分:

http://git-scm.com/book/ch6-4.html

为了方便起见,我将逐字逐句地引用他们的例子。假设你有以下提交历史记录,你想要将bug1重置并压缩到master上:

    F < bug2
   /
A-B-G-H < master
   \
    C-D-E < bug1

下面是当你输入git rebase -i master bug1时你会看到的结果

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

要将一个分支的所有提交压缩为一次提交,请将第一次提交保留为“pick”,并将所有后续的“pick”条目替换为“squash”或简单的“s”。您还将有机会更改提交消息。

pick f7f3f6d changed my name a bit
s 310154e updated README formatting and added blame
s a5f4a0d added cat-file
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit

所以,是的,压扁是有点痛苦,但我仍然建议它比大量使用存储。