If you are working in development for a long time, you should know what version control system and a GIT is. In this post, we will see what a GIT branch is and how to merge a branch to other branch or a master in git repository.
What is a GIT branch?
GIT store all the files and data in a repository. This repository is a centralized location where we store all data by versioning controlling them. A branch is nothing but a complete replica of a repository which effectively forks within your repository. At the time of new branch creation, the branch is similar to the repository main branch(many times it will be a master branch) or from which branch it is forked.
Once the branch is created we can start working on a new branch with out modifying master branch content this way any bugs we introduce is with in your working branch. In this post, we will see how to merge a branch to a master branch after we are confident about the changes.
In this post, we will use two branches
first_branch: This is actual changes which we do and need to merge these changes to another branch.
second_branch: This is the branch where we merge our first_branch changes.
Steps to merge a git branch to master
Step1: Check out to an existing branch
First, check what branches are available to you.
git branch
Example:
surendra@linuxnix:~/code/sh/gittraining$ git branch first_branch second_branch * master
From the above output, the * with a name indicates your present branch. So the current branch is a master.
Checkout into first_branch branch.
git checkout branchname
Example:
surendra@linuxnix:~/code/sh/gittraining$ git checkout first_branch Switched to branch 'first_branch' surendra@linuxnix:~/code/sh/gittraining$ git branch * first_branch second_branch master
Step2: Commit and push changes to a branch
Update your files and commit those changes locally and then push those changes to sync remote repository.
git commit -am “First commit.”
git push
Example:
Check if any changes already exists or not
surendra@linuxnix:~/code/sh/gittraining$ git status On branch first_branch Your branch is up-to-date with 'origin/first_branch'. nothing to commit, working directory clean
Edit your files and check status.
surendra@linuxnix:~/code/sh/gittraining$ vi abc.sh
surendra@linuxnix:~/code/sh/gittraining$ git status
On branch first_branch
Your branch is up-to-date with ‘origin/first_branch’.
Changes are not staged for commit:
(use “git add <file>…” to update what will be committed)
(use “git checkout — <file>…” to discard changes in working directory)
modified: abc.sh
no changes added to commit (use “git add” and/or “git commit -a”)
Commit changes
surendra@linuxnix:~/code/sh/gittraining$ git commit -am “Updated abc.sh script”
[first_branch f564909] Updated abc.sh script
1 file changed, 1 insertion(+)
surendra@linuxnix:~/code/sh/gittraining$ git push
Username for ‘https://github.com’: linuxnix
Password for ‘https://linuxnix@github.com’:
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 342 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/linuxnix/gittraining.git
e4a48b6..f564909 first_branch -> first_branch
Step3: Checkout to the desired branch
Example:
surendra@linuxnix:~/code/sh/gittraining$ git checkout second_branch Switched to branch 'second_branch'
Step4: Pull changes to local directory
surendra@linuxnix:~/code/sh/gittraining$ git pull Already up-to-date.
Step5: Merge changes to present branch.
It is a syncing your first branch data with your second branch.
git merge desired_branch_to_merge
Example:
surendra@linuxnix:~/code/sh/gittraining$ git merge first_branch Updating 075ca41..f564909 Fast-forward abc.sh | 2 ++ 1 file changed, 2 insertions(+)
Check status if the local changes are synced to remote branch or not.
surendra@linuxnix:~/code/sh/gittraining$ git status On branch second_branch Your branch is ahead of 'origin/second_branch' by 2 commits. (use "git push" to publish your local commits) nothing to commit, working directory clean
If remote second_branch and local second_branch are not synced, sync them by using git push command.
surendra@linuxnix:~/code/sh/gittraining$ git push Username for 'https://github.com': linuxnix Password for 'https://linuxnix@github.com': Total 0 (delta 0), reused 0 (delta 0) To https://github.com/linuxnix/gittraining.git 075ca41..f564909 second_branch -> second_branch
Hope this helps in understanding How to merge branches. In our next post, we will see how to delete a git branch locally and remotely.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018