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 branchOutput format:Â Âmain feature/login bugfix/issue-123Additional 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 developUseful 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 developImportant 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 developAdvantages of switch:- Clearer intent (you’re switching, not checking out)
- Better error messages for uncommitted changes
- Less likely to accidentally create new branches
# 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 abc1234Types 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---DCharacteristics:- 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---ECharacteristics:- Creates a merge commit (F)
- Preserves history of both branches
- Useful for tracking when changes were merged
# 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 - Fast-forward merges
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
Best Practices and Tips
- Always verify before deleting: Use git branch -v to check if a branch is merged before deletion.
- Use descriptive names: Branch names should clearly describe what work they contain:
feature/user-login
bugfix/payment-processing
hotfix/critical-security-issue
- Keep branches small and focused: Each branch should address one specific issue or feature.
- Regular cleanup: Remove merged branches to keep the repository clean.
- Use switch instead of checkout: For new projects, prefer git switch for clarity.
These commands form the foundation of Git branching workflows and are essential for effective version control management.
Full Working Git Example
Copy it from the previous video.
Can you answer?
Listing All Local Branches
- What does the git branch command display by default?
- How can you see both local and remote tracking branches in one list?
- What information does git branch -v show that regular git branch doesn’t?
Creating New Branches
- What is the key difference between git branch feature/new-ui and git checkout -b feature/new-ui?
- How can you create a new branch from a specific commit using the git branch command?
- Why would you use git switch -c with a branch name that already exists?
Switching Between Branches
- What are the main advantages of using git switch over git checkout?
- What happens when you use git switch –force-create branch-name?
- How do you set up upstream tracking when switching to a new branch?
Merging Branches
- Under what conditions does Git perform a fast-forward merge?
- What is the main difference between a fast-forward merge and a three-way merge?
- Why would you use git merge –no-ff instead of allowing fast-forward merges?
Deleting Branches
- What is the safety mechanism built into git branch -d that prevents accidental deletion?
- When would you use git branch -D instead of git branch -d?
- How can you delete multiple branches in one command?