我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。
我如何解除那些承诺?当地当地仓库?
我无意间把错误的档案 写错给Git 基特,但还没有将承诺推进到服务器上。
我如何解除那些承诺?当地当地仓库?
当前回答
如果存储库是在当地承诺的,但尚未被推到服务器上,那么另一种粗糙/佣人解决它的方法将是:
其他回答
如果您想要删除错误的文件, 您应该做
git reset --soft <your_last_good_commit_hash_here>
来,如果你来git status
中,您会看到中转区的文件。您可以选择错误的文件,然后从中转区取下来。
喜欢下面。
git reset wrongFile1 wrongFile2 wrongFile3
您现在可以添加您需要按键的文件,
git add goodFile1 goodFile2
提交提交它们
git commit -v
或git 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
只要在下面执行命令时重置它, 使用git
:
git reset --soft HEAD~1
解释:什么什么是git reset
基本上reset
任何承诺,如果你愿意 回去,如果你把它和--soft
键,它将返回,但保留您文件中的更改,所以您可以回到刚刚添加文件的阶段,HEAD
是分支的首脑,如果结合了~1
(在这种情况下,你们也使用)HEAD^
,它会回去 只有一个承诺 你想要什么...
我为您创建了以下图像中的步骤, 详情更多, 包括所有在真实情况下可能发生的步骤, 并承诺执行代码 :
使用 git- gui (或类似) 执行git commit --amend
。从 GUI 中,您可以从承诺中添加或删除单个文件。您也可以修改承诺信件。
仅将分支重新设置为上一个位置(例如,使用gitk
或git 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)
通常情况下,你可能不必担心你的私人部门 将历史变成原始历史。只是催促后续承诺(见上文“如何撤销公共承诺”),然后,做一个壁球- 合并隐藏历史。
撤消上一个任务
在很多情况下,你真的想撤销你代码中的最后一项承诺。例如,因为你想对它进行广泛的重组,甚至完全抛弃它!
在这种情况下,"重置"命令就是你最好的朋友:
$ git reset --soft HEAD~1
以上命令( 重置) 将把您当前的 HEAD 分支倒回指定的修改 。 在以上的例子中, 我们想回到当前修改前的修改中, 有效地取消我们上次的修改 。
注注:--soft
旗帜 : 这样可以确保未撤销修改中的更改被保存。 在运行命令后, 您会发现这些更改是您工作副本中未承诺的本地修改 。
如果您不想保留这些修改,只需使用--hard
。确保只在确定不再需要这些更改时才这样做。
$ git reset --hard HEAD~1
在回答之前,我们补充一些背景,解释一下这是什么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 t 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 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
.