【问题标题】:When do you use git reset --soft?你什么时候使用 git reset --soft?
【发布时间】:2015-07-31 22:07:46
【问题描述】:

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

【问题讨论】:

    标签: git git-reset


    【解决方案1】:

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

    ... 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
    

    【讨论】:

    • 你的意思是 --amend 不是 --append :-)
    【解决方案2】:

    糟糕,我刚才不是故意的。

    git reset --soft HEAD^
    

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

    【讨论】:

      【解决方案3】:

      虽然多个开发人员同时在同一个 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.
      

      我写了一篇关于这个主题的完整博客文章。

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

      【讨论】:

        猜你喜欢
        • 2020-06-22
        • 2013-07-19
        • 2019-08-30
        • 2014-08-25
        • 1970-01-01
        • 2011-04-01
        相关资源
        最近更新 更多