Git Branching and Merging

This tutorial will guide you through the basic Git operations for branching and merging using the command line interface. You will create a new branch, make changes to the new branch, and then merge those changes back to the main branch.

Note: If you have completed the first tutorial, you can skip to step 3

Step 1: Install Git

First, you need to ensure that Git is installed on your computer. You can check this by typing the following command in your terminal:

git --version

If Git is installed, you will see the version number. If not, you will need to install Git.

Step 2: Configure Git

Next, configure your Git username and email using the following commands:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

Step 3: Initialize a Git Repository

Create a new directory, navigate into it, and initialize a new Git repository:

mkdir git-practice
cd git-practice
git init

Step 4: Create a Branch

Create a new branch in your repository:

git branch my-branch

Step 5: Switch to the New Branch

Switch to the new branch you just created:

git checkout my-branch

Step 6: Create a File

Create a new file in the directory:

echo "Hello, Git!" > hello.txt

Step 7: Stage and Commit Changes

Add the new file to the staging area and commit the changes:

git add hello.txt
git commit -m "Add hello.txt"

Step 8: Switch Back to Main

Switch back to the main branch:

git checkout main

Step 9: Merge Changes

Merge the changes from my-branch into main:

git merge my-branch

Step 10: View Commit History

Finally, view the commit history of your repository:

git log

Congratulations! You have completed the basic Git operations for branching and merging tutorial. Keep practicing these commands until you feel comfortable with them.


Updated on August 7, 2025