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

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

What will git checkout master + git reset --hard do?

gitbranch

提问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:
enter image description here

gitk 截图:
在此处输入图片说明

回答by Jimmy Cuadra

Checking out a branch moves the local HEADpointer so that it's pointing at the same commit that the branch references. For example:

检出分支会移动本地HEAD指针,使其指向分支引用的同一个提交。例如:

When on branch mybranch(the Cs are commits):

在分支上时mybranchCs 是提交):

                        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 C3is not associated with a branch. In order to fix this, you need to update the commit that the masterbranch points to so that it includes the new stuff (C3). Checking out mastertells git that you are now working with the master branch, then doing a hard resetwith the SHA1 of the commit that you wantto be at the tip of your masterbranch 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 filenameonce for each file you've changed, where filenameis 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 -awhich will automatically add modified files to the index before committing. This allows you to skip the git addsteps. Note that git commit -awill 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 pushto 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 masteris to switch your working area to the masterbranch also known as trunkin other version control system.

git checkout master是将您的工作区域切换到分支,在其他版本控制系统中也称为主干