我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。
我如何解除那些承诺?当地当地仓库?
我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。
我如何解除那些承诺?当地当地仓库?
当前回答
在这种情况下,"重置"命令就是你最好的朋友:
git reset --soft HEAD~1
重置将会将您当前的 HEAD 分支倒带到指定的修改 。 在以上的例子中, 我们希望回到当前修改之前的修改 - 有效地取消我们最后的承诺 。
注意 -- soft 旗号 : 这样可以确保未撤销修改的更改被保存。 在运行命令后, 您会发现您的工作副本中这些更改是未承诺的本地修改 。
如果您不想保留这些更改, 请使用 -- 硬旗 。 确定只有在您确定不再需要这些更改时才这样做 。
git reset --hard HEAD~1
其他回答
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
git commit-edit <commit-hash>
这将让您在您想要编辑的承诺上掉下来 。
承诺的更改将是un un准备准备上场 如你所愿 这是第一次
并按你所希望的那样 确定并完成承诺
(您可能想要使用)git stash save --keep-index
来解开任何您没有做的文件)
重做承诺--amend
, 例如 :
git commit --amend
完成重定基数 :
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 log
commit 101: bad commit # Latest commit. This would be called 'HEAD'.
commit 100: good commit # Second to last commit. This is the one we want.
要让一切恢复到上次承诺之前的状态 我们需要reset
之前的承诺HEAD
:
git reset --soft HEAD^ # Use --soft if you want to keep your changes
git reset --hard HEAD^ # Use --hard if you don't care about keeping the changes you made
现在git log
将显示我们最后的承诺已被删除 。
如果您已经公开了承诺, 您将会想要创建一个新的承诺, 它将“ 撤销” 您对上一个承诺( 当前 HEAD) 所做的更改 。
git revert HEAD
你们的更改将恢复,并准备好你们承诺:
git commit -m 'restoring the file I removed by accident'
git log
commit 102: restoring the file I removed by accident
commit 101: removing a file we don't need
commit 100: adding a file that we need
更多信息,请查看Git Basics - 撤销事情.
这将添加一个新的承诺, 删除所添加的文件 。
git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"
或者您可以重写历史来撤销最后一项承诺 。
警告警告警告警告:此命令将永久删除对《京都议定书》的修改.java
您承诺的文件( 和任何其他文件) -- 从工作目录中删除全部修改 :
git reset --hard HEAD~1
缩略hard reset
至HEAD-1
您的工作副本将在您错误犯罪之前设定为犯罪状态 。
主要设想有两种情况:
你还没推动承诺呢
如果问题在于您所做的额外文件( 而您不希望存储库中的文件), 您可以使用git rm
并且实践,--amend
git rm <pathToFile>
您也可以以下列方式删除整个目录-r
,或甚至与其他巴什命令命令
git rm -r <pathToDirectory>
git rm $(find -name '*.class')
删除文件后, 您可以执行-- amend -- 修正 -- 修正选项选项选项
git commit --amend -C HEAD # the -C option is to use the same commit message
这将重写您最近的本地承诺删除额外文件, 因此, 这些文件将不会被按键发送, 并且 GC 将会从您本地的. git 仓库中删除 。
您已经推进了任务
您可以对其它情景应用相同的解决方案,然后做git push
和和-f
选项选项,但它是未建议因为它以不同的变化(它会破坏你的仓库) 推翻了遥远的历史。
相反,你必须做承诺--amend
(记住关于 - amend : 该选项重写上次承诺的历史) 。