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

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


当前回答

如何编辑上一个承诺

通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,

我发现自己经常去修修过去的东西 以至于我写了剧本

以下是工作流程:

git exent- edit <commit- hash> 这将让您在您想要编辑的承诺时丢弃您。 承诺的更改将会被卸下, 将按您希望的第一次进行, 并准备按您希望的第一次进行。 固定并按您希望的, 并按您希望的原初阶段进行承诺 。 (您可能想要使用 git 隐藏保存 -- kep- index 来抓松任何您没有执行的文件) 重做承诺 -- amend, 例如: git 承诺 -- amend compult the rebase: git rebase -- continue


把这个调用在 Git- commit- edit 之后, 并把它放在您的 $PATH:

#!/bin/bash

# Do an automatic git rebase --interactive, editing the specified commit
# Revert the index and working tree to the point before the commit was staged
# https://stackoverflow.com/a/52324605/5353461

set -euo pipefail

script_name=${0##*/}

warn () { printf '%s: %s\n' "$script_name" "$*" >&2; }
die () { warn "$@"; exit 1; }

[[ $# -ge 2 ]] && die "Expected single commit to edit. Defaults to HEAD~"

# Default to editing the parent of the most recent commit
# The most recent commit can be edited with `git commit --amend`
commit=$(git rev-parse --short "${1:-HEAD~}")

# Be able to show what commit we're editing to the user
if git config --get alias.print-commit-1 &>/dev/null; then
  message=$(git print-commit-1 "$commit")
else
  message=$(git log -1 --format='%h %s' "$commit")
fi

if [[ $OSTYPE =~ ^darwin ]]; then
  sed_inplace=(sed -Ei "")
else
  sed_inplace=(sed -Ei)
fi

export GIT_SEQUENCE_EDITOR="${sed_inplace[*]} "' "s/^pick ('"$commit"' .*)/edit \\1/"'
git rebase --quiet --interactive --autostash --autosquash "$commit"~
git reset --quiet @~ "$(git rev-parse --show-toplevel)"  # Reset the cache of the toplevel directory to the previous commit
git commit --quiet --amend --no-edit --allow-empty  #  Commit an empty commit so that that cache diffs are un-reversed

echo
echo "Editing commit: $message" >&2
echo

其他回答

如何使用,重置 -- 软的或重置 -- 硬的?

@Kyralessa的回答:

如果您不确定该使用什么 -- Soft (我用这个公约来记住它 -- 安全性 -- 软性) 。

为什么?

如果您选择 -- 错误地选择 -- 错误地选择, 您将会失去以前没有的更改。 如果您选择 -- 错误地选择 -- 错误地选择 -- 软, 您可以通过应用额外命令而实现同样的结果 -- 硬

git reset HEAD file.html
git checkout -- file.html

完整示例

echo "some changes..." > file.html
git add file.html
git commit -m "wrong commit"

# I need to reset
git reset --hard HEAD~1 (cancel changes)
# OR
git reset --soft HEAD~1 # Back to staging
git reset HEAD file.html # back to working directory
git checkout -- file.html # cancel changes

感谢@Kyralessa。

更改上次承诺

替换索引中的文件 :

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

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

git commit --amend

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

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

(要改变先前的承诺, 请使用惊人的互动重置 。 )


ProTipTM: 添加 *. class to a gitignore 来阻止这种情况再次发生 。


收回承诺

修改承诺是理想的解决办法,如果您需要更改最后的承诺,但更一般性的解决办法被重置。

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

git reset @~N

N是总部行政领导之前的承付次数, 重划为上一个承付次数。

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

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

检查 git 帮助重置, 特别是 -- -- oft -- 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 reset --hard HEAD~1

取消承诺,但保留您的更改

git reset HEAD~1

保存您的文件, 并自动将所有更改都放回后方

git reset --soft HEAD~1

使你们复活,而你们被毁灭;

git reflog #to find the sh

只要使用 git 重置它执行下面的命令 :

git reset --soft HEAD~1

解释 : git 重设做什么, 它基本上是重置到任何您想要返回的承诺, 然后如果它与 -- soft 键合并, 它会返回, 但保留您文件中的更改, 所以您回到刚刚添加文件的阶段, HEAD 是分支的头部, 如果您和 ~ 1 合并( 在此情况下, 您也使用 HEAD+) , 它只会返回一个您想要的...

我为您创建了以下图像中的步骤, 详情更多, 包括所有在真实情况下可能发生的步骤, 并承诺执行代码 :

如果您想要永久撤销它, 您克隆了一些仓库 。

犯罪身份可通过以下方式识别:

git log 

然后你们可以做:

git reset --hard <commit_id>

git push origin <branch_name> -f