我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。

我如何解除那些承诺?当地当地仓库?


当前回答

在回答之前,我们补充一些背景背景,解释一下HEAD是。 这是。

First of all what is HEAD?

HEAD仅指当前分支的当前承诺(最新承诺)。
只有一个,只有一个,只有一个HEAD在任何给定时间(不包括git worktree).

内容的内容HEAD存储在内部.git/HEAD并包含当前承诺的 40 字节 SHA-1 。


detached HEAD

如果你们不履行最近的承诺,那末,HEAD指向历史中的先前承诺 它被称作detached HEAD.

Enter image description here

在命令行上,它会看起来像这个 - SHA-1 而不是从HEAD不指向当前分支的端点 :

Enter image description here

Enter image description here


如何从独立的总部中恢复的几种选择:


git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to go back

这将检查指向想要的承诺的新分支 。
此命令将检出给给定的承诺 。
此时此刻,您可以创建一个分支,并从此开始工作。

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>

git reflog

你可以随时使用reflog并且,还有。
git reflog 将显示更新HEAD并检查想要的 reflog 条目将设置HEAD返回到此任务。

总部总部每次修改时,将有一个新的条目。reflog

git reflog
git checkout HEAD@{...}

这样你就可以回到你想要的事业了

Enter image description here


git reset --hard <commit_id>

"移动"你的总部 回到想要的承诺。

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
  • 注:(a)自Git 2.7以来)您也可以使用git rebase --no-autostash并且,还有。

enter image description here


git revert <sha-1>

“ 撤消” 指定的承诺或承诺范围 。
重置命令将“ 撤消” 对给定任务所做的任何更改 。
将使用解析补丁进行新承诺, 而原始承诺也将保留在历史中 。

# Add a new commit with the undo of the original one.
# The <sha-1> can be any commit(s) or commit range
git revert <sha-1>

这个计划说明哪个指挥所做什么。
如你所见reset && checkout修改HEAD.

Enter image description here

其他回答

您可以从本地仓库撤销您的承诺 。 请遵循以下方案 。

在下面的图像中, 我检查“ 测试” 分支( 使用 Git 命令)git checkout -b test)作为本地检查状态(使用 Git 命令)git status地方分支中没有任何可承诺的。

Enter image description here

在下一个图像图像中,您可以看到这里的图像 我做了一些修改过滤器1. txt然后用一些信息(使用 Git 命令) 将文件添加到中继区, 并用一些信息( 使用 Git 命令) 来进行更改git commit -m "Doing commit to test revert back").

"-m 用于发送信件"

Enter image description here

在下一个图像中,您可以看到您承诺的日志,无论您承诺什么(使用 Git 命令)git log).

Enter image description here

所以在以上图像中,您可以看到每个承诺的承付代号, 以及您现在的执信信息, 无论您想要返回或撤消的复制件, 即实施 ID 并击中下面的 Git 命令,git revert {"paste your commit id"}示例:

git revert 9ca304ed12b991f8251496b4ea452857b34353e7

Enter image description here

我恢复了上次的承诺。 现在, 如果您检查您的 Git 状态, 您可以看到修改后的文件过滤器1. txt并且尚未承诺。

Enter image description here

通常情况下,你想要取消对因为您犯了一个错误,所以您想要修正它 基本上就是OP在问问题时所做的。真的,您真的想要复复做a 承诺。

这里的大多数答案都集中在命令行上。 虽然命令行是使用Git的最佳方法, 当你对它感到舒服的时候, 它可能与来自其他版本控制系统到 Git 的人有点不同。

使用 GUI 如何操作。 如果您安装了 Git, 您已经拥有了所有您需要遵守的指示 。

注:我在这里假设您在推动前意识到承诺是错误的。 如果您不知道推力意味着什么, 那么您可能还没有推动。 继续执行指令 。 如果您推动错误承诺, 最危险的方法就是跟踪错误承诺, 并做出新的承诺, 来修补事情, 您在版本控制系统中这样做的方式不允许您重写历史 。

使用图形用户界面来修正你最近的过错:

  1. 导航到命令行的仓库,并用git gui
  2. 选择“ 修正上一个任务 ” 。 您将会看到您上次的委托信件、 您所准备的文件 以及您没有的文件 。
  3. 现在将事情更改为您想要它们查看的方式, 并单击 CON 。

主要设想有两种情况:

你还没推动承诺呢

如果问题在于您所做的额外文件( 而您不希望存储库中的文件), 您可以使用git rm并且实践,--amend

git rm <pathToFile>

您也可以以下列方式删除整个目录-r,或甚至与其他巴什命令命令

git rm -r <pathToDirectory>
git rm $(find -name '*.class')

删除文件后, 您可以执行-- amend -- 修正 -- 修正选项选项选项

git commit --amend -C HEAD # the -C option is to use the same commit message

这将重写您最近的本地承诺删除额外文件, 因此, 这些文件将不会被按键发送, 并且 GC 将会从您本地的. git 仓库中删除 。

您已经推进了任务

您可以对其它情景应用相同的解决方案,然后做git push和和-f选项选项,但它是未建议因为它以不同的变化(它会破坏你的仓库) 推翻了遥远的历史。

相反,你必须做承诺--amend(记住关于 - amend : 该选项重写上次承诺的历史) 。

更改上次承诺

替换索引中的文件 :

git rm --cached *.class
git add *.java

那么,如果它是私人分支,修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正修正承诺:

git commit --amend

或者,如果它是共享分支, 做出新的承诺:

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

(更改上一个承诺,使用真棒交互式重新基数.)


ProTipTM: 添加*.class至 a高光度( gitignore)阻止这种情况再次发生。


收回承诺

修改承诺是理想的解决办法,如果需要改变最后的承诺,但更普遍的解决办法是:reset.

您可以将 Git 重置为任何承诺 :

git reset @~N

何处处N之前承诺的数HEAD, 和@~重发到上一个任务 。

您不必修改承诺,而是可以使用:

git reset @~
git add *.java
git commit -m "Add .java files"

退 退 退git help reset,特别是:--soft --mixed--hard为了更好地了解它的作用

reflog 格式

如果你搞砸了,你总是可以使用折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式折叠式

$ git reset @~
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~
2c52489 HEAD@{1}: commit: added some .class files
$ git reset 2c52489
... and you're back where you started

类型类型类型git log并发现最后的散列代码,然后输入:

git reset <the previous co>