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 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.
Run the following commands to tracked the untracked file and check the status of the git repository again.
$ 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.
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.
$ git status
The following output will appear executing the above commands.
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 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.
Run the following command to commit the previous task with the commit message and check the status again.
$ 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.
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.
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 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.
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.
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.