A git repository contains one or more branches to manage the code efficiently. Sometimes, the git users need to work with the multiple branches simultaneously and require to switch from one branch to another branch with the changes made in the previous branch to the current branch before commit. This task can be done by using multiple ways in git. The stash command can be used from the terminal and merge commands from the GitHub Desktop to pull the changes from one branch to another branch in this tutorial.

Prerequisites

1. Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

2. Create a GitHub account

You will require to create a GitHub account to check the looks of the remote repository after publishing the change of the local repository.

3. Create a local repository

You have to create a local repository to check the commands used in this tutorial.

Use of stash command

The local repository named PHP2 has been used in this tutorial to show how to pull changes from one branch to another branch of the repository. Create a file named index.html in the repository. Open the terminal and go to the repository folder. Run the following commands to check the branch list, switch to the master branch, and display the status of the branch.

$ git branch


$ git checkout master


$ git status

The following output will appear after executing the above command. The output shows that the repository contains two branches, and the main branch was active initially. After switching to the master branch, the output of the status shows that the current branch is active and the index.html file is untracked.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-01.png" data-lazy- height="364" src="data:image/svg xml,” width=”1039″>

Run the following commands to tracked the untracked file and check the status of the git repository again.

$ git add index.html


$ git status

The following output will appear after executing the above commands. The output of the status shows that the index.html file is added to the repository but needs to commit.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-02.png" data-lazy- height="237" src="data:image/svg xml,” width=”1039″>

Run the following commands and check the status of the git repository again. `git stash` command works like the `git commit` command. The working directory of the repository will be cleaned after executing this command.

The following output will appear executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-03.png" data-lazy- height="189" src="data:image/svg xml,” width=”1039″>

After running the stash command for a branch, if the git user wants to pull the branch’s changes to another branch, it can be done easily by using the `git stash pop` command that works like the `git merge` command. Run the following commands to switch into the main branch and pull the changes from the master branch to the main branch.

$ git checkout main


$ git stash pop

The following output will appear after executing the above command that shows that the current branch is main and the index.html file is added in this branch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-04.png" data-lazy- height="237" src="data:image/svg xml,” width=”1039″>

Run the following command to commit the previous task with the commit message and check the status again.

$ git commit -m “index file added”


$ git status

The following output will appear after executing the above commands. The output of the status shows that the working tree is clean now after commit.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-05.png" data-lazy- height="193" src="data:image/svg xml,” width=”1039″>

If the local repository changes are pushed to the remote repository, and the remote repository is opened from github.com, then the repository will be looked like the following image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-06.png" data-lazy- height="581" src="data:image/svg xml,” width=”1039″>

Use of merge command

This part of this tutorial shows another way to pull the changes from one branch to another branch. The local repository named read-file has used here. Create a file named read.php in the repository location. Run the following commands to switch into the master branch, add the read.php file and commit the task. The branch command will display the branch list. The checkout command will switch the branch to master. The status command will display the current status of the branch. The add command will add the untracked file in the repository. The commit command will confirm the task.

$ git branch


$ git checkout master


$ git status


$ git add read.php


$ git commit -m “Read script is added”

The following output will appear after executing the above commands. The read.php file has been added to the master branch of the repository, and the task has also been committed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-07.png" data-lazy- height="404" src="data:image/svg xml,” width=”1039″>

After completing the above task, if the changes done in the master branch requires transfer into the main branch, then the merge command can be used to perform this task. The merge command can be used from the terminal or by using the GitHub Desktop application. The way of using GitHub Desktop to merge branches has shown in this part of the tutorial. Open the local repository in GitHub Desktop. Open the branch list dialog box by clicking the Branches list from the View menu and select the main branch as the active branch. Now, click on the Merge into the current branch… from the Branch menu. Select the master branch from the dialog box and click on the Merge master into the main button to pull the changes of the master branch into the main branch. You can delete the master branch after completing the merge operation if the branch does not require the repository.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/pull-changes-between-branches-Git-08.png" data-lazy- height="718" src="data:image/svg xml,” width=”697″>

Conclusion

Two different ways to pull the changes from one branch to another branch have been shown in this tutorial using two demo local repositories. The stash and merge commands of git have been used here to do the task. The GitHub Desktop has been used in this tutorial to show the way of merging branches graphically.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/channel-logo-150×150.jpg609fd21dab8d4.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.