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

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


当前回答

我找到了这个网站 用来描述如何撤销 你对存储库的承诺

一些命令 :

git commit --amend        # Change last commit
git reset HEAD~1 --soft   # Undo last commit

其他回答

如何编辑上一个承诺

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

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

以下是工作流程:

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 HEAD^ 

"清除工作树上的未知文件"

git clean    

见 - Git 快速参考

注意: 此命令将删除您上一个任务, 请谨慎使用! git 重置 -- hard 比较安全 。

$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend

重要的是要明白,当你正在修正你的最后一项承诺时,你并没有完全用新的、更好的承诺来取代它,新的承诺将旧承诺推开,而将新承诺置于其位置。 事实上,这好像前一项承诺从未发生,也不会出现在你的存储库历史中。

修改承诺的明显价值是略微改进你的最后一项承诺,而不会用表格“Oops,忘记添加文件”或“Darn,在最后一项承诺中确定打字符”等字词来模糊你的存储库历史。

2.4 Git Basics - 撤销事情

git reset --soft HEAD~1

git status

Output

在分支母版上, 您的分支比“ 来源/ 主机” 提前 1 承诺 。 (使用“ 给予推” 来发布您的本地承诺) 更改要执行 : (使用“ 给予恢复 - 级... ” 到 取消阶段) 新文件 : 文件 1 。

git log --oneline --graph

Output

90f8bb1 (HEAD - > master) 第二次承诺\ 7083e29 初始仓库承诺\

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