I have difficulty understanding/verifying the following line from Pro Git:
It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them. In other words, in this case, you don’t have a new serverfix branch — you have only an origin/serverfix pointer that you can’t modify.
I tried the following for a repository.
git clone git://git.bugseng.com/ppl/ppl.git
cd ppl
I removed folder ppl/src/
from my local machine, and made a copy of pre-existing folder ppl/m4/
into ppl/m4 - Copy
just so as to locally modify/edit this folder a bit.
Then, I ran git fetch origin
just to (I thought) set myself up to be in the situation mentioned in the book's quote above. I (of course) do not have any write access to this online repository.
Now, I am easily able to do git add .
and git commit
without any errors. Output of git status
is:
:~/ppl$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
all as expected. That is, I have been able to make a commit and edit the branch locally. I am unable to reconcile this with the quote from the book which states that I would not have local editable copies of them.
CodePudding user response:
Notice that the phrases you quoted refer to a serverfix
branch — not to master
.
In the case of master
(or whatever the remote HEAD is), when you clone you are given a local copy automatically; thus there was no need for you to make one.
But if there had been any other branches, clone
does not make local copies of those for you, and a mere fetch
would not have given you local copies of them either. You might have remote-tracking branches such as origin/someBranch
, but if you wanted to edit someBranch
, you'd have to create it first.
CodePudding user response:
The Pro Git reference is saying that a git fetch
does not actually checkout a local copy of the remote branch you have fetched. It is merely a pointer to allow git locally to know the branch exists and allow you to check it out. To test this, clone a copy of your remote repository. Next create a branch on your remote fetch-branch-test
. Run git checkout fetch-branch-test
on your local repo, and you will get an error saying git can't find the branch. Run git fetch
locally, then git branch
. You will see that there is still no local copy of fetch-branch-test
. Finally, run git checkout fetch-branch-test
again, and you will notice you now have a local copy of the remote branch. This is because git fetch added a local pointer of the new remote branch, but did not add a local copy of it until you ran git checkout
.