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

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


当前回答

当地承诺

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 手册页面 。

其他回答

取决于您是否已经公开了您的前一次承诺( 插入到您的远程仓库 ) :

如何撤销本地任务

比方说我承诺了当地, 但现在我想取消那个承诺。

git log
    commit 101: bad commit    # Latest commit. This would be called 'HEAD'.
    commit 100: good commit   # Second to last commit. This is the one we want.

要让一切恢复到上次承诺之前的状态 我们需要重置承诺 重置承诺

git reset --soft HEAD^     # Use --soft if you want to keep your changes
git reset --hard HEAD^     # Use --hard if you don't care about keeping the changes you made

现在 git 日志将显示我们最后的承诺已被删除 。

如何撤销公开承诺

如果您已经公开了承诺, 您将会想要创建一个新的承诺, 它将“ 撤销” 您对上一个承诺( 当前 HEAD) 所做的更改 。

git revert HEAD

你们的更改将恢复,并准备好你们承诺:

git commit -m 'restoring the file I removed by accident'
git log
    commit 102: restoring the file I removed by accident
    commit 101: removing a file we don't need
    commit 100: adding a file that we need

更多信息,请查看 Git Basics - 撤销一些事情 。

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

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 校验结果

在我的情况中,我承诺并推向错误的分支, 所以我想要的是把全部的改变都收回来, 这样我就可以把它们投向一个新的正确的分支, 所以我做了这个:

在您承诺和推动的同一分支上, 如果您输入“ gitt status” 键入“ git status” 状态, 您将不会看到任何新事物, 因为您承诺和推动, 现在键入 :

    git reset --soft HEAD~1

这将将您所有的更改( 文件) 重新回到舞台区域, 现在把它们重新回到工作目录( 非阶段) 中, 您只要输入 :

git reset FILE

“ 文件” 是您想要再次承诺的文件 。 现在, FILE 应该在工作目录中, 包含您所做的全部更改 。 现在您可以更改为您想要的分支, 并对该分支进行更改 。 当然, 您承诺的初始分支仍然有所有更改, 但对于我来说, 如果不是您的话, 您可以寻找恢复该分支承诺的方法 。

每次我需要撤销承诺/承诺时,我要做的是:

git 重置 HEADn > // 上次承诺的数量, 我需要撤销 Git 状态 / / 选项 。 所有文件现在都是红色的( 未进入阶段 ) 。 现在, 我可以添加并只承诺需要的文件 :

git 添加 < file name> & git 承诺 - m “ 消息” - m “ 细节”

可以选择:我可以将其它文件的更改(如果需要的话) 推回到它们以前的状态, 并进行退票:

git 检出 < filename>

如果我已经把它推向了遥远的起源, 之前:

git 推源 <branch name> -f // use -f 强制推 。

git push --delete (branch_name) //this will be removing the public version of your branch

git push origin (branch_name) //This will add the previous version back