JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Merge remote branches into local branches in Git

Author:JIYIK Last Updated:2025/03/31 Views:

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


Merge remote branches into local branches in Git by cloning the remote repository and updating the changes locally

We will clone a remote repository that contains two branches, namely mainand gh-pages.

We will then create a local branch test and update the remote branch gh-pages. After this, we will pull gh-pagesthe remote changes from and merge them into 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 enter 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'll create a local branch testing.

git branch test

We will then switch to our local branch for testing.

git checkout test

Output:

Switched to branch 'test.'

Before merging into the testing branch, we continue to update our by running this command gh-pages.

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 updating, we merge the remote branch gh-pagesfor testing.

git merge origin/gh-pages

Output (if gh-pagesthere are any changes in branch):

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

Output (if branch gh-pageshas no changes):

Already up to date.

In Git, merge remote branches into local branches by tracking and pulling changes from remote repositories

We will now clone a remote repository that contains two branches, masterand gh-pages. We will then create a local branch another-branchand set it up to track any and pull mainchanges made on the remote 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 go into the project folder and list the available branches via:

<!-- 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'

We then set up a branch to another-branchtrack mainchanges from the remote repository.

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 mainchanges from the remote branch.

git pull

Output:

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

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Close the Git commit editor on Windows

Publish Date:2025/03/31 Views:62 Category:Git

In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t

Changing drives in Git Bash

Publish Date:2025/03/30 Views:57 Category:Git

This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to

Adding a remote branch in Git

Publish Date:2025/03/30 Views:142 Category:Git

Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi

Synchronize your local repository with a remote repository in Git

Publish Date:2025/03/30 Views:92 Category:Git

This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito

Creating a remote repository from a local repository in Git

Publish Date:2025/03/30 Views:106 Category:Git

This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository

Removing the upstream repository in Git

Publish Date:2025/03/30 Views:177 Category:Git

This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the

Git remote add SSH

Publish Date:2025/03/30 Views:53 Category:Git

In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se

Differences between Git Reset, Revert and Checkout commands

Publish Date:2025/03/30 Views:186 Category:Git

This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with

Git revert local commits

Publish Date:2025/03/30 Views:85 Category:Git

When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial