【问题标题】:Is it possible to git --fixup on master是否可以在 master 上 git --fixup
【发布时间】:2017-03-18 16:51:18
【问题描述】:

我正在寻找一种将 master 上的所有提交合并到一个提交并最终重写一个新提交以从新开始的方法。

【问题讨论】:

    标签: git


    【解决方案1】:

    如果您想保留现有的存储库配置,您可以执行以下操作:

    • git checkout master
    • git branch backup:如果您想保留历史记录,可以选择创建一个备份分支。
    • git reset --soft $SHA_OF_INIT_COMMIT:这将更新 HEAD 所指向的内容,但将您的索引和工作目录保持在当前状态。
    • git commit --amend:将您的初始提交更改为指向您的 repo 的当前状态。
    • git push --force:强制推送到origin/master 以更新远程存储库(如果有的话)。

    我刚试过这个,它就像一个魅力。这归功于Kevin M Granger。在此之后,您可能还想在本地和远程存储库上运行git gc(同样,如果有的话)。 git gc 在当前存储库中运行许多内务任务,例如压缩文件修订(以减少磁盘空间并提高性能)和删除无法访问的对象。

    【讨论】:

      【解决方案2】:

      您可以删除您的 .git 文件夹,并在新的 Git 存储库中使用当前源作为“初始提交”。

      【讨论】:

      • 遗憾的是我需要保留这个 repo。
      • 据我了解,您说的是彻底改变master的历史。 “保留这个 repo”是怎么回事?
      • 保留 repo 的名称,但也许我应该删除 .git、repo 并重做一个。
      【解决方案3】:
          Use following Commands and Steps: 
      
          1) git rebase -i HEAD~2 origin/master    #2 is no of commits you want to fix
      
          Following window will open up
          pick 03d633e refactor
          pick d51b770 Updating Username comment
      
          # Rebase 2868146..d51b770 onto 2868146 (2 commands)
          #
          # Commands:
          # p, pick = use commit
          # r, reword = use commit, but edit the commit message
          # e, edit = use commit, but stop for amending
          # s, squash = use commit, but meld into previous commit
          # f, fixup = like "squash", but discard this commit's log message
          # x, exec = run command (the rest of the line) using shell
          # d, drop = remove commit
          #
          # These lines can be re-ordered; they are executed from top to bottom.
          #
          # If you remove a line here THAT COMMIT WILL BE LOST.
          #
          # However, if you remove everything, the rebase will be aborted.
          #
          # Note that empty commits are commented out
          "~/WebstormProjects/Working/.git/rebase-merge/git-rebase-todo" 21L, 700C
      
      2) To fixup : change pick to "fixup" or "f" and Save file .
      
           pick 03d633e refactor
           fixup  d51b770 Updating Username comment
      
      3) Verify same using command:
           git log --oneline
      
      4)force push changes :(Be Careful)
           git push origin HEAD:master --force
      

      【讨论】:

        猜你喜欢
        • 2022-07-22
        • 2022-08-17
        • 2022-07-19
        • 2017-10-19
        • 2011-04-15
        • 2011-10-13
        • 2014-01-18
        • 1970-01-01
        • 2018-04-07
        相关资源
        最近更新 更多