Skip to content

Branching and Merging

โ† Back to Git


๐ŸŒฟ Branching Commands

Branches allow you to develop features, fix bugs, or experiment safely without affecting the main codebase.

List Branches

To list both local and remote branches:

git branch -a

To list only local branches:

git branch

To list only remote branches:

git branch -r

Create Branch

To create a new branch from a specific parent branch:

git branch <new_branch_name> <parent_branch_name>
Note: If parent_branch_name is omitted, it defaults to the current branch.

Switch Branch

To switch to an existing branch:

git checkout <branch_name>

Create & Switch

To create a new branch and switch to it immediately:

git checkout -b <new_branch_name>

Delete Branch

To delete a local branch (safe delete):

git branch -d <branch_name>
Note: This fails if the branch contains unmerged changes.

To force delete a branch (even with unmerged changes):

git branch -D <branch_name>

Delete Remote Branch

To delete a branch from the remote repository:

git push origin --delete <branch_name>


๐Ÿ”„ Merging Commands

Merging integrates changes from one branch into another.

Compare Branches

To see the differences between two branches before merging:

git diff <current_branch> <target_branch>

Fast-Forward Merge (Default)

If the target branch has not diverged, Git moves the pointer forward.

git merge <source_branch>

Disable Fast-Forward

To force a merge commit even if a fast-forward is possible (preserves history topology):

git merge <source_branch> --no-ff

Merge Conflicts

If the same line in the same file was modified in both branches, Git cannot auto-merge. 1. Git pauses the merge and marks the conflict in the file. 2. Manually edit the file to resolve changes. 3. Add and commit the resolved file.

Check Merge Status

To see branches already merged into the current branch:

git branch --merged

To see branches NOT yet merged into the current branch:

git branch --no-merged


๐Ÿง  Quick Quiz โ€” Branching

#

Which command creates a new branch and switches to it in one step?


๐Ÿ“ Want More Practice?

๐Ÿ‘‰ Start Git Intermediate Quiz (20 Questions)


๐Ÿ“ฌ DevopsPilot Weekly โ€” Learn DevOps, Cloud & Gen AI the simple way.
๐Ÿ‘‰ Subscribe here