ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Chapter 6: GIT branching and merging

prodevelopertutorial August 3, 2020

In this chapter we shall learn about git branching and merging related commands.

git branch
git checkout
git merge
git mergetool
git stash
git worktree

 

git branch

1. git branch is an option, where in you create a separate branch in the from the master branch.

2. You might be developing a feature and that you don’t want to continue in the master branch, till the testing has been completed.

3. In that case, you will create a separate branch and work in that branch, once the branch is stable, then you can proceed to merge with the master branch.

We shall see the list of options that “git branch” is having through series of examples:

1. To create a new branch, use “git branch <branch_name>”, then we check the list of branches by using “git branch” command.

git branch

2. To delete a branch, use “git branch -d <branch_name>”

3. To rename a branch, use “git branch -m <old_branch_name> <new_branch_name>”

git checkout

In the above example, we saw how to create a branch. But once you create a branch, how to use the new branch ?

1. To use the new branch use “git checkout”

git checkout

One you have switched to new branch, you can start making your changes.

2. To switch to main branch, use “git checkout -m master”

3. To create and switch to a branch, use “git checkout -b <branch_name> ”

git merge

Once you have checked-out to a new branch and made the changes, how do you merge those changes to a new branch?

To do that, use “git merge” option.

Before we merge, we shall add below line to myProg.c file in the new branch.

printf(“New line added from new_feature branch”);

Once the changes have been completed, you need to add the changes by using “git add .” and commit the changes by using “git commit -m “new feature” ” command.

This will commit the changes to the “new_feature” branch.

Now that we have made the changes and committed the changes to the new branch, how to merge them to the master branch?

To do that, it is a 2 step process:

Step 1: Checkout to master branch by using “git checkout -m master”
Step 2: Use “git merge <branch_name>” to merge that branch to the master branch

Example:

I have merged “new_feature” branch to “master” branch.

git merge

git mergetool

Sometime there will be merge conflicts, at that time you need to use “git mergetool” to resolve the conflict.

git stash

When to use git stash, a generic scenario

Suppose you are working on branch A and you need to work on branch B, before being commit to branch A. Then you can stash your changes on A, then checkout B and continue with the work.

Then you can checkout branch A and apply the stash.

So generally we use “git stash” to save un-committed changes in a stash, the changes will be removed from working tree.

How to check the list of stash ?

“git stash list” is the command used to get the list of stash

Example:

git stash list

stash@{0}: my stash 1
stash@{1}: my stash 2
stash@{2}: my stash 3

How to create a stash?

Use the command “git stash save “my message” “.

Example:
git stash save “my stash 4”

How to delete a stash?

Use the command “git stash drop <stash-name> ”

How to apply a stash?

Use the command “git stash apply <stash-name>”

Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT