Git Branching and Merging

Introduction

This guide provides step-by-step instructions for creating a new branch in Git, making changes to that branch, and merging it back into the main branch without any merge conflicts.

Table of Contents

  1. Prerequisites

  2. Creating a New Branch

  3. Making Changes

  4. Committing Changes

  5. Merging the Branch

  6. Verifying the Merge

  7. Pushing Changes to Remote Repository

  8. Conclusion

Prerequisites

Before you begin, ensure you have the following:

  • A Git repository initialized on your local machine.

  • Git installed on your system.

  • Basic knowledge of navigating through the command line.

Creating a New Branch

To create a new branch called feature1 and switch to it, follow these steps:

  1. Open your terminal or command prompt.

  2. Navigate to your Git repository:

     cd /path/to/your/repo
    
  3. Create and switch to the new branch using the following command:

     git checkout -b feature1
    

    This command creates a new branch named feature1 and switches your working directory to that branch.

Making Changes

  1. Open the project files in your preferred code editor.

  2. Make the necessary changes or add new features as required.

  3. Save the changes in your code editor.

Committing Changes

After making your changes, you need to commit them:

  1. Stage the changes:

     git add .
    

    This command stages all modified files. Alternatively, you can stage individual files by specifying their names instead of using ..

  2. Commit the staged changes with a meaningful commit message:

     git commit -m "Add feature1 implementation"
    

Merging the Branch

To merge the changes from feature1 back into the main branch:

  1. Switch back to the main branch (usually named main or master):

     git checkout main
    
  2. Merge the feature1 branch into the main branch:

     git merge feature1
    

    Since no conflicting changes were made in the main branch, the merge should succeed without any conflicts.

Verifying the Merge

To ensure the merge was successful, you can check the commit history:

git log --oneline

This command displays the commit history, allowing you to verify that the changes from feature1 have been integrated into the main branch.

Pushing Changes to Remote Repository

If you want to share your changes with others or back them up, you can push the changes to the remote repository:

git push origin main

This command pushes the merged changes to the main branch on your remote repository (e.g., GitHub).

Conclusion

By following this guide, you have successfully created a new branch, made changes, and merged those changes back into the main branch without any merge conflicts. This process helps maintain an organized workflow and enables independent feature development.

Feel free to adjust any commands or processes based on your specific project needs!