KandZ – Tuts

We like to help…!

Git 3b 🧩 Basic Branch Commands

3b Basic Branch Commands
Listing All Local Branches
git branch
This command lists all local branches in your repository:

git branch

Output format:

main
feature/login
bugfix/issue-123
Additional useful options:

Show remote tracking branches too
git branch -a

Show branch history and commit info
git branch -v

Show branches with their merge status
git branch --merged
git branch --no-merged
Creating New Branches
git branch [branch-name]
Creates a new branch but doesn't switch to it:

git branch feature/new-ui
git branch develop
Useful variations:

Create branch from specific commit
git branch feature/new-ui abc1234

Create branch from specific remote branch
git branch feature/new-ui origin/main
git checkout -b [new-branch-name] or git switch -c [new-branch-name]
Creates and immediately switches to a new branch:

Old way (checkout)
git checkout -b feature/user-profile

New way (switch) - recommended
git switch -c feature/user-profile

Create from specific commit
git switch -c feature/new-feature abc1234

Create from remote branch
git switch -c feature/new-feature origin/main
Switching Between Branches
git checkout [branch-name] (Old method)
Switches to an existing branch:

git checkout main
git checkout feature/login
git checkout develop
Important note: This command can be dangerous if you have uncommitted changes, as Git will warn you about potential conflicts.

git switch [branch-name] (Newer method)
A safer and more intuitive way to switch branches:

git switch main
git switch feature/login
git switch develop
Advantages of switch:

Clearer intent (you're switching, not checking out)
Better error messages for uncommitted changes
Less likely to accidentally create new branches
Switching with additional options:

Switch and reset working directory to match branch
git switch --force-create branch-name

Switch and set upstream tracking
git switch -c branch-name --track origin/branch-name
Merging Branches
git merge [branch-name]
Combines changes from the specified branch into your current branch:

Merge feature branch into main
git checkout main
git merge feature/user-profile

Merge with specific commit
git merge abc1234
Types of Merges:

Fast-forward merges
Occurs when the current branch is a direct ancestor of the branch being merged:

Example scenario:
main: A---B---C
feature: \
D

After git merge feature:
main: A---B---C---D
Characteristics:

No merge commit is created
The branch pointer simply moves forward
Clean history, no merge artifacts
Three-way merges
Occurs when branches have diverged and Git needs to create a merge commit:

Example scenario:
main: A---B---C
feature: \
D---E

After git merge feature:
main: A---B---C---F
\ /
\ /
D---E
Characteristics:

Creates a merge commit (F)
Preserves history of both branches
Useful for tracking when changes were merged
Merge strategies:

Use specific merge strategy
git merge --strategy-option recursive

Squash all commits into one
git merge --squash feature-branch

Always create merge commit (no fast-forward)
git merge --no-ff feature-branch
Deleting Branches
git branch -d [branch-name]
Safely deletes a local branch:


git branch -d feature/user-profile
git branch -d bugfix/issue-123

Safety features:
Only deletes if the branch has been merged into the current branch
Prevents accidental deletion of unmerged work
git branch -D [branch-name]
Force deletes a local branch (even if not merged):

git branch -D feature/user-profile
git branch -D bugfix/issue-123
Warning: Use with caution as this permanently deletes unmerged changes.
Deleting Remote Branches
git push origin --delete [branch-name]
Deletes a remote branch:

```
git push origin --delete feature/user-profile
git push origin --delete bugfix/issue-123
```
Alternative syntax:
More explicit version
git push origin :feature/user-profile

Delete multiple branches
git push origin --delete feature/branch1 feature/branch2
Advanced Branch Operations
Creating and switching with branch tracking:

Create branch and set upstream (remote tracking)
git switch -c feature/new-feature origin/main

Set upstream after creation
git branch --set-upstream-to=origin/feature/new-feature
Renaming branches:

Rename current branch
git branch -m new-name

Rename specific branch
git branch -m old-name new-name
Listing branches with additional information:

Show all branches with last commit info
git branch -v

Show remote tracking information
git branch -vv

Show branch size and other statistics
git branch --stat

Leave a Reply