Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 40 ยท Git and Remote Development

Branches, History and the Timeline

Create and switch branches in VS Code, use stash and the Source Control Graph, browse file history with the Timeline view, and open pull requests from the editor.

A repository is more than the files you see today. It is a history of snapshots, arranged in branches. VS Code gives you several views into that history: the status bar branch picker, the Source Control Graph, the Timeline view for one file, and the local history that exists even without Git. This lesson shows how to use them to work on a feature safely, recover an old version and understand who changed what.

The two stand-ins at the end are plain JavaScript models of branches and of a file timeline; they do not touch Git.

Branches in the status bar

The bottom left of the window shows the current branch. Click it to open a list that offers:

  • Create new branch... from the current commit (and optionally from... another branch or tag),
  • every local and remote branch to check out,
  • Checkout detached... for a specific commit.

The Command Palette equivalents are Git: Create Branch..., Git: Checkout to..., Git: Rename Branch..., Git: Delete Branch... and Git: Merge Branch.... When you check out a branch with uncommitted changes, Git carries them along if they do not conflict. If they do, VS Code offers to stash them or to cancel.

A branch name such as feature/search-box or fix/cart-total tells teammates why it exists. Publish it with Publish Branch so the remote gets a copy.

Stash

Stashing parks uncommitted work without committing it:

  • Git: Stash stores tracked changes; Git: Stash (Include Untracked) also takes new files.
  • Git: Pop Stash... applies a stash and removes it from the list; Git: Apply Stash... applies but keeps it.
  • Git: Drop Stash... deletes one you no longer need.

Use a stash when you need to switch branches quickly (an urgent bug report) and you are not ready to commit.

The Source Control Graph

In the Source Control view, the Graph section draws the commit history of the current branch, its remote counterpart and the base branch. Each dot is a commit, with labels for branches and tags. Hover a commit to see author, date and message; click it to see the changed files and open any file's diff for that commit. The context menu offers Checkout, Create Branch, Cherry-Pick and Copy Commit ID. It is a quick way to answer "is my branch behind main?" without leaving the editor.

gitGraph
    commit id: "init"
    commit id: "add list"
    branch feature-search
    checkout feature-search
    commit id: "add box"
    commit id: "filter"
    checkout main
    commit id: "fix typo"
    merge feature-search

Two lines of work joined by a merge commit.

The Timeline view

The Timeline is a section at the bottom of the Explorer. For the file that is open, it lists:

  • Git history: every commit that touched the file, with author and date,
  • Local history: automatic saves made by VS Code itself (a safety net, for example after a bad find and replace), kept even outside Git.

Click an entry to see a diff against the current file. Right-click for Restore Contents, Compare with Working Tree and Copy Commit ID. If you changed a file by mistake and have not committed, Local history can bring it back. The setting workbench.localHistory.maxFileEntries controls how many versions are kept; workbench.localHistory.enabled turns it off.

Blame and line history

Even without extra extensions you can ask "who changed this line?":

  • Git: View File History (or Git: View Line History, depending on the version) opens the history of the file in the Source Control area.
  • Hovering over a line number can show who edited it when the GitLens extension is installed. See essential extensions for what helper extensions add.

Pull requests from the editor

The GitHub Pull Requests extension adds a view where you can browse, check out and review pull requests, add comments on lines and create a new pull request from your branch. Install it from the Marketplace and sign in with your GitHub account; the sign-in is handled by the editor's account system, and no password is stored in your repository. Similar extensions exist for GitLab and Azure DevOps.

Common tasks and where to find them

I want to... Do this
Start a new feature Status bar branch > Create new branch
Pause my work Git: Stash (Include Untracked)
See an old version of a file Timeline > click entry
Undo a bad edit not yet committed Timeline > Local history entry > Restore Contents
Bring changes from main Git: Merge Branch... or Pull
Remove a merged branch Git: Delete Branch...
Copy one commit onto another branch Graph > Cherry-Pick

Runnable stand-ins

JS Stand-in: branches are pointers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: a file timeline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a Git branch?

    Branches are lightweight pointers to commits, which is why creating one is instant.
  2. 2. Where do you see the history of just the open file?

    The Timeline section of the Explorer lists commits and local history for the active file.
  3. 3. You must switch branches but your work is not ready to commit. What do you use?

    Stash parks uncommitted changes so you can switch and come back later.
  4. 4. What does Local History protect you from?

    VS Code keeps automatic versions of saved files so you can restore an earlier one.

Summary

  • Switch and create branches from the status bar or the Command Palette.
  • Stash parks uncommitted work; pop or apply it later.
  • The Graph shows branches and commits; the Timeline shows one file's history plus local history.
  • Pull request extensions let you review and create pull requests inside the editor.
  • Branch names and small commits make history readable.

Next: Remote Development with SSH and WSL