什么时候使用 git reset --soft?

When do you use git reset --soft?

什么时候使用git reset --soft?我一直使用 git reset --hard 但似乎从来没有找到使用 git reset --soft.

的情况

哎呀,我刚才不是故意的

git reset --soft HEAD^

(我倾向于让它默认为 --mixed,但它非常接近。)

一方面,它可以让您将提交压缩在一起。让我们在处理某事时进行三个临时提交:

... change files ...
git commit -m 'Temporary 1'
... change files ...
git commit -m 'Temporary 2'
... change files ...
git commit -m 'Temporary 3'

这给了我们这样的历史:

A ---> T1 ---> T2 ---> T3
                       ^master

好的,现在这些更改已准备就绪,但我想将它们压缩到一个提交中,因为它们包含一个逻辑更改。

git reset --soft HEAD~3

这给了我们这样的历史:

A ---> T1 ---> T2 ---> T3
^master

但是索引(要提交的更改)包含 T3 中的所有内容。请注意,T1-T3 是孤立的。

git commit -m 'Full commit message'

现在我们有这个:

A ---> T1 ---> T2 ---> T3
  \
   --> B
       ^master

B和T3内容相同,历史不同

修正

git commit --amend可以改写成git reset,至少在简单的情况下:

git commit --amend

通常与...相同

git reset --soft 'HEAD^'
git commit

虽然多个开发人员同时在同一个 git 存储库上工作,但当有人要求您审查他们的 PR(拉取请求)时,这是很常见的。

这种情况经常发生在我身上。当我有未提交的 WIP(进行中的工作)时,不得不从我的功能分支切换到 PR 分支曾经有点痛苦。

以前用git stash,现在用git reset --soft

这是当有人要求我审查他们的 PR 时流程的样子

git commit -m 'WIP whatever' # instead of git stash
git checkout develop 
git pull
git checkout pr-branch

然后,在GitHub中审核合并后…

git checkout my-branch
git rebase develop # to sync my feature branch
git log # to see which commit hash was the one from the PR
git reset --soft 2cf4332  # resets from my WIP commits 
# to the PR commit.

我写了整篇博客 post 关于这个主题。

https://medium.com/@Cuadraman/how-to-deal-with-github-prs-while-working-on-your-feature-branch-f56ef829efcd#.kxnabannq