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

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


当前回答

添加/删除文件, 以便按照您想要的方式获取文件 :

git rm classdir
git add sourcedir

然后修正承诺:

git commit --amend

之前的错误承诺将会被编辑 以反映新的指数状态 换句话说,这就像你从未犯过错误一样

请注意,你只应该在还没有推的时候做这个。如果你已经推了,那么你只需要正常地做一个修正。

其他回答

在回答之前,我们补充一些背景背景,解释一下HEAD是。 这是。

First of all what is HEAD?

HEAD仅指当前分支的当前承诺(最新承诺)。
只有一个,只有一个,只有一个HEAD在任何给定时间(不包括git worktree).

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


detached HEAD

如果你们不履行最近的承诺,那末,HEAD指向历史中的先前承诺 它被称作detached HEAD.

Enter image description here

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

Enter image description here

Enter image description here


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


git checkout

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

你可以随时使用reflog并且,还有。
git reflog 将显示更新HEAD并检查想要的 reflog 条目将设置HEAD返回到此任务。

总部总部每次修改时,将有一个新的条目。reflog

git reflog
git checkout HEAD@{...}

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

Enter image description here


git reset --hard <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.
  • 注:(a)自Git 2.7以来)您也可以使用git rebase --no-autostash并且,还有。

enter image description here


git revert <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>

这个计划说明哪个指挥所做什么。
如你所见reset && checkout修改HEAD.

Enter image description here

如果您只是想要丢弃所有本地更改/ 承诺, 使您的本地分支看起来像您从... 开始的源分支...

git reset --hard origin/branch-name

您可以从本地仓库撤销您的承诺 。 请遵循以下方案 。

在下面的图像中, 我检查“ 测试” 分支( 使用 Git 命令)git checkout -b test)作为本地检查状态(使用 Git 命令)git status地方分支中没有任何可承诺的。

Enter image description here

在下一个图像图像中,您可以看到这里的图像 我做了一些修改过滤器1. txt然后用一些信息(使用 Git 命令) 将文件添加到中继区, 并用一些信息( 使用 Git 命令) 来进行更改git commit -m "Doing commit to test revert back").

"-m 用于发送信件"

Enter image description here

在下一个图像中,您可以看到您承诺的日志,无论您承诺什么(使用 Git 命令)git log).

Enter image description here

所以在以上图像中,您可以看到每个承诺的承付代号, 以及您现在的执信信息, 无论您想要返回或撤消的复制件, 即实施 ID 并击中下面的 Git 命令,git revert {"paste your commit id"}示例:

git revert 9ca304ed12b991f8251496b4ea452857b34353e7

Enter image description here

我恢复了上次的承诺。 现在, 如果您检查您的 Git 状态, 您可以看到修改后的文件过滤器1. txt并且尚未承诺。

Enter image description here

与您一起工作的人源树树这会帮助你。

右单击右键在那日,选定 "重置( 当前分支)/ master 用于此承诺" 和上一个选定 “ 软” 重置.

Enter image description here

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

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