Git is a version control system that has been around for quite some time, but it’s still not in the mainstream. Today we’re going to show you how to quickly and easily merge two branches of your project into one so that you can share it with others. Let’s get started!.
“git merge two branches” is a command-line tool that allows users to combine the changes of two different branches. This will help with merging code from one branch into another.
It’s been a hassle to combine applications into a single file, and you’re not alone in this. But fear not: the git merge command may assist.
In this article, you’ll learn how to utilize the git merge command to combine branches with GitHub, one of the most widely used project management applications.
Ready? It’s time to get right in!
Prerequisites
This will be an interactive presentation. If you want to follow along, make sure you have these items:
- Git 2.25.1 is used in this lesson.
- Account on GitHub
- This article utilizes Ubuntu 20.04, but any OS with Git installed will work.
Getting a Git Project Started
You must first create a Git project before merging branches. But first, build an example project that you’ll eventually submit to a GitHub repository.
1. Open your terminal and type the instructions below to create a directory called /git-demo (which denotes the home directory) and browse to it.
mkdir git-demo && cd git-demo
2. Next, open your favorite code editor, create a file called /git-demo/index.html, and paste the code below into it.
The code produces a “Let’s get started with git merge” message when you open the index.html file in a web browser.
<html lang=”en”> <head> <meta charset=”UTF-8″> <meta http-equiv=”X-UA-Compatible” content=”IE=edge”> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Document</title> </head> <body> <h4>Let’s get started with git merge</h4> </body> </html>
3. To create a Git project, use the command below. The program starts a new Git repository and creates a.git folder in the root directory of your project.
The git init tool may either convert an unversioned project to a Git repository or create a new/empty one.
Creating a new Git repository from scratch
4. Verify that the.git folder exists in your project’s root directory by running the command below.
The command below displays all files in the working directory, including hidden ones.
To ensure that the GIT repository exists, list all directory files, including hidden ones.
5. To stage your code, use the git add command below. The following command moves pending changes from the working directory to the staging directory. No console output is produced if the command is successful.
6. Verify that you’ve staged the code using the git status command. The modifications to be committed are visible in the snapshot below, indicating that the index.html file has been staged. status of git
Checking to see whether the code is ready to be committed
7. Now execute the command below to save any changes to the code (index.html) to the master branch through git commit, with the message “first commit” given as the -m message argument for documentation.
“first commit,” git commit -m
Changing the code and committing it (index.html)
If this is your first time using Git on your machine, use the following instructions to configure your name and email:
—global user.email “[email protected]” git config
—global user.name “Your Name” git config
You may want to look at your commit history. If that’s the case, use the git log command to get a list of all the commits you’ve made in your repository thus far.
The Git log is listed.
8. Finally, execute the git command below to list all branches in your repository and make sure the master branch is present.
In recent Git versions, the default branch is usually called main rather than master.
All branches are listed.
New Git Branch Creation
You’ll establish another branch to record changes you’ll make in the index.html page now that you’ve set up Git and made your first commit.
1. Replace the contents of index.html with the code below. The code below, unlike the previous version of the index.html file, includes username and password input areas.
<html lang=”en”> <head> <meta charset=”UTF-8″> <meta http-equiv=”X-UA-Compatible” content=”IE=edge”> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Document</title> </head> <body> <h4>Let’s get started with git merge</h4> <form action=””> <input type=”text” name=”username” /> <input type=”password” name=”password” /> </form> </body> </html>
2. Rerun the git status command to determine whether your repository has changed.
The result below demonstrates that the index.html file has been updated and is unstaged since you made modifications to it.
Examining Repository Changes
3. Use the following git checkout command to establish a New Division called “form” (-b form) and swap the current branch from master to form. You may give your New Division whatever name you choose.
Related: [Step-by-Step] How to Check Out a Remote Git Branch
New Division
4. Now execute the git branch command, and you should see two branches on your repository this time (form and master).
The form branch is shown by the asterisk (*) symbol below, indicating that the current working branch is the form branch.
The asterisk (*) marks the start of the form branch.
5. Finally, use the commands below to stage and commit your modifications to the form branch from the index.html file.
You’ll have two branches with two distinct copies of the same file after the commands are finished (index.html). The index.html file’s original code is in the master branch, while the index.html file’s most recent modifications are in the form branch.
## Add index.html to the git repository. ## Save the changes. “Added a form file” git commit -m
Changes to the “form” branch are being committed.
Locally merging Git branches
Now that you have many branches in your GitHub repository, it’s time to merge them. There are many methods for merging branches; in this tutorial, you will learn how to merge branches locally.
1. Switch to the master branch using the git checkout command below, since you’ll be updating it with the code from the form branch.
2. To merge your target branch (form) with the current branch, use the procedures below (master).
Using the git merge command to combine branches.
3. Double-check that the changes were merged by checking that you’re on the master branch and that the content has changed.
# View output cat index.html # Verify Branch git branch
After a Git merge, checking the index.html content.
Taking Down Git Branches
It’s possible that you’re adding new features to a branch with commit messages you don’t need to preserve. Squash these commit messages together into a single commit message. When accepting a merge request, squashing allows you to clean up a branch’s commit history.
You’ll make modifications to the index.html file to show the impact of squashing, then stage and commit each change.
To switch to the form branch, use the command below.
2. After that, add a form heading to the index.html file.
<html lang=”en”> <head> <meta charset=”UTF-8″> <meta http-equiv=”X-UA-Compatible” content=”IE=edge”> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Document</title> </head> <body> <h4>Let’s get started with git merge</h4> <form action=””> <!– Added a form header –> <h4>Enter account details</h4> <input type=”text” name=”username” /> <input type=”password” name=”password” /> <button>Submit</button> </form> </body> </html>
3. Execute the following commands to stage and commit the changes with a message (“Added a header to form”).
# Put the modifications in order. add index.html to git # Save the changes. “Added a header to the form” git commit -m
Filling the index.html file with content
4. Now, add an extra input field shown below under the form header (<h4>Enter account details</h4>) in the index.html file.
<html lang=”en”> <head> <meta charset=”UTF-8″> <meta http-equiv=”X-UA-Compatible” content=”IE=edge”> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Document</title> </head> <body> <h4>Let’s get started with git merge</h4> <form action=””> <!– Added a form header –> <h4>Enter account details</h4> <input type=”text” name=”fullname” /> <input type=”text” name=”username” /> <input type=”password” name=”password” /> <button>Submit</button> </form> </body> </html>
5. As in step three, stage and commit the changes, but update the commit message.
# Put the modifications in order. add index.html to git # Save the changes. “Added an additional input field to the form” git commit -m
Adding more information to index.html
6. Add a paragraph (<p>Already a user?</p>) under the form block, as shown below in the index.html file.
<html lang=”en”> <head> <meta charset=”UTF-8″> <meta http-equiv=”X-UA-Compatible” content=”IE=edge”> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> <title>Document</title> </head> <body> <h4>Let’s get started with git merge</h4> <form action=””> <!– Added a form header –> <h4>Enter account details</h4> <input type=”text” name=”fullname” /> <input type=”text” name=”username” /> <input type=”password” name=”password” /> <button>Submit</button> </form> <!– Add a paragraph –> <p>Already a user?</p> </body> </html>
7. Stage the changes and commit them with a new commit note (“Added a paragraph to file”).
# Put the modifications in order. add index.html to git # Save the changes. “Added a paragraph to the file” git commit -m
Adding a final commit to show a range of commit messages
8. Open Git in interactive mode (-i) on your chosen command-line text editor using the git rebase command below to examine your three most recent commits (HEAD3).
9. Remove pick from the first commit and replace it with rephrase. Remove pick and replace it with squash at the start of the commit lines for the second and third commits. When you save, the second and third commitments are combined into the first commit, requesting for a new commit statement.
Delete Git Commits
10. Modify the commit message that specifies the union of the commits you made after saving (step nine). Exit the editor after saving your changes.
Changes to the Git Commit Message
If squashing is effective, you should see something like this.
Confirming the Success of Git Squash
Perhaps you prefer to squash your commits while concurrently merging the branches. If this is the case, add the —squash option to the git merge command, as shown below. The command below combines the form and master branches and squashes the form branch’s commits.
If the command is successful, you should see something like this.
During a merger, squashing
Git Branch Fast-Forwarding
Maybe you’d like maintain your commit history clean while merging branches without causing havoc. If this is the case, developers generally employ fast-forwarding for small feature updates or bug fixes.
Fast-forwarding allows you to combine branches without having to create additional merging commits (3-way merge). But keep in mind that fast-forwarding only happens when one of the branches, ideally the main branch, hasn’t been committed to yet. The current branch pointer is moved to the destination branch while fast-forwarding.
To switch to the form branch, use the command below.
2. Next, create a JavaScript file and paste in any code fragments. The file may be named whatever you like, however for this example, the file is called index.js.
3. Commit (git commit) and stage (git add) the modifications.
add index.js to git “Created a new file,” git commit -m
4. Finally, move to the master branch and merge it with the form branch using the instructions below. Because you’ve only made commits to the form branch, the master branch hasn’t diverged, allowing a straight route from the master to the form branch.
The git merge command merges the commit histories and advances the pointer on the master branch to the form branch.
## Fast-forward (merge) the master branch to the form branch git merge form ## Switch to the master branch git checkout master
Getting from the master branch to the form branch quickly
Conclusion
You’ve learnt how to mix Git project branches during this lesson. You may collaborate with other developers on the same project using Git merge and other Git operations.
Why not work with other developers and contribute to GitHub projects to learn more about merging branches?
Contributing to GitHub Projects is Related (Git Pull Requests for Dummies)
“git merge branch into master” is a command that combines two branches together. The “merge” creates a new commit in the same place as the original one. It will also create a new commit with an extra tag, which can be used to identify the merge.
Related Tags
- git merge main into branch
- git merge command
- git merge command example
- git branch
- create branch git