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

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


当前回答

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

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

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

以下是工作流程:

  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

其他回答

通常情况下,你想要取消对因为您犯了一个错误,所以您想要修正它 基本上就是OP在问问题时所做的。真的,您真的想要复复做a 承诺。

这里的大多数答案都集中在命令行上。 虽然命令行是使用Git的最佳方法, 当你对它感到舒服的时候, 它可能与来自其他版本控制系统到 Git 的人有点不同。

使用 GUI 如何操作。 如果您安装了 Git, 您已经拥有了所有您需要遵守的指示 。

注:我在这里假设您在推动前意识到承诺是错误的。 如果您不知道推力意味着什么, 那么您可能还没有推动。 继续执行指令 。 如果您推动错误承诺, 最危险的方法就是跟踪错误承诺, 并做出新的承诺, 来修补事情, 您在版本控制系统中这样做的方式不允许您重写历史 。

使用图形用户界面来修正你最近的过错:

  1. 导航到命令行的仓库,并用git gui
  2. 选择“ 修正上一个任务 ” 。 您将会看到您上次的委托信件、 您所准备的文件 以及您没有的文件 。
  3. 现在将事情更改为您想要它们查看的方式, 并单击 CON 。

IntellelliJIDEA 您可以按下打开 Git 的 Git 仓库日志平平+9,鼠标右键单击承诺列表中的某些标记,并选择:重置当前分支到这里....

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

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

Enter image description here

使用此命令 :

git checkout -b old-state number_commit

如何修正上一个本地承诺

使用 git- gui (或类似) 执行git commit --amend。从 GUI 中,您可以从承诺中添加或删除单个文件。您也可以修改承诺信件。

如何撤销上一个本地任务

仅将分支重新设置为上一个位置(例如,使用gitkgit rebase) 。然后从保存的副本中重新应用您的更改。在您本地仓库的垃圾回收收集后,它会像没有发生不想要的操作一样。要用一个命令来完成所有这些,请使用git reset HEAD~1.

警告单词: 粗心使用git reset我建议基特新手们尽可能避免这样做。

如何撤销公开承诺

表演a逆向樱桃摘 (git- rever 反转)来撤销更改。

如果你还没有在你的分支上做其他的修改, 你可以简单地做。 。 。

git revert --no-edit HEAD

然后将更新的分支推至共享仓库 。

承诺历史将分别显示两个承诺.


高级:校正私立公共存储库分支

这可能很危险 -- -- 请确定您有该分支的本地副本要反射 。

备注:如果有其他人在为分支工作,您不想这样做。

git push --delete (branch_name) ## remove public version of branch

清理你的分行 在当地,然后冲...

git push origin (branch_name)

通常情况下,你可能不必担心你的私人部门 将历史变成原始历史。只是催促后续承诺(见上文“如何撤销公共承诺”),然后,做一个壁球- 合并隐藏历史。