【问题标题】:Changing an old commit in Git [duplicate]更改 Git 中的旧提交 [重复]
【发布时间】:2018-12-30 22:31:02
【问题描述】:

所以,我从 master 分支了我的项目,并对新分支进行了 5 次提交。现在,我发现我的第一次提交很有趣:我在一些我不应该有的文件中添加了一些更改。我怎样才能在不搞砸整个事情的情况下纠正这个错误?

更新:抱歉,我解释错了,因为英语不是我的母语。打开 Unity 编辑器时,某些文件会自动更改。我也不小心提交了这些文件。我不想要那个。我宁愿摆脱这些更改(使用clean?)并提交我实际所做的更改。

【问题讨论】:

    标签: git unity3d


    【解决方案1】:

    简答:你不能!

    长答案:也许你可以!

    git 中的每个提交都依赖于以前的提交,并且所有提交都在一个链中。所以你不能在不更改所有以后的提交的情况下更改提交。 (每个提交都有一个 hash id,它依赖于之前的提交)

    因此,这样做的唯一方法是将所有提交从上次提交 (HEAD) 更改为该提交。假设我们有 3 个提交(commit1、commit2、commit3 == HEAD)并且您想要更改 commit1。

    你必须这样做:

    git reset HEAD~1  // Going back one commit (now HEAD is commit2)
    git stash // to keep this commit's changes in stash area
    git reset HEAD~1 // Going back one commit (now HEAD is commit1)
    (Now doing your changes in commit1)
    git add [changed_files]
    git commit --amend // Changing first commit
    git stash pop // inserting back changes of commit2
    git commit -am 'new commit message for commit2'
    git stash pop // inserting back changes of commit3
    git commit -am 'new commit message for commit3'
    

    【讨论】:

      猜你喜欢
      • 2014-07-12
      • 2021-02-06
      • 1970-01-01
      • 2020-07-30
      • 2016-06-23
      • 2021-02-15
      • 2012-02-08
      • 2014-07-13
      • 2010-12-25
      相关资源
      最近更新 更多