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

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


当前回答

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

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

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

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

git 检出 < filename>

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

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

其他回答

git diff + git 应用程序是迄今为止我团队的最好选择, 清洁和非常容易, 不仅对最后一次任务来说

我想撤销我们共享存储库中最新的五个承诺。 我查了我想回溯到的订正代号 。 然后我输入了下面的内容 。

prompt> git reset --hard 5a7404742c85
HEAD is now at 5a74047 Added one more page to catalogue
prompt> git push origin master --force
Total 0 (delta 0), reused 0 (delta 0)
remote: bb/acl: neoneye is allowed. accepted payload.
To git@bitbucket.org:thecompany/prometheus.git
 + 09a6480...5a74047 master -> master (forced update)
prompt>

为了完整起见,我将提出前几个答复所忽略的一个明显明显的方法。

由于承诺没有被推,遥控器没有改变,因此:

删除本地仓库。 克隆远程仓库 。

有时候,如果你的高贵的Git客户告别(看着你,伊吉特),这有时是必要的。

别忘了重新承诺上次推后保存的更改 。

您可以以两种方式撤销您的 Git 承诺 : 首先, 您可以使用 Git 返回, 如果您想要保留您的承诺历史 :

git revert HEAD~3
git revert <hashcode of commit>

第二是您可以使用 Git 重置, 这将删除您全部的委托历史, 并随心所欲地将您的头移到您想要的地方 。

git reset <hashcode of commit>
git reset HEAD~3

您也可以使用 -- hard 关键词, 如果有的话, 如果它开始有其他行为的话。 但是, 我建议在非常必要的时候使用它。

如何编辑上一个承诺

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

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

以下是工作流程:

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