Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching a Git Branch Under a Different Name

I want to know if I can fetch a Git branch from a remote repo, but store it under a different name in my local repo. It happens to be the master branch of the remote repo, but I'd like it to show up as, say, featureX.

I know it's possible to push <remote> local_name:remote_name (on the initial push) to change the name that will show up in remote. Basically I want to do that in reverse, without the other repo having to rename its master branch. Any configuration options required to make the future fetches/pulls behave correctly with the featureX name would be appreciated as well.

like image 402
automaton Avatar asked Feb 28 '17 16:02

automaton


People also ask

How do I checkout a branch with a different name?

To be precise, renaming a remote branch is not direct – you have to delete the old remote branch name and then push a new branch name to the repo. Step 2: Reset the upstream branch to the name of your new local branch by running git push origin -u new-branch-name .

Is git fetch branch specific?

Git fetch commands and options Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository. Same as the above command, but only fetch the specified branch.

How do I fetch a remote branch?

Use git branch -a (both local and remote branches) or git branch -r (only remote branches) to see all the remotes and their branches. You can then do a git checkout -t remotes/repo/branch to the remote and create a local branch. There is also a git-ls-remote command to see all the refs and tags for that remote.


1 Answers

git fetch <remote>
git checkout -b <local-branch> <remote>/<remote-branch>

Alternatively if you already have <local-branch> and just want it to track <remote-branch>:

git checkout <local-branch>
git branch -u <remote>/<remote-branch>
like image 149
Pockets Avatar answered Sep 20 '22 18:09

Pockets