How to Merge a Remote Branch to a Local Branch in Git

Kevin Amayi Feb 02, 2024
  1. Merge a Remote Branch to a Local Branch in Git by Cloning the Remote Repository and Updating the Changes Locally
  2. Merge a Remote Branch to a Local Branch in Git by Tracking and Pulling Changes on the Remote Repository
How to Merge a Remote Branch to a Local Branch in Git

This tutorial will merge a remote git branch to a local one by cloning the remote repository and updating the changes locally.

Merge a Remote Branch to a Local Branch in Git by Cloning the Remote Repository and Updating the Changes Locally

We will clone a remote repository containing two branches, namely main and gh-pages.

Then, we will create a local branch test and update the remote branch gh-pages. After this, we will pull the remote changes in gh-pages and merge them to the test branch.

<!-- The command to use is -->
git clone <remote-repo-url>
    
<!-- From your terminal run -->
git clone https://github.com/KEVINAMAYI/AkanNameGenerator.git

Output:

Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), 2.38 MiB | 1.86 MiB/s, done.

Using the command below, we will get into the project folder and the list of available branches.

<!-- get into project folder -->
cd AkanNameGenerator

<!-- List branches available -->
git branch -a

Output:

<!-- The asterix indicates we are on branch main -->
* main
remotes/origin/HEAD -> origin/main
remotes/origin/gh-pages
remotes/origin/main

Next, we will create a local branch test.

git branch test

Then, we will switch to our local branch test.

git checkout test

Output:

Switched to branch 'test.'

We proceed to update our gh-pages before merging to the test branch by running this command.

git fetch origin gh-pages

Output:

remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 726 bytes | 363.00 KiB/s, done.
From https://github.com/KEVINAMAYI/AkanNameGenerator
* branch            gh-pages   -> FETCH_HEAD
4a458ff..4edc95b  gh-pages   -> origin/gh-pages

After the update, we merge our remote branch gh-pages to test.

git merge origin/gh-pages

Output (if the branch gh-pages has any changes):

Updating f25a425..4a458ff
Fast-forward
js/global.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

Output (if the branch gh-pages has no changes):

Already up to date.

Merge a Remote Branch to a Local Branch in Git by Tracking and Pulling Changes on the Remote Repository

We will now clone a remote repository containing two branches, master and gh-pages. Then, we will create a local branch another-branch and set it to track any and pull changes made on the remote main branch.

<!-- The command to use is -->
git clone <remote-repo-url>
    
<!-- From your terminal run -->
git clone https://github.com/KEVINAMAYI/AkanNameGenerator.git

Output:

Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), 2.38 MiB | 1.86 MiB/s, done.

Next, we need to get into the project folder and list the available branches by:

<!-- get into project folder -->
cd AkanNameGenerator

<!-- List branches available -->
git branch -a

Output:

<!-- The asterix indicates we are on branch main -->
* main
remotes/origin/HEAD -> origin/main
remotes/origin/gh-pages
remotes/origin/main

We will create a local branch, another-branch.

git branch another-branch

We will switch to our local branch another-branch.

git checkout another-branch

Output:

Switched to branch 'another-branch'

Then, we’ll set our branch to another-branch to track the remote main unit changes.

git branch --set-upstream-to=origin/main another-branch

Output:

Branch 'another-branch' set up to track remote branch 'main' from 'origin'.

We can now directly pull changes made on the remote branch main.

git pull

Output:

Merge made by the 'recursive' strategy.
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

Related Article - Git Branch

Related Article - Git Merge