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

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


当前回答

在回答之前,让我们补充一些背景,解释一下这个总部是什么。

首先,什么是总部?

HEAD 只是引用当前分支的当前承诺( 最新承诺) 。 在任何特定时间( 不包括 git worktree) 只能有一个 HEAD 。

HEAD的内容存放在.git/HEAD内,包含当前承诺的40字节SHA-1。


独立头部

如果您没有在最新的承诺 - 意思是,HEAD指向 历史的先前承诺, 它被称为独立的HEAD。

在命令行上, 它会看起来像这个 - SHA-1 而不是分支名称, 因为 HEAD 没有指向当前分支的端点 :


如何从独立的总部中恢复的几种选择:


git 检出

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to go back

这将检查指向想要的承诺的新分支 。 此命令将检查退出给定的承诺 。 在此点上, 您可以创建一个分支, 从此开始工作 。

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>

git 重新格式

您也可以同时使用reflog。 git reflog 将显示任何更新 HEAD 的更改, 并检查想要的 reflog 条目, 将会将 HEAD 设置回至此任务 。

每次对总部领导部进行修改时,在重新格式中将有一个新的条目

git reflog
git checkout HEAD@{...}

这样你就可以回到你想要的事业了


git 重置 - 硬设置 < commit_ id>

"移动"你的总部 回到想要的承诺。

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.

注: (自Git 2. 7) 您也可以使用 Git rebase -- no- autostash 。


git 返回 <sha-1>

“ 撤消” 指定的承诺或承诺范围。 重置命令将“ 撤消” 对给定承诺所做的任何更改 。 在原始承诺也将保留在历史中时, 将使用撤消补丁的新承诺 。

# Add a new commit with the undo of the original one.
# The <sha-1> can be any commit(s) or commit range
git revert <sha-1>

此图示显示哪个命令做什么。 正如您可以看到的, 重置 {} 检查退出 修改 HEAD 。

其他回答

另一种方式:

检查您想要返回的分支, 然后将本地工作副本重置到您想要在远程服务器上成为最新一份的承诺( 它将在离别后进行的一切) 。 要做到这一点, 请在 SupportTree I右键点击并选中的“ 重置 BranchNAME 以进行此承诺 ” 。

然后导航到仓库的本地目录并运行此命令 :

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

这将删除您本地仓库中当前一个后的所有承诺, 但只删除该分支 。

如何编辑上一个承诺

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

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

以下是工作流程:

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 <previous good commit id where you want the local repository  to go>

现在,我们通过使用此命令,在远程仓库大力推动这项良好承诺:

git push --force-with-lease

强制选项的“ 租赁” 版本将防止意外删除您不知道的新承诺( 即上次拉动后来自其它来源 ) 。

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

git reset --hard HEAD~1

如果你犯下了破案 但没有被逼

git reset --soft HEAD~1

HEAD ~ 1 是头部前的承诺的速记。 或者, 如果您想要重置的话, 您可以引用散列的 SHA-1 。 -- 软选项会删除此项承诺, 但是它会留下您更改过的所有文件“ 更改要执行 ” , 正如 git status 所声明的那样 。 如果您想要删除工作树中跟踪文件的任何更改, 因为任务在头部使用“ 硬” 取代前, 您可以在工作树中取消此任务 。

如果你已经被推了 然后有人拉了过来 通常是我的案子 你不能用Git重置

git revert HEAD

这将产生新的承诺, 扭转意外行为带来的一切。