Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
Git is a powerful version control system that allows developers to work collaboratively on a project. One of the key features of Git is the ability to merge branches. Merging allows developers to combine changes from one branch into another, making it easier to manage changes and keep track of different versions of a project.
Merge a Branch and Create a Merge Commit
As a developer, you may need to merge a branch into the current branch, creating a merge commit. This can be a bit tricky if you're not familiar with Git. The problem is to merge a branch into the current branch, creating a merge commit, using the Git repository named https://github.com/labex-labs/git-playground directory.
For this challenge, let's use the repository from https://github.com/labex-labs/git-playground.
- Clone a repository from
https://github.com/labex-labs/git-playground.git:
git clone https://github.com/labex-labs/git-playground.git
- Navigate to the directory and configure the identity:
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
- Create and switch to a branch called
feature-branch:
git checkout -b feature-branch
- Add "This is a new line." to the
README.mdfile, add it to the staging area and commit it, the commit message is "Add new line to README.md":
echo "This is a new line." >> README.md
git add .
git commit -am "Add new line to README.md"
- Switch to the
masterbranch:
git checkout master
- Merge the
feature-branchinto themasterbranch,which will create a merge commit with the message "Merge feature-branch":
git merge --no-ff -m "Merge feature-branch" feature-branch
This is the result of running git log:
ADD new line to README.md
Summary
Merging branches is an important part of working with Git. By following the steps outlined in this challenge, you should be able to merge a branch into the current branch, creating a merge commit. Remember to always test your changes before merging them into the main branch, and to communicate with your team to avoid conflicts and ensure a smooth development process.