git checkout master + git reset --hard会怎么样? [英] What will git checkout master + git reset --hard do?

查看:145
本文介绍了git checkout master + git reset --hard会怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对git比较陌生,并且在早期遇到一些问题。我做了几个提交,但是当我尝试推送它们时,我收到了一个回复,表示一切都是最新的。我觉得我的问题与上面列出的问题是一样的这个问题,但它建议如下:

  $ git log -1 
#注意最新提交的SHA-1
$ git checkout master
#重置你的分支头到你以前分离的提交
$ git reset --hard< commit-id>

检查主人到底会做什么?我只是不想失去我所做的改变......



gitk的截图:

解决方案

检出一个分支移动本地 HEAD 指针,以便它指向与分支引用相同的提交。例如:

当分支 mybranch 时( C s是提交):

  HEAD 
|
V
master mybranch
| |
V V
C1 -------> C2 -------> C3

运行 git checkout master

  HEAD 
|
V
master mybranch
| |
V V
C1 -------> C2 -------> C3

这也会根据需要移动工作目录中的文件,以便它能够完美地显示项目看起来像那个提交。它不会删除或更改提交,因此您不会在一个分支中通过签出另一个分支而失去工作。



发生分离头部如其他问题所述, C3 不与分支关联。为了解决这个问题,你需要更新 master 分支指向的提交,以便它包含新的东西( C3 )。检出 master 会告诉git您现在正在使用master分支,然后使用SHA1中的<1 code> reset 您希望提交到 master 分支的顶端,以将分支引用更新为您想要的内容。



编辑:



在这种情况下,分离的头部不是问题。请记住,提交和推送是git中的两个不同的事情。提交不会像Subversion那样与中央存储库进行通信。在对工作目录进行更改后,对每个已更改的文件运行 git add filename 一次,其中 filename 是文件的名称。一旦所有的文件都被添加到索引中,您可以使用 git commit 来提交它们。



这个简写是使用 git commit -a ,它会在提交之前自动将修改后的文件添加到索引中。这允许您跳过 git add 步骤。请注意, git commit -a 只会添加已修改文件。如果您要引入一个从未提交的新文件,则必须手动将其添加到 git add



提交完成后,您可以运行 git push 将该提交发送到远程存储库并更新远程分支。这只是远程通信的东西。与Subversion不同,提交本身是在本地处理的,与服务器没有任何交互。


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 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:

解决方案

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

When on branch mybranch (the Cs are commits):

                        HEAD
                        |
                        V
            master      mybranch
            |           |
            V           V
C1 -------> C2 -------> C3

After running 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 want to be at the tip of your master branch updates the branch references to what you want.

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.

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 modified files. If you're introducing a new file that has never been committed, you must manually add it with 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 checkout master + git reset --hard会怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆