`git reset --hard master` 和 `git reset --hard origin/master` 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29862319/
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
What's the difference between `git reset --hard master` and `git reset --hard origin/master`?
提问by vashishatashu
I tried a lot of links on Stackoverflow/elsewhere to properly understand behaviour of
我在 Stackoverflow/elsewhere 上尝试了很多链接以正确理解
git reset --hard option
git reset --hard option
I know that:
我知道:
- If it is omitted or if it is
origin
, reset is done on most recent commit onorigin
- If a SHA1 hash is provided, reset is done on the corresponding commit.
- 如果省略或省略
origin
,则在最近一次提交时完成重置origin
- 如果提供了 SHA1 哈希,则在相应的提交上完成重置。
What I don't understand are the following values:
我不明白的是以下值:
origin
HEAD
origin/master
origin/branch
origin
HEAD
origin/master
origin/branch
All seem to have same behavior i.e. they a point to latest commit on master
.
所有似乎都具有相同的行为,即它们指向master
.
Please explain what is the significance of all 4 value option provided above.
请解释上面提供的所有 4 个值选项的意义是什么。
I would also like to know if I am on a specific branch how can I reset to the last commit on that very branch?
If for example I am on v1.2
, origin/v1.2
still takes me to latest commit on master
.
我还想知道我是否在特定分支上,如何重置到该分支上的最后一次提交?例如v1.2
,如果我在,origin/v1.2
仍然带我到最新的提交master
。
回答by Mohammad AbuShady
First of all you need to understand that branch and tag names are just pointers to hash values, which represent a single commit, if you say that there's 4 options that have the same behaviour, then the first logical answer is because they all point to the same commit
首先,您需要了解分支和标签名称只是指向哈希值的指针,它们代表一次提交,如果您说有 4 个选项具有相同的行为,那么第一个合乎逻辑的答案是因为它们都指向相同的提交
origin
I'm not sure about this but i thinkorigin
by it self will resolve toorigin/HEAD
which i think will be dependent on your github settings, in github you set a 'default branch',origin/head
will resolve toorigin/[default_branch]
, in your case im assuming it's master, so this is why it has the same effect asorigin/master
.HEAD
always points to the current commit, the one you are standing on, so agit reset --hard HEAD
will permenantly remove all changes in tracked files and staged files changes, but not change the commit hash.origin/master
is last commit in the remote master branch since your last fetch/pull, each time you commit tomaster
, your localmaster
is updated and yourorigin/master
is updated too, if someone else pushes tomaster
your repo will have no idea that there is an update, unless you do agit fetch
then yourorigin/master
will move ahead of yourmaster
, or maybe even diverge.
running agit reset --hard origin/master
will have the same effect if you are currently are on themaster
branch and themaster
is in sync withorigin/master
origin/branch
I'm not sure what this represents, because there's noorigin/branch
by default, so I'm guessing you created a branch calledbranch
and happens to be in the same commit as your master, to confirm you can try doing agit branch
to see all your branches, i'm guessing you'll find one calledbranch
origin
我不确定这一点,但我认为origin
它自己会解决origin/HEAD
,我认为这取决于你的 github 设置,在 github 中你设置了一个“默认分支”,origin/head
将解决origin/[default_branch]
,在你的情况下,我假设它是主人,所以这就是为什么它具有与origin/master
.HEAD
始终指向当前提交,即您所在的提交,因此 agit reset --hard HEAD
将永久删除跟踪文件和暂存文件中的所有更改,但不会更改提交哈希。origin/master
是自上次获取/拉取以来远程主分支中的最后一次提交,每次提交时master
,您的本地master
都会更新,您的本地也会更新origin/master
,如果其他人推送到master
您的存储库将不知道有更新,除非您做一个git fetch
然后你的origin/master
意志走在你的前面master
,甚至可能会发散。
运行git reset --hard origin/master
将具有相同的效果,如果您当前是在master
分支和master
处于同步与origin/master
origin/branch
我不确定这代表什么,因为origin/branch
默认情况下没有,所以我猜你创建了一个名为的分支,branch
并且恰好与你的主人在同一个提交中,以确认你可以尝试git branch
查看你的所有分支,我猜你会发现一个叫branch
To see all this in a visual way, you could try running git log --graph --decorate --all
or I prefer a visual tool like gitk
, if you have the binary installed you would run gitk --all
to see all branches relative to each other
要看到一个可视化的方式这一切,你可以尝试运行git log --graph --decorate --all
或者我喜欢的可视化工具一样gitk
,如果你有二进制安装你可以运行gitk --all
看相互所有分支
回答by Tobia Tesan
master
, HEAD
, origin/something
and maybe some tag, why not, mayall point to the same commit, but they are most definitely not the same thing.
master
, HEAD
,origin/something
也许还有一些标签,为什么不,可能都指向同一个提交,但它们绝对不是同一件事。
origin
is usually the name of a remoterepository.
origin
通常是远程存储库的名称。
You can see your remotes and configure new ones with git remote -v
.
您可以查看您的遥控器并使用git remote -v
.
Try it (with -v
) and it will probably make sense.
尝试一下(使用-v
),它可能会有意义。
remote/somebranch
points to the head of some branch on a remote repository.
remote/somebranch
指向远程存储库上某个分支的头部。
origin/master
points to the head of master
on origin
.
origin/master
指向的头部master
上origin
。
Is it the same as master
?
是一样的master
吗?
Yes and no. If you pull your master branch, do some work and in the meantime somebody else commits on master
and pushes to origin
, they will differ.
是和否。如果你拉你的主分支,做一些工作,同时其他人提交master
并推送到origin
,他们会有所不同。
When you do a git fetch origin
, then, origin/master
will have additional commits (will be ahead).
当您执行 agit fetch origin
时,origin/master
将有额外的提交(将领先)。
HEAD
is simply "the current commit".
Think of it as .
.
HEAD
只是“当前提交”。把它想象成.
.
See this question
看到这个问题
Again, this couldbe the same as master
, but if you check out another branch or commit or are in the middle of a rebase, well, it's not.
同样,这可能与 相同master
,但如果您检查另一个分支或提交,或者处于 rebase 的中间,那么,事实并非如此。
So try this on a fresh repository on which nobody else is working:
所以在一个没有其他人工作的新存储库上试试这个:
$ git checkout master
$ git log -1 --format="%H" HEAD
123abc
$ git log -1 --format="%H" origin/master
123abc
They are the same!
他们是一样的!
$ git diff origin/master
Of course their content is the same.
当然它们的内容是一样的。
$ echo "foo" > foo
$ git add foo
$ git commit -m "Foo the thingy"
$ git log -1 --format="%H" HEAD
321bca
$ git log -1 --format="%H" origin/master
123abc
Ah, look, now they're different commits!
啊,看,现在它们是不同的提交!
$ git push origin master
$ git log -1 --format="%H" HEAD
321bca
$ git log -1 --format="%H" origin/master
321bca
And now they aren't! we have pushed our latest commit and they both point to the same.
而现在他们不是!我们已经推送了我们最新的提交,它们都指向相同的内容。
$ git checkout -b newbranch
$ echo "baz" > baz
$ git add baz
$ git commit -m "Baz the thingy with the stuff"
$ git branch -a
master
* new_branch
origin/master
$ git log -1 --format="%H"
789def
$ git log -1 --format="%H" master
321bca
git log -1 --format="%H" origin/master
321bca
git log -1 --format="%H" origin/new_branch
unknown revision or path not in the working tree.
Of course not. We haven't pushed new_branch
to origin
, it is only on our local machine
当然不是。我们还没有推new_branch
送到origin
,它只是在我们的本地机器上
git checkout 123abc
We have just checked out 123abc
, the old head of master
. It is not the head of any branch, now, but we can check it out just the same.
我们刚退房123abc
,老团长了master
。现在它不是任何分支的负责人,但我们可以同样地检查它。
Note: checking out 123abc. You are in 'detached HEAD' state, etc
$ git checkout -b old_master
$ git branch -a
master
* new_branch
origin/master
old_master
Now guess what their SHA1 will respectively be?
现在猜猜他们的 SHA1 分别是多少?