stackabuse.com Open in urlscan Pro
2606:4700:20::681a:826  Public Scan

URL: https://stackabuse.com/git-merge-branch-into-master/
Submission: On January 10 via manual from US — Scanned from DE

Form analysis 1 forms found in the DOM

<form class="sm:flex"><label for="email-address" class="sr-only">Email address</label><input type="email" id="email-address" autocomplete="email" required=""
    class="w-full border-white px-3 py-3 placeholder-gray-500 focus:outline-none focus:ring-1 focus:ring-offset-1 focus:ring-offset-gray-500 focus:ring-white rounded-md" placeholder="Enter your email" value=""><button type="submit"
    class="mt-3 w-full flex items-center justify-center px-5 py-3 border border-transparent shadow text-base font-medium rounded-md text-white hover:bg-orange-400 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-orange-700 focus:ring-white sm:mt-0 sm:ml-3 sm:w-auto sm:flex-shrink-0"
    style="background-color:#f16334">Sign Up</button></form>

Text Content

WE VALUE YOUR PRIVACY

We and our partners store and/or access information on a device, such as cookies
and process personal data, such as unique identifiers and standard information
sent by a device for personalised ads and content, ad and content measurement,
and audience insights, as well as to develop and improve products.
With your permission we and our partners may use precise geolocation data and
identification through device scanning. You may click to consent to our and our
partners’ processing as described above. Alternatively you may access more
detailed information and change your preferences before consenting or to refuse
consenting.
Please note that some processing of your personal data may not require your
consent, but you have a right to object to such processing. Your preferences
will apply to this website only. You can change your preferences at any time by
returning to this site or visit our privacy policy.
MORE OPTIONSAGREE

Stack Abuse
PythonJavaScriptJavaWrite with Us


PythonJavaScriptJava
HomeArticles


GIT: MERGE BRANCH INTO MASTER

Jacob Stopak


Note: In 2020, Git (alongside major repository hosting platforms like GitHub,
GitLab, etc.) adopted a change in terminology, and the default branch name is
main, due to the negative conotations the word master may entail. Many projects
haven't migrated or renamed their main branches, so for the foreseeable future -
the terms master and main will likely be used interchangeably.

One of Git's most powerful features is the ability to easily create and merge
branches. Git's distributed nature encourages users to create new branches often
and to merge them regularly as a part of the development process. This
fundamentally improves the development workflow for most projects by encouraging
smaller, more focused, granular commits.

In legacy Version Control Systems (like CVS) the difficulty of merging
restricted it to advanced users. Other modern but centralized version control
systems like Subversion require commits to be made to a central repository, so a
nimble workflow with local branching and merging is atypical.

A commonly used branching workflow in Git is to create a new code branch for
each new feature, bug fix, or enhancement. These are called Feature Branches.
Each branch compartmentalizes the commits related to a particular feature. Once
the new feature is complete – i.e. a set of changes has been committed on the
feature branch – it is ready to be merged back into the master branch (or other
main code line branch depending on the workflow in use).

Note: Feature branching isn't the only branching workflow you can go with, but
it's a widely adopted one.



The git branch command is used to list all existing branches in a repository. An
asterisk will appear next to the currently active branch.

$ git branch
* master


To create a new branch, we can use the git branch new-branch command. This will
create a new branch mirroring the commits on the currently active branch:

$ git branch new-branch
$ git branch
* master
new-branch


As a brief aside, keep in mind that behind the scenes Git does not actually
create a new set of commits to represent the new branch. In Git, a branch is
really just a tag. It is a label that we can use to reference a particular
string of commits. It would be inefficient to duplicate a set of commits behind
the scenes, so Git allows us to create multiple diverging sets of commits from a
single base.

At this point we have created a new branch, but are still located on the source
branch. To start working on the new branch we first need to run the command git
checkout new-branch. This will change the active branch to the new branch:

$ git checkout new-branch
Switched to branch ‘new-branch'
$ git branch
master
* new-branch


At this point, commits can be made on the new branch to implement the new
feature. Once the feature is complete, the branch can be merged back into the
main code branch.

First we run git checkout master to change the active branch back to the master
branch. Then we run the command git merge new-branch to merge the new feature
into the master branch.

Note: git merge merges the specified branch into the currently active branch. So
we need to be on the branch that we are merging into.


FREE EBOOK: GIT ESSENTIALS

Check out our hands-on, practical guide to learning Git, with best-practices,
industry-accepted standards, and included cheat sheet. Stop Googling Git
commands and actually learn it!

Download the eBook  

If you're merging a new feature into the main branch, you first want to switch
to the main branch and then merge into it:

# ...develop some code...

$ git add –A
$ git commit –m "Some commit message"
$ git checkout master
Switched to branch 'master'
$ git merge new-branch


If all goes well then our job is done. The new feature commits now appear in the
main branch. However, it is possible that Git won't be able to complete the
merge due to a conflict change in the source branch. This is called a merge
conflict.

> If you'd like to read more about Merge Conflicts, how they can arise and how
> to resolve them - read our Git: Guide to Resolving Merge Conflicts!

To summarize, here are the commands to create a new branch, make some commits,
and merge it back into master:

$ git checkout master
$ git branch new-branch
$ git checkout new-branch

# ...develop some code...

$ git add –A
$ git commit –m "Some commit message"
$ git checkout master
$ git merge new-branch


ABOUT THE AUTHOR

This article was written by Jacob Stopak, a software consultant and developer
with passion for helping others improve their lives through code. Jacob is the
creator of Initial Commit - a site dedicated to helping curious developers learn
how their favorite programs are coded. Its featured project helps people learn
Git at the code level.


# git
Last Updated: January 3rd, 2022

Was this article helpful?




YOU MIGHT ALSO LIKE...

 * Git: Guide to Solving Merge Conflicts
 * Git: Undo Latest Local Commit
 * Git: An Introduction for Beginners
 * Git: Adding a Commit Message
 * Git: Stashing Uncommitted Changes


IMPROVE YOUR DEV SKILLS!

Get tutorials, guides, and dev jobs in your inbox.

Email addressSign Up

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Jacob StopakAuthor


Jacob Stopak is a software developer and creator of InitialCommit.io - a site
dedicated to teaching people how popular programs are coded. Its main project
helps people learn Git at the code level.





WANT A REMOTE JOB?


 * Backend Developer
   Remote Crew LDA 7 hours ago
   saasdjangomongodbpython
 * Back-End Developer
   Imaginary Cloud 12 hours ago
   node-jsvue-jsangular-jsandroid
 * Security Engineer - Infrastructure
   Plaid 2 days ago
   awssaaspythoncloud
 * Software Architect
   SemanticBits 3 days ago
   javascriptnode-jsreact-jsaws

  More Jobs
Jobs byHireRemote.io



PREPPING FOR AN INTERVIEW?


 * Improve your skills by solving one coding problem every day
 * Get the solutions the next morning via email
 * Practice on actual problems asked by top companies, like:


 

 

 

  Daily Coding Problem
MAKE CLARITY FROM DATA - QUICKLY LEARN DATA VISUALIZATION WITH PYTHON




Learn the landscape of Data Visualization tools in Python - work with Seaborn,
Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms -
understand your data and learn to draw conclusions from it.

Learn more  

TwitterGitHubFacebook

© 2013-2022 Stack Abuse. All rights reserved.

DisclosurePrivacyTerms