How to Push to a New Branch on GitHub

Creating and pushing to a new branch lets you work on features, fixes, or experiments without affecting the main codebase. Here’s how to do it smoothly.


🚀 Steps to Push to a New Branch

1. Check Out a New Branch Locally

To create and switch to a new branch named feature-branch:

git checkout -b feature-branch

This creates a new branch and switches you to it immediately.


2. Make Your Changes

Edit your files, add new code, and when ready, stage and commit your changes:

git add .
git commit -m "Add feature xyz"

3. Push the New Branch to GitHub

Push your local branch to the remote repository:

git push -u origin feature-branch
  • -u sets the upstream tracking so future git push or git pull commands will know which branch to use.
  • origin is the default name for the remote repository.
  • feature-branch is the branch name you want to push.

4. Create a Pull Request (Optional)

After pushing, go to GitHub. You’ll usually see a prompt to Compare & pull request for your new branch. Click it to open a PR and start code review.


🧠 Summary Table

CommandPurpose
git checkout -b <branch>Create & switch to new branch
git add .Stage changes
git commit -m "message"Commit changes
git push -u origin <branch>Push new branch to GitHub and set upstream

🔥 Pro Tips

  • Use descriptive branch names: feature/login, bugfix/navbar-fix, hotfix/security-patch
  • Keep branches short-lived and focused to avoid merge conflicts
  • Regularly pull changes from main or master into your branch to stay updated
Sharing Is Caring:

Leave a Comment