我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
我无意中把错误的文件给了Git, 但还没有把承诺推到服务器上。
我如何从当地存储库撤销这些承诺?
当前回答
如果你们在本地做出你们不喜欢的事,而他们还没有被推倒,而你们却可以把事情重现为以往的善事。这好像坏事从未发生过。下面是这样的:
在您的终端中( 终端、 Git Bash 或 Windows 命令提示) , 浏览到您的 Git repo 的文件夹中。 运行 git 状态, 并确保您有干净的工作树。 每个承诺都有一个独特的散列( 看起来像 2f5451f ) 。 您需要找到最后一件好事的散列( 您想要返回到哪个地方 ) 。 这里有两个地方您可以看到要承诺的散列 : 在 GitHub 或 Bitbucket 网站或网站的承诺历史中 。 在您的终端中( 终端、 Git Bash 或 Windows 命令快递) 运行命令 git 日志 -- 在线, 一旦您知道最后一件好事的仓列( 您想要返回到哪个地方) , 请运行以下的命令( 将 2f5451f 与您的承诺重写 ) :
git reset 2f5451f
git reset --hard 2f5451f
注意 : 如果您确实重置承诺, 将会删除, 但更改将显示为未承诺, 允许您访问代码 。 这是最安全的选项, 因为也许您想要一些代码, 现在您可以做出一些好的更改和新承诺 。 通常您会想要撤销承诺, 丢弃代码, 这正是 Git 重置 -- hard 所做的 。
其他回答
添加/删除文件, 以便按照您想要的方式获取文件 :
git rm classdir
git add sourcedir
然后修正承诺:
git commit --amend
之前的错误承诺将会被编辑 以反映新的指数状态 换句话说,这就像你从未犯过错误一样
请注意,你只应该在还没有推的时候做这个。如果你已经推了,那么你只需要正常地做一个修正。
典型基周期
在前几次答复中谈到与Git相关的命令时,我想向所有读者介绍我典型的Git周期,这可能是有益的。 这就是我如何与Git合作,
首次从远程服务器上克隆 $project pulling 从远程( 当我没有未完成的本地推动承诺) git 将新的本地文件 1 添加到 $to_be_compent_list_list (只是想象 $to_be_compent_list means staged 区域) git 添加 $file1 从 $to_be_compent_list_list_list 列表中删除错误添加的文件 2 (假设文件2 添加为步骤 3 ) git 重新设置 $file2 提交文件 1 , 该文件位于 $to_ be_compent_ list git 承诺 - m “ comm 信件描述” 与远程仓库同步本地文件 1 (在冲突发生时配置合并工具 Git 合并时重新解析 # re Solive 合并, 也可以手动合并 添加冲突解析文件, 让我们说: git 添加 $file1 继续我之前的重定义命令 gfile1 -- 继续推进和已经同步的上次本地承诺推进源头: rechangs/ for
如何编辑上一个承诺
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
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
IntelliJIDEA 您可以按 Alt+9 键右键单击承诺列表中的某些标签打开 Git 仓库日志, 然后选择 : “ 将当前分支重置到这里... ” 。
我更喜欢用Git rebase - i 来做这份工作,因为一个不错的名单会出现在我可以选择摆脱承诺的地方。它可能不像这里其他答案那样直接,但它只是感觉是正确的。
选择要列出多少个承诺, 然后这样引用( 加入最后 3 个)
git rebase -i HEAD~3
样本列表
pick aa28ba7 Sanity check for RtmpSrv port
pick c26c541 RtmpSrv version option
pick 58d6909 Better URL decoding support
然后 Git 将删除任何您删除的线条 。