git pull会改变HEAD吗?

时间:2019-05-31 10:52:33

标签: git

我了解git checkout如何更改HEAD指针,但是git pull呢?

HEAD可以看作是“当前分支”,但是当您执行git pull且当前分支被更改时。它会影响HEAD指针吗?为什么?

3 个答案:

答案 0 :(得分:1)

Usually HEAD is a double-indirection to a commit. git checkout manipulates the first indirection, git commit, git merge, git pull and other commands the second.

Example: After git checkout master the file .git/HEAD contains

ref: refs/heads/master

which is a "symbolic reference" to a branch ref (see git help symbolic-ref). Doing git checkout develop gives you

ref: refs/heads/develop

git pull (which is either git fetch; git merge or git fetch; git rebase) will not touch the .git/HEAD but manipulate .git/refs/heads/master (and/or git./packed-refs).

So the answer is: git pull does not manipulate HEAD but the ref HEAD points to. The effect is that HEAD points to another commit.

答案 1 :(得分:0)

根据git-scm

  

更准确地说,git pull使用给定的参数运行git fetch并调用git merge来“将检索到的分支头合并到当前分支中”。

注意:我已添加双引号以突出显示。


  

它会影响HEAD指针吗?

是的,如果已撤消较新的提交。

  

为什么?

因为HEAD引用了最近的提交。当您提取最新更改(读取提交)时,HEAD必须引用所提取的较新提交。

答案 2 :(得分:0)

Does it affect HEAD pointer?

It's easy to find out for yourself. In you local clone of some git repo, create a new branch (so that you can mess around without endangering any of your work), and make sure that it's up to date with the remote:

git checkout -b test-branch
git pull

Next, take a look at HEAD in that branch:

git log

Note the value of HEAD, which is the most recent commit. Now change HEAD using the reset command to go back a few commits:

git reset --hard HEAD~3

Now you've intentionally changed HEAD -- that's what reset does. The synopsis of the reset command is: Reset current HEAD to the specified state. That is, you've just gone back in time to 3 commits before the latest commit. Let's look at HEAD again:

git log

You can see that HEAD has changed from what it was before. Now, use pull to add the "new" commits from the remote back to your branch, and look again at HeAD:

git pull
git log

You should see that HEAD is back to where it was before the reset, and this should show you that git pull does change HEAD and why.

HEAD isn't special for any reason other than being a name for the latest commit. Anything you do that changes what git sees as the latest commit in your branch changes the value of HEAD.