Git Foundations / Branch and Merge

Branches and HEAD

Lesson 14 of 33. Open to read. No account needed.

Why Branches Exist

So far every commit has gone onto one line of history, main. Real work is rarely that tidy: you start a new feature, a bug report arrives, a teammate wants to try an idea. Branches let each piece of work grow separately and be combined later, which the next lesson covers.

A Branch Is a Name for a Commit

A branch is not a copy of your files. It is a name that points at one commit, the newest commit on that line of work, called the branch's tip. When you commit on a branch, Git makes the new commit and moves that name forward to it. Nothing else moves.

HEAD is how Git records where you are. Normally HEAD points at a branch, meaning "I am on this branch", and that branch's tip is the commit your files come from.

Three panels. Before: commits A and B; main and photos both point at B, and HEAD points at photos. After a commit on photos: a new commit C; photos and HEAD moved to C, while main still points at B. Detached HEAD: HEAD points straight at a new commit D, and no branch moves.

Walk through the diagram:

  1. main and photos start on the same commit, B. HEAD points at photos.
  2. A commit on photos creates C. Only photos moves to C, and HEAD comes along because it points at photos. main stays on B.
  3. In the last panel HEAD points straight at a commit instead of a branch. This is a detached HEAD. A new commit D moves only HEAD; no branch name records it.

Working With Branches

git branch                  # list branches; * marks the one you are on
git branch <name>           # create a branch at the current commit
git switch <name>           # move to that branch
git switch -c <name>        # create a branch and move to it

Switching changes the files in your folder to match the tip of the branch you switch to. A file committed only on one branch disappears when you switch away and comes back when you return. It is not lost; it lives in that branch's commits.

If you have uncommitted edits, Git carries them along when the switch does not touch those files. If it would have to overwrite them, Git refuses with Your local changes to the following files would be overwritten by checkout. Commit or discard the edits first; a later lesson shows another way.

To see every branch at once, ask git log for all of them, drawn as a graph:

git log --oneline --graph --all

In a terminal, each commit line shows the names that point at it in brackets, such as (HEAD -> photos) or (main). HEAD -> photos means HEAD points at photos.

Detached HEAD

git switch expects a branch. To look at an older commit itself, ask for a detached HEAD explicitly:

git switch --detach <commit>

git status then says HEAD detached at and the commit's short ID. You can look around and even commit, but no branch moves. If you switch away, those commits are left behind with no name, and Git warns you are leaving 1 commit behind. To keep them, create a branch while you are still there:

git switch -c <new-branch>

That branch starts at the current commit, so the work is safe. To leave a detached HEAD without keeping anything, switch back to a branch.

Older guides use git checkout <branch> to switch, git checkout -b <name> to create and switch, and git checkout <commit> to detach. They still work; git switch is the newer command that does only this job.

Exercise: Create, Switch and Inspect Branches

You will create branches, commit on them and watch which names move.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup. It makes two commits on main:

    mkdir -p ~/git-practice/branch-basics
    cd ~/git-practice/branch-basics
    git init -b main
    cat > guide.md <<'EOF'
    # Trail guide
    EOF
    git add guide.md
    git commit -m "Add trail guide"
    cat >> guide.md <<'EOF'
    
    ## Safety
    
    Carry water.
    EOF
    git commit -am "Add safety tips"
    
  2. Look at the branches and the graph:

    git branch
    git log --oneline --graph --all
    

    git branch prints * main. The graph shows (HEAD -> main) on "Add safety tips".

  3. Create a branch, then look again:

    git branch photos
    git branch
    git log --oneline --graph --all
    

    Both branches are listed, but the * is still on main: creating a branch does not switch to it. The newest commit shows (HEAD -> main, photos), because both names point at it.

  4. Switch to the new branch:

    git switch photos
    git branch
    

    Git prints Switched to branch 'photos', and the * moves to photos.

  5. Commit on photos and see which name moves:

    cat > photos.md <<'EOF'
    # Photo spots
    
    The ridge at sunrise.
    EOF
    git add photos.md
    git commit -m "Add photo spots"
    git log --oneline --graph --all
    

    Illustrative output:

    * bbac593 (HEAD -> photos) Add photo spots
    * 95b233f (main) Add safety tips
    * 7cfe2bc Add trail guide
    

    photos moved to the new commit, taking HEAD with it. main stayed where it was.

  6. Switch to main and back, checking the files each time:

    git switch main
    ls
    git switch photos
    ls
    git switch main
    

    photos.md is missing on main and back on photos. You end on main.

  7. Create a second branch from main and switch to it in one step, then commit:

    git switch -c maps
    cat > maps.md <<'EOF'
    # Maps
    
    Download the park map before you go.
    EOF
    git add maps.md
    git commit -m "Add map links"
    git log --oneline --graph --all
    

    The graph now forks. Each branch grew its own commit from "Add safety tips":

    * fde0760 (HEAD -> maps) Add map links
    | * bbac593 (photos) Add photo spots
    |/
    * 95b233f (main) Add safety tips
    * 7cfe2bc Add trail guide
    
  8. Try a detached HEAD, commit on it, then keep the work:

    git switch --detach main
    git status
    cat > ideas.md <<'EOF'
    # Ideas
    
    A night hike?
    EOF
    git add ideas.md
    git commit -m "Try a night hike idea"
    git log --oneline --graph --all
    git switch -c ideas
    git log --oneline --graph --all
    

    git status says HEAD detached at with main's commit ID. After the commit, the graph shows (HEAD) on "Try a night hike idea" with no branch name. After git switch -c ideas, it shows (HEAD -> ideas): the commit now belongs to a branch.

Success: git log --oneline --graph --all shows main on "Add safety tips", photos and maps each one commit ahead of it on separate lines, and (HEAD -> ideas) on the night hike commit.

If something differs:

What you seeLikely causeNext step
No names in brackets in the graphOutput is not going straight to a terminal, or decoration is turned offAdd --decorate to the git log command
fatal: a branch is expected, got commitgit switch was given a commit, not a branchAdd --detach, or switch to a branch name
fatal: invalid reference: photoA misspelled branch nameCheck the spelling with git branch
fatal: a branch named 'photos' already existsThe branch was created earlierSwitch to it with git switch photos
Your local changes to the following files would be overwritten by checkoutUncommitted edits to a file that differs on the other branchCommit or discard them, then switch
you are leaving 1 commit behindYou switched away from a detached HEAD with new commitsRun the git branch <new-branch-name> <ID> command Git printed
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf branch-basics
    ls
    

Independent task: before cleaning up, switch to main, then create a branch named glossary and switch to it with one command. Commit a new file glossary.md on it. Before looking at the graph, predict which names will point at your new commit and which commit main will point at. Then check.

Check Your Understanding

  1. main and feature both point at commit B, and you are on feature. You make commit C. Where do main, feature and HEAD point now?
  2. You are on a detached HEAD at commit B and make commit C. Which reference moves? What happens to C if you then switch to main?

Answer and Reasoning

Independent task

git switch main
git switch -c glossary
cat > glossary.md <<'EOF'
# Glossary

Cairn: a pile of stones that marks a trail.
EOF
git add glossary.md
git commit -m "Add glossary"
git log --oneline --graph --all

The new commit shows (HEAD -> glossary): only glossary and HEAD point at it. Its parent is "Add safety tips", where main still points, because the branch was created from main and main does not move when you commit on another branch.

Understanding check

  1. feature and HEAD point at C, because HEAD points at feature and the current branch moves forward with each commit. main still points at B.
  2. Only HEAD moves, to C; no branch records it. If you switch to main, C is left with no name pointing at it, and Git warns that you are leaving a commit behind. To keep C, run git switch -c <name> before switching, or the git branch command Git prints afterwards.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy