Git 只保留1条 commit

如果需要 Git 仓库只保留 1 条 commit,您可以:

  1. 首先,使用命令 git checkout--orphan 选项创建一个空白分支(本文为 temporary):
    git checkout --orphan temporary
    
  2. 添加当前目录下所有文件:
    git add .
    
  3. 提交 commit 到 Git 仓库(本文的 commit 信息为 Initial commit):
    git commit -m "Initial commit"
    
  4. 删除只保留 1 条 commit的分支(本文为 main):
    git branch -d main
    
  5. 重命名当前分支为只保留 1 条 commit的分支(本文为 main):
    git branch -m main
    
  6. 最后,强制推送到 Git 仓库:
    git push -f
    

    完成!

相关文章:

参考:GIT 只保留最后一次提交代码 | 修心修己

Built with Hugo
主题 StackJimmy 设计