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

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


当前回答

认为我们已经有了 code. txt 文件。 我们对其做了一些修改并承诺。 我们可以以三种方式撤销此承诺, 但首先, 您应该知道什么是预置文件... 一个预设文件是一个可以承诺的文件。 如果您运行 git 状态, 此文件将会显示为绿色颜色, 如果它没有被预设用于承诺, 将会显示为红色颜色 :

意思是如果您承诺更改, 此文件中的更改没有被保存 。 您可以在您的舞台上添加此文件, 添加 code. txt , 然后进行更改 :

撤消上次承诺 :

现在,如果我们想要在不做其他任何更改的情况下撤销承诺, 我们可以使用 git 重设 -- soft HEAD * 如果我们想要撤销承诺及其更改( THIS 是危险的, 因为您的更改将会丢失) , 我们可以使用 git 重置 -- 硬 HEAD * 如果我们想要撤销承诺并删除舞台上的更改, 我们可以使用 git 重置 -- mixed HEAD* 或短格式的 git 重置 HEAD***

其他回答

如何编辑上一个承诺

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

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

以下是工作流程:

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

如果您想要删除错误的文件, 您应该做

git 重置 -- s软 < your_ last_ good_commit_hash_ here> 在这里, 如果您做 Git 状态, 您可以在集结区看到文件。 您可以选择错误的文件, 从集结区拿下来 。

喜欢下面。

git 重置错误的File1 错误的File2 错误的File3

您现在可以添加您需要按键的文件,

添加好文件1 好文件2 git 添加好文件1 goodFile2

提交提交它们

git 承诺 -v 或 git 承诺 - am "Message"

推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推,推

git 推进源源主机

但是,如果您不关心已更改的文件, 您可以硬重置到先前的良好承诺, 并将所有文件都推到服务器 。

git reset --hard <your_last_good_commit_hash_here>

git 推进源源主机

如果您已经向服务器发布错误的文件, 您可以使用 -- 强制旗将文件推到服务器并编辑历史 。

git 推推 -- 原力硕士

(2015年等)

如果你在视觉工作室无法同步, 因为不允许你按到像“开发”那样的分支, 那么就像我努力的一样, 在视觉工作室NEITHER, REWET NOR(硬的或软的)RESET(硬的或软的)将有效。

答案是用数字标注的:

使用此命令命令 根 你的项目根 核武器 任何试图 被推推:

git reset --hard HEAD~1

备份或拉链你的文件 只是以防你不想失去任何工作,等等...

撤消上次承诺 :

git 重设 -- s软 HEADQ 或 git 重置 -- s软 HEAD~

这将撤销最后一项承诺 。

在这里 -- 软意味着重置中。

HEAD~ 或 HEAD_ 意思是移动 在 HEAD 之前承诺。


替换上次承诺的新承诺 :

git commit --amend -m "message"

它将以新承诺取代最后一项承诺 。

假设您是在视觉工作室工作, 如果您进入您的分支历史, 并查看您的所有承诺, 只需在承诺之前选择您想要撤销的事件, 右键单击它, 并选择回溯。 简单 。