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>
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>
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