我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。
我如何解除那些承诺?当地当地仓库?
我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。
我如何解除那些承诺?当地当地仓库?
当前回答
通常我并不想撤销一连串的承诺, 而是编辑早先的承诺,
我发现自己经常去修修过去的东西 以至于我写了剧本
以下是工作流程:
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 Bash 或 Windows 命令提示) , 浏览到您的 Git repo 的文件夹中。 运行 git 状态, 并确保您有干净的工作树。 每个承诺都有一个独特的散列( 看起来像 2f5451f ) 。 您需要找到最后一件好事的散列( 您想要返回到哪个地方 ) 。 这里有两个地方您可以看到要承诺的散列 : 在 GitHub 或 Bitbucket 网站或网站的承诺历史中 。 在您的终端中( 终端、 Git Bash 或 Windows 命令快递) 运行命令 git 日志 -- 在线, 一旦您知道最后一件好事的仓列( 您想要返回到哪个地方) , 请运行以下的命令( 将 2f5451f 与您的承诺重写 ) :
git reset 2f5451f
git reset --hard 2f5451f
注:如果您确实重置了承诺, 承诺将被删除, 但更改将显示为未承诺, 允许您访问代码 。 这是最安全的选项, 因为也许您想要一些代码, 现在您可以做出一些好的更改和新承诺 。 通常您会想要撤销承诺, 丢弃代码, 这正是 Git 重置 -- 硬键所做的 。
为了将一些文件从 Git 承诺中移除, 请使用“ - 软” 选项的“ git 重置” 命令, 并在 HEAD 之前指定此承诺 。
$ git reset --soft HEAD~1
当运行此命令时, 您将会看到最近任务( HEAD) 中的文件, 您将能够执行它们 。
现在您的文件在中转区, 您可以使用“ git reset” 命令再次删除它们( 或取消它们) 。
$ git reset HEAD <file>
注意: 这次, 您正在从 HEAD 中重新设置文件, 因为您只是要将文件从您的中转区域中排除 。
如果您对该文件不再感兴趣,请使用“git rm” 命令从索引(也称为中转区)中删除文件。
$ git rm --cached <file>
修改完成后,您可以使用“修正”选项再次进行修改。
$ git commit --amend
要验证文件是否正确从仓库中移除文件, 您可以运行“ git Is- 文件” 命令, 检查文件是否在文件中出现( 如果是新的文件) 。
$ git ls-files
<file1>
<file2>
自 Git 2. 23 以来, 有一种将文件从承诺中移除的新方式, 但是您必须确保您使用的 Git 版本大于或等于 2. 23 。
$ git --version
2.244.1 Git 版本
注: Git 2.23 于2019年8月发布,
要安装更新版本的 Git, 您可以检查此教程 。 要将文件从承诺中删除, 请使用“ git reform” 命令, 使用“ 源代码” 选项指定源代码, 并指定要从仓库中删除的文件 。
例如,为了将名为“ Myfile” 的文件从HEAD 删除, 您将写入以下命令
$ git restore --source=HEAD^ --staged -- <file>
举例来说, 让我们假装您编辑了您最近在您的“ 主管” 分支上的承诺中的文件 。
文件已正确执行, 但您想要从您的 Git 仓库中删除它 。
要从 Git 仓库中删除您的文件, 您需要先恢复它 。
$ git restore --source=HEAD^ --staged -- newfile
$ git status
您的分支比“ 来源/ 主管” 提前 1 个承诺 。 (使用“ 提供推动” 来发布您本地的承诺 )
拟承诺的修改:
(use "git restore --staged <file>..." to unstage)
modified: newfile
未准备进行的更改:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: newfile
如你所见 你的档案又回到了中转区
从那里,您有两个选择, 您可以选择编辑您的文件, 以便重新重新打开它, 或者简单地从您的 Git 仓库中删除它 。
在本节中,我们将描述从您的 Git 仓库中删除文件的步骤。
首先,您需要卸载您的文件, 因为如果它被摆放, 您将无法删除它 。
要卸载文件, 请使用“ git 重置” 命令, 并指定 HEAD 为源代码 。
$ git reset HEAD newfile
当您的文件被正确卸载时, 请使用“ git rm” 命令, 使用“ cached” 选项来从 Git 索引中删除此文件( 这不会删除磁盘上的文件)
$ git rm --cached newfile
r m ' newfile' (新文件)
如果您现在检查了仓库状态, 您将可以看到 Git 正在准备删除承诺 。
$ git status
您的分支比“ 来源/ 主管” 提前 1 个承诺 。 (使用“ 提供推动” 来发布您本地的承诺 )
拟承诺的修改:
(use "git restore --staged <file>..." to unstage)
deleted: newfile
现在您的文件已经准备就绪, 只需使用“ 授权承诺” 和“ 修正” 选项, 以修正您仓库中的最新承诺 。
`$ git commit --amend
[master 90f8bb1] Commit from HEAD
Date: Fri Dec 20 03:29:50 2019 -0500
1 file changed, 2 deletions(-)
delete mode 100644 newfile
`如你所见,这不会产生新的承诺,但将从根本上修改最近的承诺,以便包括您的更改。
某些情况下,你不希望所有文件再次出现:你只有一个文件可以修改一个非常具体的存储库文件。
要从 Git 承诺中删除特定文件, 请使用“ 软” 选项中的“ git 重置” 命令, 指定 HEAD 之前的承诺, 以及您要删除的文件 。
$ git reset HEAD^ -- <file>
修改完成后,您的文件就会回到中转区
首先,您可以选择使用“ Git 重置” 命令从中转区域删除文件,并指定您要从 HEAD 中重置。
$ git reset HEAD <file>
注意: 这并不意味着您会丢失此文件中的更改, 只是文件会被从中转区删除 。
如果您想要从索引中完全删除文件, 您就必须使用“ git rm” 命令, 并使用“ cached” 选项 。
$ git reset HEAD <file>
为了确保您的文件被正确从中转区移除, 请使用“ git Is- files” 命令来列出属于索引的文件 。
$ git ls-files
当您的修改完全完成后, 您可以使用“ git deform” 命令和“ amend” 选项来修改您删除文件的承诺 。
$ 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
.
在命令行上,它会看起来像这个 - SHA-1 而不是从HEAD
不指向当前分支的端点 :
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@{...}
这样你就可以回到你想要的事业了
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.
git rebase --no-autostash
并且,还有。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
.
为了完整起见,我将提出前几个答复所忽略的一个明显明显的方法。
由于承诺没有被推,遥控器没有改变,因此:
有时候,如果你的高贵的Git客户告别(看着你,伊吉特),这有时是必要的。
别忘了重新承诺保存已保存上次按键后的变化 。
通过下列方式查看日志来查找上次承诺的散列代码 :
git log
然后
git reset <the previous co>