我无意间把错误的档案 写错给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>

其他回答

视觉演播室代码使得这很容易。

VS Code

要重置上一个修订版, 请永久删除所有未承诺的更改 :

git reset --hard HEAD~1

我更喜欢用git rebase -i对于这份工作来说, 因为一份不错的清单 出现在我可以选择摆脱承诺的地方。 它可能不如这里的其他答案直接直接, 但是它只是...感觉右右.

选择要列出多少个承诺, 然后这样引用( 加入最后 3 个)

git rebase -i HEAD~3

样本列表

pick aa28ba7 Sanity check for RtmpSrv port
pick c26c541 RtmpSrv version option
pick 58d6909 Better URL decoding support

然后 Git 将删除任何您删除的线条 。

撤消一次承诺重做( 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)
  1. git reset是负责取消对。它会解除您最后的承诺,而您的工作树( 您在磁盘上的文件状态) 尚未被移动 。在你再次犯下罪行之前,你需要再加一次)。
  2. 作出以下更正工作树文件。
  3. git add任何您想要包含在您新承诺中的内容 。
  4. 提交修改,重复使用旧的已发信件 。reset复制旧头头到.git/ORIG_HEAD; commit-c ORIG_HEAD将打开编辑器,该编辑器最初包含旧承诺的日志消息,并允许您编辑它。如果不需要编辑该消息,您可以使用该-C选项。

或者编辑上一个承诺( 或仅其承诺信件), commit --amend将在当前索引内对上一个承诺添加变化。

要删除( 不再返回) 被推到服务器的承付重写历史git push origin main --force[-with-lease]这是必要的。这是几乎几乎总是使用一个坏主意--force; 偏; 更优--force-with-lease改为,如以下所示:Git 手册:

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


继续阅读

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


HEAD~HEAD~1该条什么是头在Git?如果您想要解开多项承诺, 将会很有帮助 。

如何编辑编辑编辑编辑先前犯过

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

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

以下是工作流程:

  1. git commit-edit <commit-hash>
    

    这将让您在您想要编辑的承诺上掉下来 。

    承诺的更改将是un un准备准备上场 如你所愿 这是第一次

  2. 并按你所希望的那样 确定并完成承诺

    (您可能想要使用)git stash save --keep-index来解开任何您没有做的文件)

  3. 重做承诺--amend, 例如 :

    git commit --amend
    
  4. 完成重定基数 :

    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