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:
    1. 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
    2. 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

Best Practices and Tips

  1. Always verify before deleting: Use git branch -v to check if a branch is merged before deletion.
  2. Use descriptive names: Branch names should clearly describe what work they contain:
feature/user-login
bugfix/payment-processing
hotfix/critical-security-issue
  1. Keep branches small and focused: Each branch should address one specific issue or feature.
  2. Regular cleanup: Remove merged branches to keep the repository clean.
  3. 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
  1. What does the git branch command display by default?
  2. How can you see both local and remote tracking branches in one list?
  3. What information does git branch -v show that regular git branch doesn’t?
Creating New Branches
  1. What is the key difference between git branch feature/new-ui and git checkout -b feature/new-ui?
  2. How can you create a new branch from a specific commit using the git branch command?
  3. Why would you use git switch -c with a branch name that already exists?
Switching Between Branches
  1. What are the main advantages of using git switch over git checkout?
  2. What happens when you use git switch –force-create branch-name?
  3. How do you set up upstream tracking when switching to a new branch?
Merging Branches
  1. Under what conditions does Git perform a fast-forward merge?
  2. What is the main difference between a fast-forward merge and a three-way merge?
  3. Why would you use git merge –no-ff instead of allowing fast-forward merges?
Deleting Branches
  1. What is the safety mechanism built into git branch -d that prevents accidental deletion?
  2. When would you use git branch -D instead of git branch -d?
  3. How can you delete multiple branches in one command?

Leave a Reply