DEV Community

Cover image for Git Branching, Pull, Merge, Commit, and Push – Step-by-Step Guide
Saurabh Chavan
Saurabh Chavan

Posted on

Git Branching, Pull, Merge, Commit, and Push – Step-by-Step Guide

In this post, I’ll walk you through how to create a Git branch from a parent branch, pull the latest changes, commit your code, push it to the remote repository, merge branches, and finally make sure everything stays up to date. I’ll explain each step in a simple and practical way.

Let’s get started 🚀

1️⃣ Switch to the Parent Branch and Fetch Latest Changes

First, move to the parent branch (usually main or develop) and fetch the latest updates from the remote repository.

git checkout branch-name
git fetch origin

Enter fullscreen mode Exit fullscreen mode

Now pull the latest changes to make sure your parent branch is up to date:

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

2️⃣ Create a New Branch from the Parent Branch

Once your parent branch is updated, create a new feature branch from it:

git checkout -b new_branch_name(feature/login)

Enter fullscreen mode Exit fullscreen mode

This command creates a new branch and switches to it at the same time.

3️⃣ Make Changes and Commit Your Code

After making your code changes, check the status:

git commit -m "commmit msg"

Enter fullscreen mode Exit fullscreen mode

4️⃣ Push the Branch to Remote Repository

To push your newly created branch to GitHub:

git push origin branch_name(in which you have push)

Enter fullscreen mode Exit fullscreen mode

5️⃣ Merge Parent Branch into Your Feature Branch

Before pushing or raising a pull request, always merge the latest parent branch into your feature branch.

git merge branch_name(which branch you have to merge)

Enter fullscreen mode Exit fullscreen mode

6️⃣ Always Pull Before You Push

To avoid conflicts, make it a habit to pull the latest changes before pushing your code:

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

Please refer the below picture

Top comments (0)