Before my questions, I would like to know if my understanding of some Git concepts is correct, and please feel free to correct me:
- a branch name points to the tip (i.e. the end commit) of a branch, and
- HEAD points to the branch name of the current branch, and
- the current branch is defined as the branch checked out into the working directory.
Here are my questions:
When a new commit is added to a branch by a git command (any git command that can add commits to a branch),
Questions:
does the git command always move the branch name forward to point to the new commit?
Or are there git commands, which add new commits to a branch, which do not move the branch name and thus require me running some additional git command to move it?
When
- a new commit is added to the current branch by a git command and
- the branch name is moved forward to point to the new tip commit in the current branch,
Questions:
is it necessary to update HEAD additionally?
My guess is no, because HEAD has already pointed to the current branch's name, the current branch's name has already been updated to point to the new tip commit of the current branch, and moving the current branch's name doesn't change the fact that HEAD points to the current branch's name. So all looks fine without additional change to HEAD.
has the working directory already been updated to be the same as the new tip commit of the branch?
Or can the working directory still be the same as the previous tip commit of the branch, and, in order to check out the new tip commit of the current branch into the working directory, I need to run some additional git command?
The followings are two cases/examples with concrete git commands (
git push
andgit pull
), which made me think of and ask the above two parts' questions for arbitrary git commands that can add commits to a branch or to the current branch:git push
adds new commit(s) to a branch on the remote repository.Now suppose
- the remote repository isn't bare i.e. has a working directory, and
- right before pushing, the current branch of the remote repository is the branch to push to.
Questions:
in the remote repository, does
git push
move the branch name to point to the new tip commit?Or does the user of the remote repository have to run some additional git command to move it?
In the remote repository, does
git push
update the working directory to be the same as the new tip commit?I.e. in the remote repository, is the updated version (by
git push
) of the current branch ("current" beforegit push
) still the current branch aftergit push
? (note the definition of a current branch I gave at the beginning of the post: "the current branch is defined as the branch checked out into the working directory.")
My guess is no, if I understand correctly from Version Control with Git by Loeliger 2ed which says:
Recall that the git push command does not check out files in the receiving repository. It simply transfers objects from the source repository to the receiving repository and then updates the corresponding refs on the receiving end.
In a bare repository, this behavior is all that can be expected, because there is no working directory that might be updated by checked out files. That’s good. However, in a development repository that is the recipient of a push operation, it can later cause confusion to anyone using the development repository.
The push operation can update the repository state, including the HEAD commit. That is, even though the developer at the remote end has done nothing, the branch refs and HEAD might change, becoming out of sync with the checked out files and index.
A developer who is actively working in a repository into which an asynchronous push happens will not see the push. But a subsequent commit by that developer will occur on an unexpected HEAD, creating an odd history. A forced push will lose pushed commits from the other developer. The developer at that repository also may find herself unable to reconcile her history with either an upstream repository or a downstream clone because they are no longer simple fast-forwards as they should be. And she won’t know why: the repository has silently changed out from underneath her. Cats and dogs will live together. It’ll be bad.
In
git pull
command, the merge step merges the newly fetched remote tracking branch into the current branch. If I am correct, the merge step ofgit pull
also advances the banch name of the branch to point to the new top commit on the branch.Question:
In the local repository, does
git pull
update the working directory to be the same as the new fetched tip commit?I.e. In the local repository, is the updated version (by
git pull
) of the current branch ("current" beforegit pull
) still the current branch aftergit pull
? (note the definition of a current branch I gave at the beginning of the post: "the current branch is defined as the branch checked out into the working directory.")
I have asked the question about
git pull
at Does git pull check out the current branch after its merge/rebase step?. From my understanding, what Joseph's reply means isgit pull
checks out the new fetched tip commit into the working directory of the local repository, and I don't need to run additional git commands to check it out. torek's reply is great, but it will be a while until I could understand it.