我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。

我如何解除那些承诺?当地当地仓库?


当前回答

您可以使用git revert <commit-id>.

为了得到身份证明 只需使用git log.

其他回答

git reset --soft HEAD~1

软性 : 这将只从本地端删除承诺, 并保留文件所做的更改 。

    git reset --hard HEAD~1
    git push origin master

硬性 : 这将从本地和远程删除承诺, 并删除文件中所做的更改 。

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

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

  1. 删除本地仓库 。
  2. 克隆远程仓库 。

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

别忘了重新承诺保存已保存上次按键后的变化 。

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

git reset --soft <your_last_good_commit_hash_here>来,如果你来git status中,您会看到中转区的文件。您可以选择错误的文件,然后从中转区取下来。

喜欢下面。

git reset wrongFile1 wrongFile2 wrongFile3

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

git add goodFile1 goodFile2

提交提交它们

git commit -vgit commit -am "Message"

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

git push origin master

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

git reset --hard <your_last_good_commit_hash_here>

git push origin master

如果您已经向服务器发布错误的文件, 您可以使用--force挂号以向服务器推动并编辑历史。

git push --force origin master

在这种情况下,"重置"命令就是你最好的朋友:

git reset --soft HEAD~1

重置将会将您当前的 HEAD 分支倒带到指定的修改 。 在以上的例子中, 我们希望回到当前修改之前的修改 - 有效地取消我们最后的承诺 。

注意 -- soft 旗号 : 这样可以确保未撤销修改的更改被保存。 在运行命令后, 您会发现您的工作副本中这些更改是未承诺的本地修改 。

如果您不想保留这些更改, 请使用 -- 硬旗 。 确定只有在您确定不再需要这些更改时才这样做 。

git reset --hard HEAD~1

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

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

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

以下是工作流程:

  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