git checkout master + git reset --hard 会做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2421717/
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 will git checkout master + git reset --hard do?
提问by danwoods
I 'm relatively new to git and and having some problems early on. I've made several commits, but when I try to push them I get a response that says everything is up-to-date. I feel like my problem is the same on listed in this question, but it recommends the following:
我对 git 比较陌生,并且在早期遇到了一些问题。我已经提交了几次提交,但是当我尝试推送它们时,我得到了一个响应,说一切都是最新的。我觉得我的问题与此问题中列出的问题相同,但它建议如下:
$ git log -1
# note the SHA-1 of latest commit
$ git checkout master
# reset your branch head to your previously detached commit
$ git reset --hard <commit-id>
What exactly will "checking out the master" do? I just don't want to lose the changes I've made...
“查主”究竟会做什么?我只是不想丢失我所做的更改...
screenshot of gitk:
gitk 截图:
回答by Jimmy Cuadra
Checking out a branch moves the local HEAD
pointer so that it's pointing at the same commit that the branch references. For example:
检出分支会移动本地HEAD
指针,使其指向分支引用的同一个提交。例如:
When on branch mybranch
(the C
s are commits):
在分支上时mybranch
(C
s 是提交):
HEAD
|
V
master mybranch
| |
V V
C1 -------> C2 -------> C3
After running git checkout master
:
运行后git checkout master
:
HEAD
|
V
master mybranch
| |
V V
C1 -------> C2 -------> C3
This also moves files in your working directory as required so that it is a perfect snapshot of what the project looked like at that commit. It does not delete or alter commits, so you won't lose work in one branch by checking out another.
这也会根据需要移动工作目录中的文件,以便它是该提交时项目外观的完美快照。它不会删除或更改提交,因此您不会因为检查另一个分支而丢失一个分支中的工作。
What has happened in the case of a "detached head" as described in that other question is that C3
is not associated with a branch. In order to fix this, you need to update the commit that the master
branch points to so that it includes the new stuff (C3
). Checking out master
tells git that you are now working with the master branch, then doing a hard reset
with the SHA1 of the commit that you wantto be at the tip of your master
branch updates the branch references to what you want.
在另一个问题中描述的“分离头”的情况下发生的事情C3
与分支无关。为了解决这个问题,您需要更新master
分支指向的提交,以便它包含新内容 ( C3
)。检查出来master
告诉混帐,你现在与主分支工作,然后做一个硬reset
用的SHA1的承诺,你希望是在你的尖端master
分支更新分支引用到你想要的东西。
Edit:
编辑:
In this case, a detached head was not the issue. Just remember that committing and pushing are two different things in git. Committing does not communicate with a central repository like it does in Subversion. After you make changes to your working directory, you run git add filename
once for each file you've changed, where filename
is the name of the file. Once all the files have been added to the index, you commit them with git commit
.
在这种情况下,分离的头部不是问题。请记住,提交和推送在 git 中是两件不同的事情。提交不像在 Subversion 中那样与中央存储库通信。更改工作目录后,git add filename
对每个更改的文件运行一次,其中filename
是文件名。将所有文件添加到索引后,您可以使用git commit
.
A shorthand for this is to use git commit -a
which will automatically add modified files to the index before committing. This allows you to skip the git add
steps. Note that git commit -a
will only add modifiedfiles. If you're introducing a new file that has never been committed, you must manually add it with git add
.
对此的简写是使用git commit -a
它会在提交之前自动将修改后的文件添加到索引中。这允许您跳过这些git add
步骤。请注意,git commit -a
只会添加修改过的文件。如果您要引入一个从未提交过的新文件,则必须手动添加git add
.
Once your commit has been made, you can run git push
to send that commit to your remote repository and update the remote branches. This only does the remote communication stuff. Unlike Subversion, the commit itself is handled locally, without any interaction with the server.
提交完成后,您可以运行git push
以将该提交发送到远程存储库并更新远程分支。这只做远程通信的东西。与 Subversion 不同的是,提交本身是在本地处理的,无需与服务器进行任何交互。
回答by DJ.
git checkout master
is to switch your working area to the masterbranch also known as trunkin other version control system.
git checkout master
是将您的工作区域切换到主分支,在其他版本控制系统中也称为主干。