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

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


当前回答

有许多方法可以做到:

撤消上次执行/ 上一个承诺的 Git 命令 :

警告 :不使用 - 硬 如果你不知道自己在做什么, 硬就是太硬危险,它可能会删除您的文件 。

返回 Git 中的承诺的基本命令是 :

$ git reset --hard <COMMIT -ID>

$ git reset --hard HEAD~<n>

协调会 协调会:承诺的代号

n:您想要返回的上次承诺的数

您可以获取下列承诺代号:

$ **git log --oneline**

d81d3f1 function to subtract two numbers

be20eb8 function to add two numbers

bedgfgg function to multiply two numbers

何 地d81d3f1 d81d3f1贝多208执行 ID。

现在,让我们来看看一些案例:

假设您想要返回上次的“ d81d3f1 ” 。 这里有两个选项 :

$ git reset --hard d81d3f1

$ git reset --hard HEAD~1

假设您想要还原“ ebe20eb8” 承诺 :

$ git reset --hard be20eb8

欲了解更详细的信息,请参考并尝试其他命令,将头重置为指定状态:

$ git reset --help

其他回答

我验证了所提议的有效方法,这里就是使用这一方法的一个具体例子:

如果您想要永久撤销/取消您的最后一项承诺(等等, 一个一个一个, 尽可能多) , 有三个步骤 :

1: 获取您想要到达的承诺的 id = SHA, 当然

$ git log

2: 删除您先前的承诺

$ git reset --hard 'your SHA'

3: 将新的本地历史强制在您的原籍 GitHub 与-f选项( 将从 GitHub 历史中删除最后的磁轨)

$ git push origin master -f

示例示例示例示例

$ git log

最后一次承诺取消

commit e305d21bdcdc51d623faec631ced72645cca9131 (HEAD -> master, origin/master, origin/HEAD)
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 03:42:26 2020 +0200

U2_30 S45; updating files package.json & yarn.lock for GitHub Web Page from docs/CV_Portfolio...

现在就向总部提交我们想要的文件

commit 36212a48b0123456789e01a6c174103be9a11e61
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 02:38:01 2020 +0200

First commit, new title

删除最后一项, 达到承诺前, 删除最后一项

$ git reset --hard 36212a4

HEAD is now at 36212a4 First commit, new title

检查一下,没事的

$ git log

commit 36212a48b0123456789e01a6c174103be9a11e61 (HEAD -> master)
Author: Christophe <blabla@bla.com>
Date:   Thu Jul 30 02:38:01 2020 +0200

    First commit, new title

$ git status

On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
 (use "git pull" to update your local branch)

nothing to commit, working tree clean

更新 Git (Hub) 上的历史

$ git push origin master -f

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ GitUser bla bla/React-Apps.git
 + e305d21...36212a4 master -> master (forced update)

检查一下,没事的

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

如何编辑编辑编辑编辑先前犯过

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

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

以下是工作流程:

  1. git commit-edit <commit-hash>
    

    这将让您在您想要编辑的承诺上掉下来 。

    承诺的更改将是un un准备准备上场 如你所愿 这是第一次

  2. 并按你所希望的那样 确定并完成承诺

    (您可能想要使用)git stash save --keep-index来解开任何您没有做的文件)

  3. 重做承诺--amend, 例如 :

    git commit --amend
    
  4. 完成重定基数 :

    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 HEAD~
fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

发生错误的原因是,如果最后一次承诺是存储处的初始承诺(或没有父/母),则不存在 HEAD~ 。

解决方案

如果您想要重置“ master” 分支上唯一的承诺

$ git update-ref -d HEAD
$ git rm --cached -r .

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

git reset --hard origin/branch-name

恢复和放弃承诺是最好的,如果你想在向公共部门等提出补丁时保持历史清洁有用的话。

如果你必须放下最上层的承诺, 那么以下的单行帮助

git rebase --onto HEAD~1 HEAD

但如果你想放弃其中的 多项承诺,你确实说

- > b - > c - > > d - > 硕士

,然后您要放弃“ c” 承诺

git rebase --onto b c

这将使“ b” 成为“ d” 删除“ c” 的新基础