Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cleanly get/copy a remote git branch to local repository

Tags:

git

I want an exact "copy" of a remote branch "copied" to a specific local branch.

Say, for example, a team member has created an experimental feature, which he has checked into a branch called experiment on the remote repository. I want to be able to checkout a new branch on my local repository, and "copy" the experiment branch, as-is, to my newly checked out branch.

I don't want to merge it with my code - I want to completely overwrite my code so that I can have a clean look at what he's done on the "experiment" branch.

How would one "get" (fetch/pull/whatever...) a remote branch that someone else has committed to the remote repository to your own local repository, in a way that does not try and perform a merge on your own local code?

like image 231
Stanley Avatar asked Jul 06 '12 05:07

Stanley


People also ask

How do I get a local copy of a remote branch?

just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

How do I pull changes from remote repo to local repository?

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.


2 Answers

If you don't care about merging:

git reset --hard <remote>/<branch_name> 

This will exactly do what you want: no merging, no rebasing, simply put the local branch to exactly the same state as the remote is.

In your case however, you don't need that. You want to create a new local branch that has the same state as the remote, so specify the remote branch when creating the local one with git checkout:

git checkout -b <my_new_branch> <remote>/<branch_name> 

If you want to use the same name locally, you can shorten that to:

git checkout <branch_name> 

You would use git reset --hard if you already have a local branch (for example with some changes in it), discard any modifications that you made and instead take the exact version of the remote branch.

To be sure that you have the latest state of the remote branch, use git fetch <remote> before either checking out or resetting.

like image 86
eckes Avatar answered Sep 29 '22 07:09

eckes


I don’t have enough rep to comment, but I’d like to add an example for those still struggling. In my example, I’m using Github as my remote and copying the branch to my Linux server environment/terminal.

First, you’ll want to run git fetch To make sure you’re up to date.

Using the example of

git checkout -b <my_new_branch> <remote>/<branch_name>

With a remote branch named “picklerick”

I ran

git checkout -b picklerick remotes/origin/picklerick

I probably could have just used remote/picklerick for my remote path, but this worked for me.

like image 45
paulvantuyl Avatar answered Sep 29 '22 08:09

paulvantuyl