How to Create a New Branch, Make Changes, and Merge It into the Main Branch without Conflicts

Ayushmaan Srivastav
3 min readOct 5, 2024

--

When working on a project with Git, a common workflow involves creating a feature branch to implement new functionality and merging it back into the main branch once the work is done. Here’s a detailed guide on how to achieve this, step by step:

Prerequisites

  • You should have Git installed on your machine. If not, download and install it from here.
  • You need a local Git repository or clone an existing repository from a remote (e.g., GitHub, GitLab).

Step 1: Clone the Repository (If Needed)

If you already have the repository cloned, you can skip this step. Otherwise, you can clone it using the following command:

git clone https://github.com/username/repository.git

Replace username and repository with your GitHub username and the repository name. Navigate into the repository's directory:

cd repository

Step 2: Create a New Branch (feature1)

Start by creating a new branch where you’ll implement your new feature or changes. This branch will be separate from the main branch, so you can work without affecting the production code.

git checkout -b feature1
  • checkout -b creates and switches to a new branch called feature1.

You can verify that you are now on the feature1 branch using:

git branch

Step 3: Make Changes

At this point, make the necessary changes to your files in the feature1 branch. This could involve editing code, adding new files, or deleting unnecessary ones.

For example, let’s assume you’re editing a file called app.js:

nano app.js

Once your changes are done, save the file.

Step 4: Stage and Commit Your Changes

Now that the changes are made, you’ll need to add them to the staging area and commit them.

Stage the files:

git add .

Commit the changes with a message describing what you’ve done:

git commit -m "Added new feature in app.js"

Step 5: Switch Back to the Main Branch

Before merging, you need to switch back to the main branch. You can do this using the following command:

git checkout main

Step 6: Pull the Latest Changes from the Main Branch

Before merging your feature1 branch, it's essential to ensure your main branch is up to date. This helps to avoid merge conflicts.

If you’re working with a remote repository, pull the latest changes:

git pull origin main

If no changes were made by others, this command won’t make any modifications. However, it ensures you’re working with the most recent code.

Step 7: Merge the Feature Branch into Main

Now, you’re ready to merge the feature1 branch into the main branch.

git merge feature1

At this stage, if no one else has made conflicting changes to the code, the merge should proceed without conflicts.

Step 8: Resolve Merge Conflicts (If Any)

If there are any conflicts (which can happen if changes were made to the same lines of code in both branches), Git will pause the merge and allow you to manually resolve them.

Git will show you the conflicted files. You can open them, resolve the conflicts, and then stage the resolved files:

git add <file>

After resolving all conflicts, continue the merge process:

git commit

Step 9: Push Changes to the Remote Repository

Finally, push your merged changes to the remote repository so that others can access the updated main branch:

git push origin main

Conclusion

You’ve now successfully created a new feature branch, made changes, and merged it back into the main branch without conflicts! This process is common in team-based development workflows, as it allows each developer to work on separate features without affecting the main codebase.

--

--

No responses yet