有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。
我做错了吗?我是否误解了git如何以其他方式简化我的过程?
有没有办法让我把阶段性的变化藏起来?我遇到问题的情况是,我在给定的时间内处理了几个bug,并且有几个未分阶段的更改。我希望能够单独运行这些文件,创建我的.patch文件,并将它们保存起来,直到代码被批准。这样,当它被批准时,我可以隐藏我的整个(当前)会话,弹出错误并推送代码。
我做错了吗?我是否误解了git如何以其他方式简化我的过程?
当前回答
如果你没有一个更新的git,它有——staging选项,下面是如何直接做到这一点。
git stash命令只是一个复杂的shell脚本,用于操作树对象和提交等。我们可以手动完成它所做的事情。
概述
隐藏堆栈记录特殊的提交。我们将从阶段性更改中创建一个提交,然后手动将其转移到存储中。然后,取消提交。
设置
我有一个项目,其中Makefile有两个更改。一个是有舞台的,一个是没有舞台的:
$ git diff --cached
diff --git a/Makefile b/Makefile
index 4ca6058f..c8c7480a 100644
--- a/Makefile
+++ b/Makefile
@@ -605,7 +605,7 @@ conftest2: conftest1.c conftest2.c
$(V)if echo | $(CC) -dM -E - | grep -s __ANDROID__ > /dev/null 2>&1 ; then \
echo yes ; \
fi
-
+# FOO
.PHONY: conftest.clean
conftest.clean:
$(V)rm -f conftest$(EXE) conftest.[co] \
$ git diff
diff --git a/Makefile b/Makefile
index c8c7480a..270c313d 100644
--- a/Makefile
+++ b/Makefile
@@ -611,3 +611,4 @@ conftest2: conftest1.c conftest2.c
$(V)rm -f conftest$(EXE) conftest.[co] \
conftest2$(EXE) conftest[12].[oc] \
conftest.err
+# BAR
添加# FOO行是阶段性的;# BAR的添加是无阶段的。
步骤1:创建树对象。
首先,我们从当前索引(包含分段项)创建一个树对象。
$ git write-tree
0d9651ad74328e747a053a9434d9867c8cd79d41 <-- output
步骤2:创建两次提交。
首先,从树中创建一个提交,它有一个父结点,即当前分支HEAD:
$ git commit-tree -p HEAD -m 'add # FOO' 0d9651ad74328e747a053a9434d9867c8cd79d41
baa34222e781078d82cefed519ff105715c7f665 <-- output
然后,从树中创建另一个提交,它有两个父节点:HEAD和baa34222…我们刚刚做出的承诺:
$ git commit-tree -p HEAD -p baa34222e781078d82cefed519ff105715c7f665 -m 'add # FOO' 0d9651ad74328e747a053a9434d9867c8cd79d41
2c96b028e475a05d84f472da7f2a70ac53d0ac90 <-- output
这个双亲2c96b02…将是我们安装到隐藏的提交。
注意,git commit-tree不是git commit。这是一个低级别的命令。这些提交不会对当前分支做任何事情;我们只是在Git的存储中分配对象,并没有对我们所在的分支做任何事情,也没有改变索引或工作树。
步骤3。
接下来,我们将这个提交写到.git/refs/stash中。你可能需要备份这个文件。
$ echo 2c96b028e475a05d84f472da7f2a70ac53d0ac90 > .git/refs/stash
步骤4。
我们将同样的提交钩子到.git/logs/refs/stash文件中。在编辑之前,文件中的最后一行看起来是这样的:
b1819d98ab24720796315b9497236172d1fb1f5f 3b2ecc6604d77c9df4fe72efd1fbd384b2c43f76 Au Thor <author@example.com> 1654892876 -0700 On master: elim-aliases
我们手动添加这条伪行:
3b2ecc6604d77c9df4fe72efd1fbd384b2c43f76 2c96b028e475a05d84f472da7f2a70ac53d0ac90 Au Thor <author@example.com> 1654892876 -0700 On master: add # FOO
您可能还想备份此文件。然而,如果出了问题,事情很容易恢复。
注意左边哈希3b2ecc…在这个新行中与前一行中的右哈希相同。这是之前的存储提交,与我们在这里所做的无关,必须重复将这一行链接到存储堆栈中。在它的右边,我们有我们的散列2c96b028e4....然后剩下的人都假装离开了。在时区-0700后面有一个硬选项卡,而不是空格。我只是复制粘贴了一下。
第5步。
我们验证我们已经将commit添加到存储堆栈:
$ git stash list | head -3
stash@{0}: On master: add # FOO
stash@{1}: On master: elim-aliases
stash@{2}: On master: compiler-safe-eval
and:
$ git stash show -p
diff --git a/Makefile b/Makefile
index 4ca6058f..c8c7480a 100644
--- a/Makefile
+++ b/Makefile
@@ -605,7 +605,7 @@ conftest2: conftest1.c conftest2.c
$(V)if echo | $(CC) -dM -E - | grep -s __ANDROID__ > /dev/null 2>&1 ; then \
echo yes ; \
fi
-
+# FOO
.PHONY: conftest.clean
conftest.clean:
$(V)rm -f conftest$(EXE) conftest.[co] \
它在那里;Git stash认为我们的提交是一个“类仓库”的提交,并接受它。
总结
我们手动获取一个分阶段更改的索引,并生成一个树对象。 然后,我们将树对象转换为常规提交对象,然后再进行一个双亲提交。双父对象作为类存储提交是可以接受的。 最后,我们通过编辑一对文件手动将这个提交修补到存储堆栈中。
附录
我们没有执行任何操作索引或工作树的不安全命令。但是,我们已经不安全地操作了git存储堆栈。如果出现问题,以下是如何修复它(而不是从备份文件恢复):
删除我们添加到.git/logs/refs/stash的伪行,这样这仍然是最后一行: b1819d98ab24720796315b9497236172d1fb1f5f 3b2ecc6604d77c9df4fe72efd1fbd384b2c43f76 Au Thor <author@example.com> 1654892876 -0700 On master: elime -aliases 取右边的散列3b2ecc6604d77c9df4fe72efd1fbd384b2c43f76并将其植入到.git/refs/stash文件中: $ echo 3b2ecc6604d77c9df4fe72efd1fbd384b2c43f76 > .git/refs/stash
以前的收藏现在恢复了。
其他回答
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.
根据你对Mike Monkiewicz的回答,我建议使用一个更简单的模型:使用常规的开发分支,但是使用合并的squash选项来在你的主分支中获得一个单一的提交:
git checkout -b bug1 # create the development branch
* hack hack hack * # do some work
git commit
* hack hack hack *
git commit
* hack hack hack *
git commit
* hack hack hack *
git commit
git checkout master # go back to the master branch
git merge --squash bug1 # merge the work back
git commit # commit the merge (don't forget
# to change the default commit message)
git branch -D bug1 # remove the development branch
这个过程的优点是您可以使用正常的git工作流程。
为什么不提交对某个错误的更改,并根据该提交及其前身创建一个补丁呢?
# hackhackhack, fix two unrelated bugs
git add -p # add hunks of first bug
git commit -m 'fix bug #123' # create commit #1
git add -p # add hunks of second bug
git commit -m 'fix bug #321' # create commit #2
然后,创建适当的补丁,使用git format-patch:
git format-patch HEAD^^
这将创建两个文件:0001-fix-bug-123。补丁和0002-fix-bug-321.patch
或者您可以为每个错误创建单独的分支,这样您就可以单独合并或重新建立错误修复,甚至在它们不起作用时删除它们。
这个回答被接受了,但太老了。现在你可以把它藏起来-S见https://stackoverflow.com/a/70231955/683157
最初的回答: 是的,这是可能的DOUBLE STASH
准备好你需要藏匿的所有文件。 运行git stash——keep-index。这个命令将创建一个包含所有更改(阶段性和非阶段性)的存储,但是将阶段性更改保留在工作目录中(仍然处于阶段性状态)。 运行git stash push -m "good stash"(添加-u标志以在stash中包含新文件) 现在你的“好收藏”只有阶段性文件。
现在,如果你需要非暂存文件在stash之前,只需应用第一个stash(一个创建的——keep-index),现在你可以删除文件,你储存到“良好的stash”。
享受
在Git 2.35 (Q1 2022)中,“Git stash”(man)学会了——staging选项来隐藏添加到索引中的内容(而不是其他内容)。
所以现在这是官方支持的(8年后)。
参见Sergey Organov (sorganov)的commit a8a6e06(2021年10月28日)和commit 41a28eb(2021年10月18日)。 (由Junio C Hamano—gitster—在commit 44ac8fd中合并,2021年11月29日)
Stash:为“推送”和“保存”执行“——staging”选项 署名:Sergey Organov
只保存已上演的更改。 这种模式允许轻松地隐藏一些与当前正在进行的工作无关的更改,以便以后重用。 与'stash push -patch'不同,- staging支持使用任何工具来选择要存储的更改,包括但不限于'git add -interactive'(man)。
Git stash现在包括在它的手册页:
“git藏”[推动[p |——补丁][s |——上演][- k |——[不——]keep-index] [q |——安静)
Git stash现在包括在它的手册页:
保存[- p |——补丁][s |——上演][- k |——[不——]keep-index] [- u |——include-untracked][——|——所有][q |——安静][<消息>]
Git stash现在包括在它的手册页:
- s ——上演了 此选项仅对推送和保存命令有效。 只保存当前暂存的更改。这类似于 基本的git提交,除了状态被提交到stash 当前分支的。 ——patch选项优先于此选项。
Git stash现在包括在它的手册页:
Saving unrelated changes for future use When you are in the middle of massive changes and you find some unrelated issue that you don't want to forget to fix, you can do the change(s), stage them, and use git stash push --staged to stash them out for future use. This is similar to committing the staged changes, only the commit ends-up being in the stash and not on the current branch. ---------------------------------------------------------------- # ... hack hack hack ... $ git add --patch foo # add unrelated changes to the index $ git stash push --staged # save these changes to the stash # ... hack hack hack, finish curent changes ... $ git commit -m 'Massive' # commit fully tested changes $ git switch fixup-branch # switch to another branch $ git stash pop # to finish work on the saved changes ----------------------------------------------------------------