我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。

我如何从当地存储库撤销这些承诺?


当前回答

我验证了所提议的有效方法,这里就是使用这一方法的一个具体例子:

如果您想要永久撤销/取消您的最后一项承诺(等等, 一个一个一个, 尽可能多) , 有三个步骤 :

1: 获取您想要到达的承诺的 id = SHA, 当然

$ git log

2: 删除您先前的承诺

$ git reset --hard 'your SHA'

3: 强制使用 - f 选项( 最后一个承诺轨道将从 GitHub 历史中删除) 强制使用 GitHub 的本地新历史 。

$ git push origin master -f

示例示例示例示例

$ git log

最后一次承诺取消

commit e305d21bdcdc51d623faec631ced72645cca9131 (HEAD -> master, origin/master, origin/HEAD)
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 03:42:26 2020 +0200

U2_30 S45; updating files package.json & yarn.lock for GitHub Web Page from docs/CV_Portfolio...

现在就向总部提交我们想要的文件

commit 36212a48b0123456789e01a6c174103be9a11e61
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 02:38:01 2020 +0200

First commit, new title

删除最后一项, 达到承诺前, 删除最后一项

$ git reset --hard 36212a4

HEAD is now at 36212a4 First commit, new title

检查一下,没事的

$ git log

commit 36212a48b0123456789e01a6c174103be9a11e61 (HEAD -> master)
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 02:38:01 2020 +0200

    First commit, new title

$ git status

On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
 (use "git pull" to update your local branch)

nothing to commit, working tree clean

更新 Git (Hub) 上的历史

$ git push origin master -f

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ GitUser bla bla/React-Apps.git
 + e305d21...36212a4 master -> master (forced update)

检查一下,没事的

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

其他回答

你需要做简单快的动作

    git commit --amend

是私人分行还是私人分行

    git commit -m 'Replace .class files with .java files'

如果它是共享的或公共的分支。

添加/删除文件, 以便按照您想要的方式获取文件 :

git rm classdir
git add sourcedir

然后修正承诺:

git commit --amend

之前的错误承诺将会被编辑 以反映新的指数状态 换句话说,这就像你从未犯过错误一样

请注意,你只应该在还没有推的时候做这个。如果你已经推了,那么你只需要正常地做一个修正。

当地承诺

git reset --soft HEAD~1

或者如果你们记不起来,那末,你们可以使用

git rm --cached <file>

用于被推动的 承诺

从存储库历史中删除文件的适当方式是使用 git 过滤器- blanch 。 也就是说,

git filter-branch --index-filter 'git rm --cached <file>' HEAD

但我建议您谨慎使用此命令 。 更多信息请访问 git- filter- branch- blanch- 1 手册页面 。

撤消一次承诺重做( R)

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)

git 重新设置是撤销命令的责任。 它会解除您最后的承诺, 同时将您的工作树( 您在磁盘上的文件状态) 保留下来。 您需要重新添加它们才能再次执行 。 对工作树文件进行更正 。 git 添加您想要包含在您新承诺中的任何内容 。 提交修改, 重用旧承诺信件 。 重置旧头部复制到. git/ ORIG_ HEAD; 与 - c ORIG_ HEAD 承诺将打开编辑器, 该编辑器最初包含旧承诺的日志信息, 并允许您编辑它 。 如果您不需要编辑信件, 您可以使用 - C 选项 。

或者,为了编辑上一个承诺(或仅仅是其承诺信息),承诺 -- -- 修正将在当前索引中将修改添加到上一个承诺。

要删除( 不回覆) 被推进到服务器的承诺, 有必要重写历史, 使用 Git 推源主 -- force [- with- level] 重写历史。 使用 -- force; 偏好 -- force- with- level, 几乎总是一个坏主意。 正如 Git 手册中所指出的 :

如果你[重写历史]已经发表,你应该了解重写历史的意义。


继续阅读

您可以使用 git reflog 来确定您想要返回的 SHA-1 承诺的 SHA-1 。 一旦您拥有此值, 请使用上文解释的命令序列 。


HEAD~ 和 HEAD~ 1 相同。 如果您想要解开多次承诺, 文章“ HEAD ” 是否有帮助 。

在我的案子中,我无意中犯了一些我不想犯的案卷。所以我做了以下的,它奏效了:

git reset --soft HEAD^
git rm --cached [files you do not need]
git add [files you need]
git commit -c ORIG_HEAD

用 gitk 或 git 日志 -- stat 校验结果