I have a remote repository that has a lot of branches. Every time there is a new version, a new branch is created. How can I, when a new version is available, change the branch and pull the changes avoiding to clone it all again. I want to only download the new committs.
-
1`git fetch` does not "clone it all again". – matt Apr 16 '17 at 00:03
-
But what I want is to change the branch and sync it. So that I can use pull, push, etc. Not only fetch the commits – Mati Halperin Apr 16 '17 at 00:07
-
1Possible duplicate of [How to check out a remote Git branch?](http://stackoverflow.com/questions/1783405/how-to-check-out-a-remote-git-branch) – matt Apr 16 '17 at 00:28
1 Answers
Executive Summery:
git fetch origin
git checkout new-version`
Where origin
is the remote name and new-version
is the branch name.
Details:
The documentation for git fetch
says:
Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated.
This basically means that it goes and downloads the history of the repository from the remote. However, a repository can have multiple remotes, so git fetch
can fetch either one or more of them using the --all
flag.
By default, any tag that points into the histories being fetched is also fetched; the effect is to fetch tags that point at branches that you are interested in. This default behavior can be changed by using the --tags or --no-tags options... By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.
Meaning that if you had, for example, branches A, B & C on the remote, and on local you were working on branch C. If you git fetch origin C
, by default, this will also fetch the tags pointing to that branch, but it will not fetch those related to other branches, for instance A and B.
If you want to fetch all tags, you just add the --tags
to the command.
Finally,
When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch.
Resources:
git fetch
documentation, it's all well explained.- The Git Pro book, chapter 3.5 goes through this.
- You may also want to look at this related question as well: What is the difference between 'git pull' and 'git fetch'?

- 1
- 1

- 11,521
- 1
- 20
- 31
-
Does this work with tags? Also, does this only fetch the commits needed to get up-to-date or all of the new ones? – Mati Halperin Apr 16 '17 at 00:13
-
You can think of _tags_ as branches, so yes, it does work with tags provided that you add the `--tags` to the `fetch` command. This fetches the changes for the whole repository, but you can also fetch only specific branches if you like by adding the name of the branch. i.e `git fetch origin branch-x` – Ayman Nedjmeddine Apr 16 '17 at 00:19
-
Usually you don't even need `--tags`: Git has some magic (I think "overly magic"—it's nearly inexplicable in terms of precisely what happens when) code to try to guess which tags to copy during normal `git fetch`, that mostly Just Works to get you the tags too. But use `git fetch --tags` if for some reason a tag is missing, and you will get it. – torek Apr 16 '17 at 00:21
-
@Ayman Nedjmeddine and does this work to fetch the changes from a tag that doesn't exist in the local repository because it's newer? – Mati Halperin Apr 16 '17 at 00:22
-
@Ayman Nedjmeddine Last cuestion, can I then use git pull and the new tag will get pulled, and not the old one? – Mati Halperin Apr 16 '17 at 01:09
-
@MatiHalperin, I've detailed my answer more, I hope this helps better. Please feel free to ask if something's not clear, otherwise please mark my answer ;) – Ayman Nedjmeddine Apr 16 '17 at 06:15
-
That is an explanation of how fetch works, but it is not the solution I need. I have a repository which I cloned from a remote. I used: "git clone -b tag-x remote-repo". Now I want to change "tag-x" to "tag-y" and "git pull" the changes. I hope you understand what I need – Mati Halperin Apr 16 '17 at 13:40
-
That means you still have not understood. I have explained what `fetch` does and linked to the difference between it and `pull`. Anyways: `git clone -b tag-x remote` `.... do work ....` `git fetch origin` `git checkout tag-y` `git merge origin/tag-y` `... keep working on tag-y ...` OR `git clone -b tag-x remote` `.... do work ....` `git checkout tag-y` `git pull origin tag-y` `... keep working on tag-y ...` That is what you want to do right? – Ayman Nedjmeddine Apr 16 '17 at 16:32
-
Yes, that is exactly what I want. But, if possible, use only `git pull` instead of `git pull origin tag-y` – Mati Halperin Apr 16 '17 at 20:05