【问题标题】:How to undo a commit in freshly initialized repository (not pushed) [duplicate]如何撤消新初始化存储库中的提交(未推送)[重复]
【发布时间】:2019-07-24 17:28:45
【问题描述】:

我初始化了一个 repo,但现在想undo 第一个(也是唯一的)提交。

这些three solutions 用于撤消提交所有错误,因为freshly created repo points to a ref that doesn't exist yet

所以我的问题是如何撤消尚未推送且尚未获得 ref 的 repo 中的提交?

注意:我也试过git commit -amend没有成功

【问题讨论】:

    标签: git


    【解决方案1】:

    注意:从技术上讲,这不会撤消提交,但它同样好——空的存储库几乎没有功能。要真正清空存储库,请参见下文。

    git commit --amend 应该可以工作。这是一个演示:

    $ git init
    Initialized empty Git repository in .../.git/
    $ echo test changing root commit > README
    $ git add README 
    $ git commit -m initial
    [master (root-commit) 90e83ae] initial
     1 file changed, 1 insertion(+)
     create mode 100644 README
    $ echo different readme > README
    $ git add README 
    $ git commit --amend --no-edit
    [master 8e159b1] initial
     Date: Sat Mar 2 21:00:53 2019 -0800
     1 file changed, 1 insertion(+)
     create mode 100644 README
    $ git log --all --decorate --oneline --graph
    * 8e159b1 (HEAD -> master) initial
    

    请注意,“不同的 README”是这个新根提交的内容:

    $ git show --pretty=oneline
    8e159b1f0d397b31cb1be5168e77ba200269c62a (HEAD -> master) initial
    diff --git a/README b/README
    new file mode 100644
    index 0000000..ef0411a
    --- /dev/null
    +++ b/README
    @@ -0,0 +1 @@
    +different readme
    

    如果您在新的根提交中有不想要的文件,请使用git rm(可能使用--cached)删除它们。


    如果您真的想删除 master 分支及其单个提交,然后像通常在一个新的空存储库中一样位于 master 分支上,则需要几个额外的步骤,至少只要您想使用正常(而不是管道)命令。步骤如下:

    1. git branch -m master delete:重命名 master 分支
    2. git checkout --orphan master:返回一个名为 master 的不存在的分支
    3. git branch -D delete:删除不需要的分支
    4. git read-tree --empty:清空当前索引

    工作树不受此过程的干扰。

    【讨论】:

      【解决方案2】:

      如果您只想删除一个提交,您可以删除记录提交日志 ID 的 .git/logs/refs/heads/master 文件。同时删除记录master 分支提交的文件.git/refs/heads/master。 您可能还想删除存储最后提交 ID 的 .git/logs/HEAD 文件。 删除这些引用后,这些对象最终将被垃圾回收。

      但是,删除整个.git 目录和再次删除git init 可能更容易、更快捷。

      【讨论】:

        猜你喜欢
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 2018-03-14
        • 2014-05-06
        • 2023-04-09
        • 2013-05-25
        • 2021-01-03
        • 1970-01-01
        相关资源
        最近更新 更多