Reputation: 351
I have a remote branch that my friend created, this branch does not exist in my local repo (not checked out).
I run git fetch
,then git checkout <myFriendBranch>
,
now i'm up to date with my friend branch, and I do not need to run git merge
or git pull
.
I wonder why?
For example, if there is a branch that we both working on, and exist on my local device,git fetch
alone won't update my working directory and I will need to run git merge
or git pull
.
Upvotes: 1
Views: 2895
Reputation: 141946
Let's start by explaining ow git store its information internally.
git
store its data inside the .git
folder. This folder contains information about local and remote branches.
Both local and remote branches are simply a pointer to a SHA1, and are stored inside the .git
folder as u can see below.
git fetch
?git fetch
is a porcelain command which sync the remote repository to your local repository. It simply downloading all the hunks
, the delta of content which does not exists yet on your machine into your .git
folder without making any modifications to your working directory, simply download and store the ocntent for later use.
git fetch
does not delete deteled remote branches so if you want to "clean" your local repository and remove the deleted branches use the --prune
flag.
adding --all
will fetch all the reomtes if you have more than a single one.
git fetch --all --prune
At this point the latest commits are only stored inside the .git
folder. If you wish to update your local workind dir you need to merge it. keep on understand to how and why.
git checkout
?git checkout <name>
is switching between branches. First git check to see if the branch exixts locally - if it was already checked out previously, if not if will search for it under the remotes and than will checkout it as a new local branch.
git pull
?git pull
is simply an alises for git fetch
+ git merge
.
git merge
will add the desired branch content into your current branch.
git pull origin master
for exampel will fetch the remote and will download the missing content as explaind above and than will merge (update) it into your local working directory.
Here is a detailed output of the git pull
command, you can see that git first fetch the content and than merge it back to your branch
Upvotes: 5
Reputation: 45659
It seems your question is about when you do have to merge or pull, vs. when you don't (and can instead just fetch and checkout). Since pull is basically "fetch and merge", this boils down to: when do I have to merge, and why?
You merge to incorporate changes (which, for example, might be changes fetched from a remote) into an existing branch.
In your first example, your friend created a branch, and you're just copying your friend's branch as it is; you're not incorporating their changes into any existing branch, but rather you're creating a new branch from their changes. So you don't need to merge anything. Instead, after fetch you create a new branch. (You happen to be using a shortcut to cause the branch to be created, because git recognizes your intent when you try to checkout a non-existing branch but exactly one remote tracking ref matches the given name.)
Upvotes: 1
Reputation: 83517
git fetch
downloads the changes from your friend's repository and creates a branch named origin/friends-branch
in your local repository. When you do git checkout friends-branch
, Git recognizes the remote tracking branch and automatically creates a new local branch with the name friends-branch
.
Upvotes: 2