git reset --soft 的实际用途? [英] Practical uses of git reset --soft?

查看:58
本文介绍了git reset --soft 的实际用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 git 一个多月了.确实我昨天才第一次使用reset,但是软复位对我来说仍然没有太大意义.

I have been working with git for just over a month. Indeed I have used reset for the first time only yesterday, but the soft reset still doesn't make much sense to me.

我知道我可以使用软重置来编辑提交而无需更改索引或工作目录,就像使用 git commit --amend 一样.

I understand I can use the soft reset to edit a commit without altering the index or the working directory, as I would with git commit --amend.

这两个命令真的一样吗(reset --soft vs commit --amend)?有什么理由在实际中使用其中之一吗?更重要的是,除了修改提交之外,reset --soft 还有其他用途吗?

Are these two commands really the same (reset --soft vs commit --amend)? Any reason to use one or the other in practical terms? And more importantly, are there any other uses for reset --soft apart from amending a commit?

推荐答案

git reset 是关于移动 HEAD, 通常是分支ref.
问题:工作树和索引怎么样?
当与 --soft 一起使用时,移动 HEAD,最常更新分支引用,只有 HEAD.
这与 commit --amend 的不同之处在于:

git reset is all about moving HEAD, and generally the branch ref.
Question: what about the working tree and index?
When employed with --soft, moves HEAD, most often updating the branch ref, and only the HEAD.
This differ from commit --amend as:

  • 它不会创建新的提交.
  • 它实际上可以将 HEAD 移动到任何提交(因为 commit --amend 只是关于 移动 HEAD,同时允许重做当前提交)
  • it doesn't create a new commit.
  • it can actually move HEAD to any commit (as commit --amend is only about not moving HEAD, while allowing to redo the current commit)

刚刚找到这个组合的例子:

Just found this example of combining:

  • 经典合并
  • 子树合并

全部合二为一(八爪鱼,因为合并了两个以上的分支)提交合并.

all into one (octopus, since there is more than two branches merged) commit merge.

Tomas "wereHamster" Carnecky 在他的 子树章鱼合并"文章:

  • 如果您想将一个项目合并到另一个项目的子目录中,并且随后使子项目保持最新状态,则可以使用子树合并策略.它是 git 子模块的替代品.
  • 章鱼合并策略可用于合并三个或更多分支.正常的策略只能合并两个分支,如果您尝试合并多个分支,git 会自动回退到章鱼策略.

问题是你只能选择一种策略.但我想将两者结合起来,以获得完整的历史记录,其中整个存储库自动更新到新版本.

The problem is that you can choose only one strategy. But I wanted to combine the two in order to get a clean history in which the whole repository is atomically updated to a new version.

我有一个超级项目,我们称之为projectA,还有一个子项目projectB,我将它合并到projectA 的子目录中.

I have a superproject, let's call it projectA, and a subproject, projectB, that I merged into a subdirectory of projectA.

(那是子树合并部分)

我也在维护一些本地提交.
ProjectA 定期更新,projectB 每隔几天或几周就会有一个新版本,并且通常取决于 projectA 的特定版本.

I'm also maintaining a few local commits.
ProjectA is regularly updated, projectB has a new version every couple days or weeks and usually depends on a particular version of projectA.

当我决定更新两个项目时,我不会简单地从 projectAprojectB 中提取,因为这会为应该是原子的内容创建两次提交整个项目的更新.
相反,我创建了一个合并提交,它结合了 projectAprojectB 和我的本地提交.
这里棘手的部分是,这是一个章鱼合并(三个头),但是projectB需要与子树策略合并.所以这就是我所做的:

When I decide to update both projects, I don't simply pull from projectA and projectB as that would create two commits for what should be an atomic update of the whole project.
Instead, I create a single merge commit which combines projectA, projectB and my local commits.
The tricky part here is that this is an octopus merge (three heads), but projectB needs to be merged with the subtree strategy. So this is what I do:

# Merge projectA with the default strategy:
git merge projectA/master

# Merge projectB with the subtree strategy:
git merge -s subtree projectB/master

这里作者使用了 reset --hard,然后使用 read-tree 来恢复前两次合并对工作树和索引所做的事情,但是reset --soft 可以提供帮助的地方:
如何重做这两次合并,它们已经起作用了,即我的工作树和索引很好,但不必记录这两次提交?

Here the author used a reset --hard, and then read-tree to restore what the first two merges had done to the working tree and index, but that is where reset --soft can help:
How to I redo those two merges, which have worked, i.e. my working tree and index are fine, but without having to record those two commits?

# Move the HEAD, and just the HEAD, two commits back!
git reset --soft HEAD@{2}

现在,我们可以继续 Tomas 的解决方案:

Now, we can resume Tomas's solution:

# Pretend that we just did an octopus merge with three heads:
echo $(git rev-parse projectA/master) > .git/MERGE_HEAD
echo $(git rev-parse projectB/master) >> .git/MERGE_HEAD

# And finally do the commit:
git commit

<小时>

所以,每次:


So, each time:

  • 您对最终结果感到满意(在工作树和索引方面)
  • 你对所有让你达到目标的提交满意:

git reset --soft 就是答案.

这篇关于git reset --soft 的实际用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆