Bird
Raised Fist0
Gitdevops~5 mins

Listing branches in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Listing branches
Start in Git repo
Run 'git branch'
Git reads local branches
Display list of branches
End
The flow shows how Git lists all local branches when you run the 'git branch' command.
Execution Sample
Git
git branch
This command lists all local branches in the current Git repository.
Process Table
StepActionEvaluationResult
1Run 'git branch' commandGit checks local branch referencesFinds branches: main, feature1, bugfix
2Prepare output listMark current branch with '*'* main feature1 bugfix
3Display branches to userOutput list shown in terminalBranches listed with current branch marked
4EndNo more actionsCommand completes
💡 All local branches listed and command finishes
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
branchesempty[main, feature1, bugfix][* main, feature1, bugfix][* main, feature1, bugfix][* main, feature1, bugfix]
current_branchunknownmainmainmainmain
Key Moments - 2 Insights
Why is one branch marked with an asterisk (*)?
The asterisk (*) marks the current branch you are on, as shown in execution_table step 2 where '* main' indicates 'main' is active.
Does 'git branch' show remote branches?
'git branch' by default shows only local branches. To see remote branches, you need 'git branch -r'. This is why only local branches appear in the output in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the list of branches after step 1?
A[* main, feature1, bugfix]
B[main]
C[main, feature1, bugfix]
Dempty
💡 Hint
Check the 'branches' variable in variable_tracker after Step 1
At which step is the current branch marked with '*'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for marking
If you want to list remote branches, what should you do?
ARun 'git branch'
BRun 'git branch -r'
CRun 'git branch -a'
DRun 'git remote show'
💡 Hint
Recall key_moments about local vs remote branches
Concept Snapshot
git branch
Lists all local branches in the current repository.
The current branch is marked with '*'.
To list remote branches, use 'git branch -r'.
To list all branches (local + remote), use 'git branch -a'.
Full Transcript
When you run 'git branch', Git reads all local branch references in your repository. It then prepares a list of these branches and marks the current active branch with an asterisk (*). Finally, it displays this list in your terminal. This command only shows local branches. To see remote branches, you need to add the '-r' option. The current branch marking helps you know which branch you are working on. The command finishes after displaying the list.

Practice

(1/5)
1. What does the command git branch show by default?
easy
A. All remote branches in the repository
B. All local branches in the repository
C. All branches, both local and remote
D. The current branch only

Solution

  1. Step 1: Understand the default behavior of git branch

    The command git branch without any options lists only the local branches in your repository.
  2. Step 2: Differentiate from remote branches

    Remote branches require the -r option, and all branches require -a. So by default, it shows local branches only.
  3. Final Answer:

    All local branches in the repository -> Option B
  4. Quick Check:

    Default git branch = local branches [OK]
Hint: No option means local branches only [OK]
Common Mistakes:
  • Confusing local with remote branches
  • Thinking it shows all branches by default
  • Assuming it shows only the current branch
2. Which command correctly lists all remote branches in a Git repository?
easy
A. git branch -r
B. git branch -a
C. git branch --remote-list
D. git branch --all-remote

Solution

  1. Step 1: Identify the option for remote branches

    The option -r with git branch lists all remote branches.
  2. Step 2: Verify other options

    -a lists all branches (local + remote), but the question asks only for remote branches. The other options are invalid.
  3. Final Answer:

    git branch -r -> Option A
  4. Quick Check:

    -r means remote branches [OK]
Hint: Use -r to list remote branches only [OK]
Common Mistakes:
  • Using -a to list only remote branches
  • Typing invalid options like --remote-list
  • Confusing remote with local branches
3. You want to list all branches but exclude remote branches from the output. Which command should you use?
easy
A. git branch -r
B. git branch -a
C. git branch
D. git branch --no-remote

Solution

  1. Step 1: Understand the requirement

    You want to list all branches but exclude remote branches, so only local branches should appear.
  2. Step 2: Identify the correct command

    git branch by default lists only local branches. git branch -a lists all branches including remote, git branch -r lists only remote branches, and git branch --no-remote is invalid.
  3. Final Answer:

    git branch -> Option C
  4. Quick Check:

    Default git branch = local branches only [OK]
Hint: No option lists local branches only [OK]
Common Mistakes:
  • Using git branch -a, which includes remote branches
  • Using invalid options like --no-remote
  • Confusing remote and local branch listings
4. What is the output of the command git branch -a if your repository has local branches main, dev and remote branches origin/main, origin/feature?
medium
A. * main\n dev\n remotes/origin/main\n remotes/origin/feature
B. * main\n dev
C. remotes/origin/main\nremotes/origin/feature
D. * origin/main\n origin/feature

Solution

  1. Step 1: Understand git branch -a output format

    This command lists all branches: local branches are shown plainly, remote branches are prefixed with remotes/.
  2. Step 2: Match branches to output

    Local branches main and dev appear without prefix. Remote branches appear as remotes/origin/main and remotes/origin/feature. The current branch is marked with *.
  3. Final Answer:

    * main\n dev\n remotes/origin/main\n remotes/origin/feature -> Option A
  4. Quick Check:

    -a shows all branches with remotes/ prefix [OK]
Hint: All branches show; remotes have remotes/ prefix [OK]
Common Mistakes:
  • Missing remotes/ prefix for remote branches
  • Showing only local or only remote branches
  • Not marking current branch with *
5. You ran git branch -r but got an error: error: unknown option '-r'. What is the likely cause?
medium
A. You need to use git remote branch instead
B. You typed git branch -r in a non-Git directory
C. You forgot to fetch remote branches first
D. You used an old Git version that does not support -r

Solution

  1. Step 1: Analyze the error message

    The error says unknown option '-r', meaning Git does not recognize the -r flag.
  2. Step 2: Identify possible causes

    This usually happens if the Git version is very old and does not support -r with git branch. Other options would give different errors or no error.
  3. Final Answer:

    You used an old Git version that does not support -r -> Option D
  4. Quick Check:

    Old Git versions lack -r option [OK]
Hint: Check Git version if options cause unknown errors [OK]
Common Mistakes:
  • Assuming wrong command syntax
  • Thinking fetch is required to list remote branches
  • Confusing directory errors with option errors