git 克隆存储库的“重置”命令是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12669278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:34:15  来源:igfitidea点击:

What is the "reset" command for a git cloned repository?

gitversion-controlrepository

提问by axis

I have downloaded a repository with

我已经下载了一个存储库

git clone <address>

i have made some modification and have edited some files, now i want to discard everything and just be sure that what i have on the disk is nothing else than the original remote version of the codebase: what is the right command ?

我进行了一些修改并编辑了一些文件,现在我想丢弃所有内容,并确保我在磁盘上的内容只是代码库的原始远程版本:什么是正确的命令?

回答by Stephen Connolly

Here is the long explanation so that you understand what is going on:

这是很长的解释,以便您了解发生了什么:

Assuming the remote is called origin

假设遥控器被调用 origin

git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master

What this does is:

它的作用是:

  1. (Optional if git status says no modified files) Discard anymodified files on the disk (that's why reset --hard)

  2. Checkout the remote master branch (note: you will be in a “detatched head” state)

  3. Delete the local masterbranch (throwing away all your local changes)

  4. Call the current head as the new masterbranch

  1. (如果 git status 显示没有修改的文件,则可选)丢弃磁盘上的任何修改过的文件(这就是为什么reset --hard

  2. 签出远程主分支(注意:您将处于“分离头”状态)

  3. 删除本地master分支(丢弃所有本地更改)

  4. 将当前head称为新master分支

Now you probably want to do something slightly different... i.e. don't throw away your changes , just put them on another named branch... after all you never know when you'll need them again

现在你可能想做一些稍微不同的事情......即不要丢弃你的更改,只需将它们放在另一个命名的分支上......毕竟你永远不知道什么时候会再次需要它们

git checkout -b my-silly-changes
git branch -D master
git checkout -b master origin/master

That saves your current changes in a new local branch called my-silly-changesand then removes the old local branch called masterand finally recreates it from the remote head.

这会将您当前的更改保存在一个名为的新本地分支中my-silly-changes,然后删除旧的本地分支master,最后从远程头重新创建它。

And here is the explanation for people who think they know what they are doing:

以下是对那些认为自己知道自己在做什么的人的解释:

The single command:

单一命令:

git reset --hard origin/master

Will discard any local changes and re-point the current branch to the most recently fetched state of origin/master. This has the exact same effect as the four commands at the start of this answer, but without looking behind the curtain

将丢弃任何本地更改并将当前分支重新指向origin/master. 这与此答案开头的四个命令具有完全相同的效果,但无需看幕后

回答by sleske

If you are certain you want to discard all files, and loseall your modifications (!), you can do:

如果您确定要丢弃所有文件并丢失所有修改 (!),您可以执行以下操作:

git reset --hard origin/master

This will reset the state of your local copy of the "master" branch and of your working copy to the last version that you fetched from your upstream (original remote).

这会将“主”分支的本地副本和工作副本的状态重置为您从上游(原始远程)获取的最后一个版本。

Note: This assumes that

注意:这假设

  • your upstream repository is called "origin" (the default for git clone)
  • you are working in a clone of the remote masterbranch
  • 您的上游存储库称为“来源”(默认为git clone
  • 您正在远程master分支的克隆中工作

If you are working in a branch other than "master" , adjust the command:

如果您在 "master" 以外的分支中工作,请调整命令:

git reset --hard origin/branch1

回答by Ikke

If you haven't committed anything, then git reset --hardis just enough.

如果你什么都没犯,那git reset --hard就够了。

Hereis a good resource explaining how git reset with its different modes work (--hard, --mixed and --soft).

是一个很好的资源,解释了 git reset 及其不同模式(--hard、--mixed 和--soft)的工作原理。

Disclaimer:

免责声明

git reset --hardis an unsafe operation. It removes any uncomnitted changes from the working tree, and cannot be undone.

git reset --hard是不安全的操作。它从工作树中删除任何未提交的更改,并且无法撤消。