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

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


当前回答

您可以使用您不想播放的文件重置不想要的文件命令。 通过使用此命令, 我们可以将更改的文件从中位区域移到工作目录 。

我如何在本地及远程解析 Git 承诺?

其他回答

有许多方法可以做到:

撤消上次执行/ 上一个承诺的 Git 命令 :

警告: 如果您不知道自己在做什么, 不要使用 -- 硬。 -- 硬太危险, 它可能会删除您的文件 。

返回 Git 中的承诺的基本命令是 :

$ git reset --hard <COMMIT -ID>

$ git reset --hard HEAD~<n>

承诺的代号:

n: 是您想要返回的最后一次承诺的数

您可以获取下列承诺代号:

$ **git log --oneline**

d81d3f1 function to subtract two numbers

be20eb8 function to add two numbers

bedgfgg function to multiply two numbers

d81d3f1 和be20eb8 是实施代号的 。

现在,让我们来看看一些案例:

假设您想要返回上次的“ d81d3f1 ” 。 这里有两个选项 :

$ git reset --hard d81d3f1

$ git reset --hard HEAD~1

假设您想要还原“ ebe20eb8” 承诺 :

$ git reset --hard be20eb8

欲了解更详细的信息,请参考并尝试其他命令,将头重置为指定状态:

$ git reset --help

你需要做简单快的动作

    git commit --amend

是私人分行还是私人分行

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

如果它是共享的或公共的分支。

使用 reflog 查找正确状态

git reflog

安全前的路线

选择正确的 reflog( 以我为例, f3cb6e2) 和类型

git reset --hard f3cb6e2

在那之后 重置总部将重置 重置后,HEADLOGED

最终, 折叠图看起来像下面的图片

谈判最后定本

如何编辑上一个承诺

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

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

以下是工作流程:

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 reset --hard <commit_hash>

确保您有备份的更改, 以防万一, 因为它是一个困难的重置, 这意味着它们会丢失( 除非您更早隐藏) 。