Skip to content

Commit a582403

Browse files
committed
Add article about branching and worktrees
1 parent d0b338a commit a582403

15 files changed

+188
-201
lines changed

build/sitemap.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,12 @@
296296
<priority>0.8</priority>
297297
</url>
298298
<url>
299-
<loc>https://code.visualstudio.com/docs/sourcecontrol/intro-to-git</loc>
299+
<loc>https://code.visualstudio.com/docs/sourcecontrol/staging-commits</loc>
300300
<changefreq>weekly</changefreq>
301301
<priority>0.8</priority>
302302
</url>
303303
<url>
304-
<loc>https://code.visualstudio.com/docs/sourcecontrol/staging-commits</loc>
304+
<loc>https://code.visualstudio.com/docs/sourcecontrol/branches-worktrees</loc>
305305
<changefreq>weekly</changefreq>
306306
<priority>0.8</priority>
307307
</url>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
ContentId: a9b2c3d4-e5f6-7890-ab12-cd3456789012
3+
DateApproved: 11/12/2025
4+
MetaDescription: Learn how to work with Git branches and worktrees in VS Code. Create, switch between, and manage multiple branches, use Git worktrees for parallel development, and manage stashes for temporary changes.
5+
---
6+
# Git Branches and Worktrees in VS Code
7+
8+
Git branches enable you to work on different features or experiments simultaneously without affecting your main codebase. VS Code provides tools for branch management, Git worktrees for parallel development, and stash management for temporary changes.
9+
10+
This article covers working with branches, worktrees, and stashes in VS Code to manage parallel development work.
11+
12+
## Working with branches
13+
14+
Branches are lightweight, movable pointers to specific commits in your Git history. They allow you to diverge from the main line of development and work on features independently.
15+
16+
For example, suppose you're working on a web application and need to add user authentication while also fixing a bug in the payment system. You can create two branches:
17+
18+
* `feature/user-authentication` - contains your login and signup functionality
19+
* `bugfix/payment-validation` - contains fixes for payment processing errors
20+
21+
Each branch maintains its own set of changes without affecting the other. You can switch between branches to work on different tasks, and later merge the completed branches back into your main branch.
22+
23+
### View current branch
24+
25+
The current branch appears in several places in VS Code:
26+
27+
* **Status Bar**: shows the current branch name and allows quick branch switching
28+
* **Repositories view**: displays the current branch in the repository header
29+
* **Source Control Graph**: visually represents branch relationships and history
30+
31+
![Screenshot showing the current branch displayed in the Status Bar and Source Control view.](images/branches-worktrees/current-branch.png)
32+
33+
### Switch between branches
34+
35+
Switching to a different branch is called "checking out" a branch in Git terminology. When you check out a branch, Git updates your working directory to match that branch's state.
36+
37+
To switch to a different branch:
38+
39+
1. Select the branch name in the Status Bar, or run the **Git: Checkout to** command from the Command Palette (`kb(workbench.action.showCommands)`).
40+
41+
2. Choose from the list of available branches:
42+
* **Local branches**: Branches that exist on your local machine
43+
* **Remote branches**: Branches from the remote repository that you can check out locally
44+
* **Recent branches**: Recently used branches
45+
46+
> [!TIP]
47+
> If you have uncommitted changes when switching branches, Git might prevent the switch to avoid losing work. Consider committing your changes or using a [stash](#stash-management) before switching.
48+
49+
### Create new branches
50+
51+
Create a new branch to start working on a feature or experiment:
52+
53+
1. Select the branch name in the Status Bar or run **Git: Create Branch** from the Command Palette.
54+
55+
1. Enter a name for your new branch. Use descriptive names like `feature/user-authentication` or `bugfix/login-error`.
56+
57+
> [!TIP]
58+
> VS Code can generate random branch names for you. Configure this with the `setting(git.branchRandomName.enable)` and `setting(git.branchRandomName.dictionary)` settings.
59+
60+
1. Choose the source branch (usually `main` or `develop`) from which to create the new branch.
61+
62+
![Screenshot showing the create branch dialog with branch name input and source branch selection.](images/branches-worktrees/scm-create-branch.png)
63+
64+
VS Code switches to the new branch after creation.
65+
66+
> [!TIP]
67+
> If you use the [GitHub Pull Requests and Issues](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github) extension, you can create branches directly from GitHub issues, which gets you started working in a new local branch and automatically prefills the pull request for you.
68+
69+
### Rename and delete branches
70+
71+
To rename the current branch:
72+
73+
1. Run **Git: Rename Branch** from the Command Palette or select it from the **More Actions** (...) menu.
74+
1. Enter the new branch name.
75+
76+
To delete a branch:
77+
78+
1. Switch to a different branch (you can't delete the currently active branch).
79+
1. Run **Git: Delete Branch** from the Command Palette or select it from the **More Actions** (...) menu.
80+
1. Select the branch to delete from the list.
81+
82+
You can also delete a remote branch by using the matching **Delete Remote Branch** action.
83+
84+
> [!CAUTION]
85+
> Deleting a branch permanently removes it from your local repository. Make sure the branch has been merged or you no longer need the changes.
86+
87+
### Merge and publish branches
88+
89+
When your feature is complete, merge it back into the main branch:
90+
91+
1. Switch to the target branch (usually `main` or `develop`).
92+
1. Run **Git: Merge Branch** from the Command Palette.
93+
1. Select the branch to merge.
94+
95+
To publish a branch to your remote repository, use the **Publish Branch** action.
96+
97+
VS Code shows the merge result in the Source Control view. If there are conflicts, VS Code highlights them and provides tools to resolve them. Learn more about [resolving merge conflicts](/docs/sourcecontrol/merge-conflicts.md).
98+
99+
## Working with Git worktrees
100+
101+
VS Code has built-in support for [Git worktrees](https://git-scm.com/docs/git-worktree), making it easy to manage and work with multiple branches at the same time.
102+
103+
### Understanding worktrees
104+
105+
A worktree is a separate checkout of a Git branch in its own directory. This allows you to have multiple working directories for the same repository, each on a different branch. Worktree functionality is especially useful for:
106+
107+
* Work on multiple features simultaneously in separate folders
108+
* Run different versions of your application side by side
109+
* Compare implementations across branches
110+
111+
### Create a worktree
112+
113+
To create a new worktree in VS Code:
114+
115+
1. Open the **Source Control Repositories** view from the Source Control view.
116+
117+
![Screenshot showing the Source Control Repositories view with multiple repositories listed.](images/branches-worktrees/source-control-view-repositories.png)
118+
119+
1. Right-click on your repository and select **Worktree** > **Create Worktree**.
120+
121+
![Screenshot showing the worktree context menu in the Source Control Repositories view.](images/branches-worktrees/worktree-create.png)
122+
123+
1. Follow the prompts to choose a branch and location for the new worktree.
124+
125+
VS Code creates a new folder for the worktree at the specified location and checks out the selected branch into that folder.
126+
127+
The new worktree appears as a separate entry in the **Source Control Repositories** view.
128+
129+
### Switch between worktrees
130+
131+
VS Code can display multiple repositories (including worktrees) simultaneously:
132+
133+
* Each worktree appears as a separate repository in the **Source Control Repositories** view
134+
* You can open multiple VS Code windows, each pointing to a different worktree
135+
* Use **File** > **Open Recent** to quickly switch between worktree directories
136+
137+
### Open a worktree
138+
139+
There are multiple ways to open a worktree:
140+
141+
* Directly open the folder associated with the worktree in VS Code. VS Code automatically detects that it's a worktree of an existing repository.
142+
143+
* Right-click the worktree in the Source Control Repositories view and select **Open Worktree in New Window** or **Open Worktree in Current Window**.
144+
145+
* Run the **Git: Open Worktree in Current Window** or **Git: Open Worktree in New Window** command in the Command Palette and select the desired worktree.
146+
147+
### Compare and migrate changes from a worktree
148+
149+
When you make changes in a worktree, you can compare those changes with your main workspace and bring worktree changes back into your main repository.
150+
151+
1. In the Source Control view, right-click a changed file in the worktree and select **Compare with Workspace** to see the differences side-by-side.
152+
153+
![Screenshot showing the compare with workspace option in the worktree context menu and side-by-side diff view.](images/branches-worktrees/worktree-compare-changes.png)
154+
155+
1. After reviewing, use the **Migrate Worktree Changes** command from the Command Palette to merge all changes from a worktree into your current workspace.
156+
157+
## Next steps
158+
159+
* [Staging and Committing](/docs/sourcecontrol/staging-commits.md) - Learn about committing changes within branches
160+
* [Merge Conflicts](/docs/sourcecontrol/merge-conflicts.md) - Handle conflicts when merging branches
161+
* [Repositories and Remotes](/docs/sourcecontrol/repos-remotes.md) - Work with remote branches and collaboration
162+
* [Collaborate on GitHub](/docs/sourcecontrol/github.md) - Use GitHub pull requests with your branch workflow

docs/sourcecontrol/github.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ The Status bar also shows the active issue and if you select that item, a list o
173173

174174
You can configure the name of the branch using the **GitHub Issues: Issue Branch Title** (`setting(githubIssues.issueBranchTitle)`) setting. If your workflow doesn't involve creating a branch, or if you want to be prompted to enter a branch name every time, you can skip that step by turning off the **GitHub Issues: Use Branch For Issues** (`setting(githubIssues.useBranchForIssues)`) setting.
175175

176+
> [!TIP]
177+
> Learn more about [working with branches](/docs/sourcecontrol/branches-worktrees.md) to understand branch management, switching between branches, and organizing your development work.
178+
176179
Once you are done working on the issue and want to commit a change, the commit message input box in the **Source Control** view will be populated with a message, which can be configured with **GitHub Issues: Working Issue Format SCM** (`setting(githubIssues.workingIssueFormatScm)`).
177180

178181
## GitHub Repositories extension
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)