DEV Community

lous code
lous code

Posted on

Fetching a Remote Branch to the Local Repository in Git

To bring a branch from the remote (upstream) repository to the local one, you can use the following commands in Git:

**1. Fetching updates from the remote repository

**
First, fetch all branches and updates from the remote repository without merging them into the current branch:

git fetch origin

Enter fullscreen mode Exit fullscreen mode

This command fetches all updates from the remote origin repository but does not integrate them into the local repository.

**2. Checking remote branches

**
After executing fetch, you can view the list of available branches in the remote repository using:

git branch -r
Enter fullscreen mode Exit fullscreen mode

It will show you the branches stored in the remote repository (such as origin/branch-name).

**3. Create a local branch and track the remote branch

**
If you want to work on a specific branch of the remote repository, you can create a local branch and track it:

git checkout -b branch-name origin/branch-name

Enter fullscreen mode Exit fullscreen mode

Or using the latest version of Git:

git switch --track origin/branch-name

Enter fullscreen mode Exit fullscreen mode

*This creates a local copy of the remote branch and identifies it as the local master branch.
*

4. Switch to the new branch

If the branch was previously fetched, you can switch to it directly using:

git checkout branch-name

Enter fullscreen mode Exit fullscreen mode

Or using:

git switch branch-name

Enter fullscreen mode Exit fullscreen mode
  1. Update the local branch with the latest changes After fetching the branch, if you need to update it with the latest changes from the remote repository, you can use:
git pull origin branch-name

Enter fullscreen mode Exit fullscreen mode

This way, you will have an updated branch of the remote repository in your local environment, and you can work on it as you like. 🚀

Top comments (0)