Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating and Managing Branches in Git

Branching in Git allows you to work on different versions of your project independently. Here’s how to create, switch, merge, and rebase branches using essential Git commands.

1. Create a Branch

To create a new branch:

git branch <branch-name>

Example:

git branch feature/login

2. Switch Between Branches

You can switch branches using:

git checkout <branch-name>

Or, in newer versions of Git:

git switch <branch-name>

Example:

git checkout feature/login
# or
git switch feature/login

3. Create and Switch to a Branch

Create a new branch and switch to it immediately:

git checkout -b <branch-name>
# or
git switch -c <branch-name>

Example:

git checkout -b feature/signup
# or
git switch -c feature/signup

4. List Branches

To see all branches in your repository:

git branch

5. Merge Branches

To merge changes from one branch (e.g., feature/login) into your current branch (e.g., main):

git checkout main
git merge feature/login

6. Rebase Branches

Rebasing applies your changes on top of another branch’s history:

git checkout feature/login
git rebase main

This moves the feature/login branch to begin on the tip of main, replaying your commits.

7. Delete a Branch

After merging, you may want to delete a branch:

git branch -d <branch-name>

Example:

git branch -d feature/login

Summary Table

Action Command Example
Create branch git branch feature/login
Switch branch git checkout feature/login
Create & switch git checkout -b feature/signup
List branches git branch
Merge into current branch git merge feature/login
Rebase onto another branch git rebase main
Delete branch git branch -d feature/login

Reference

For more details, see the Git Branching documentation.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading