- What is
GitinVS Code - Clone the repository
- Switch to the
<branch> - Switch to a new branch
- Fetch the latest changes using the
VS Code Terminal - Detect conflicts
- Hard reset the
<branch> - Resolve a merge conflict
- Pull changes from the
<branch>on<remote> - Pull changes from
<branch>on<remote>and rebase - Stage changes using the
Source Control - Unstage specific changes
- Commit changes
- Undo commits
- Publish the branch
- Push more commits
VS Code has built-in Git support and can be extended with GitLens for advanced operations. This page covers common Git workflows in VS Code, including cloning, branching, committing, and pushing.
Docs:
Note
See <repo-url>, <repo-name>.
- Method 1: Clone the repository using the
VS Code Terminal - Method 2: Clone the repository using the
Command Palette
-
To clone the repository,
git clone <repo-url> <repo-path>Replace:
<repo-url><repo-path>with the path where you want to clone the repository
Example:
git clone https://github.com/microsoft/vscode ~/vscodeSee Home directory (
~).fatal: destination path <directory-path> already exists and is not an empty directory. -
To verify that the repository isn't empty,
ls <repo-path>The output should be the list of names of files in the repository.
Example:
ls ~/vscode
- Run using the
Command Palette:Git: Clone. - Click
Clone from GitHub. - Allow the extension to sign in.
- Paste the
<repo-url>. - Select the repo.
- Choose a directory where to clone the repository.
- Confirm the choice.
- Method 1: Switch to the
<branch>using theVS Code Terminal - Method 2: Switch to the
<branch>usingGitLens
-
To switch to the
<branch>,git switch <branch>Replace the placeholder
<branch>.Example:
git switch main
- Run using the
Command Palette:GitLens: Git Switch to... - Select the
<branch>.
- Method 1: Switch to a new branch using
GitHub - Method 2: Switch to a new branch using the
VS Code Terminal - Method 3: Switch to a new branch using
GitLens
-
Copy the command provided by
GitHub.It looks like this:
git fetch origin git checkout <branch>Replace
<branch>.
-
To create and switch to a new branch,
git checkout -b <branch>Replace
<branch>.
- Run using the
Command Palette:GitLens: Git Create Branch.... - Select
mainas the base branch. - Write the new branch name (let it be
<branch>). - Press
Enterto confirm. - Select
Create & Switch to Branch.
-
To fetch the latest changes from
<remote>,git fetch <remote> <branch>Replace the placeholders:
Example:
git fetch origin main
It can happen that commits in <branch> on <remote> are different from commits on the <branch> in the cloned repo on your computer.
- Method 1: Detect conflicts using
GitLens - Method 2: Detect conflicts using the
VS Code Terminal
-
To compare the local
<branch>with the<branch>on<remote>,git statusIf the output contains
Your branch is behind,Your branch is ahead, orhave diverged, there are differences between the local and remote branches.
-
Look at the
Status Bar.You should see that there is a non-zero number of commits to pull from
<branch>on<remote>.
Note
This operation will make your local <branch> match the <branch> on <remote>.
Caution
This operation discards all uncommitted changes and local commits that are not on the <remote>.
-
To hard reset the local
<branch>,git reset --hard <remote>/<branch>Replace the placeholders:
Example:
git reset --hard origin/main
Resolve a merge conflict.
- Method 1: Resolve a merge conflict using
VS Code - Method 2: Resolve a merge conflict using
GitLens - Method 3: Resolve a merge conflict using the
VS Code Terminal
VS Code has a built-in merge conflict editor.
-
Open a file with conflicts.
-
Use the inline options that appear above the conflict markers:
Accept Current Change— keep your branch's version.Accept Incoming Change— keep the other branch's version.Accept Both Changes— keep both versions.
-
Save the file.
-
To stage the resolved file,
git add <file-path> -
To continue the merge,
git merge --continue
Docs:
If you see a pull error like the one below, resolve the conflicts:
For each conflicting file, complete the following steps:
- Open the
Source Control. - Go to
Merge Changes. - Click a conflicting file.
- Click
Resolve in Merge Editor. - Accept the changes that you want to keep.
- Click
Complete Merge. - Open the
Source Control. - Click
Continue.
Note
If there are more conflicts, VS Code shows Merging (1/3) or Rebasing (1/3) (or similar).
Repeat the steps above for each remaining conflict.
-
To see which files have conflicts,
git statusFiles with conflicts are listed under
Unmerged paths. -
Open each conflicted file.
-
Find the conflict markers (
<<<<<<<,=======,>>>>>>>). -
Edit the file to keep the correct content.
-
Remove all conflict markers from the file.
-
To stage the resolved file,
git add <file-path> -
To continue the merge,
git merge --continue
- Method 1: Pull changes from
<branch>on<remote>using theVS Code Terminal - Method 2: Pull changes from
<branch>on<remote>usingGitLens
-
To pull changes from
<branch>on<remote>,git pull <remote> <branch>Example:
git pull origin main
- Run using the
Command Palette:GitLens: Pull.
- Method 1: Pull and rebase using the
VS Code Terminal - Method 2: Pull and rebase using
GitLens
-
To pull changes from
<branch>on<remote>and rebase onto it,git pull --rebase <remote> <branch>Example:
git pull --rebase origin main
- Run using the
Command Palette:GitLens: Pull. - Select
Pull with Rebase. - If
GitLensdoes not show any error, the rebase is complete.
- Method 1: Stage all changes in a specific file
- Method 2: Stage all changes in specific files
- Method 3: Stage specific changes in a specific file
- Open the
Source Control. - Go to
Changes. - Hover over the file name.
- Click the
+icon next to the file name.
- Open the
Source Control. - Go to
Changes. - Select multiple files by holding
Ctrland clicking each file. - Right mouse click the selected files.
- Click
Stage Changes.
- Open the
Source Control. - Go to
Changes. - Click a file to open it.
- Select changed lines in the editor (red-green).
- Right mouse click the selected lines.
- Click
Stage Selected Ranges.
- Open the
Source Control. - Go to
Staged Changes. - Click a file.
- Select changed lines in the editor (red-green).
- Right mouse click the selected lines.
- Click
Unstage Selected Ranges.
Note
Commit message format is: type: short description
Common types:
fix:— bug fixesfeat:— additions (e.g., new feature)docs:— documentation changes
- Method 1: Commit using the
VS Code Terminal - Method 2: Commit using
Source Control
-
Open the
VS Code Terminal. -
To stage your changes,
git add <file-path>See
<file-path>.Example:
git add README.mdExample (path with spaces):
git add 'path/some image.svg' -
To commit your changes,
git commit -m '<type>: <short description>'Example:
git commit -m 'docs: add architecture diagram'
- Open the
Source Control. - Write a commit message.
- Click
Commit.
Note
There can appear a merge conflict when you try to undo.
- Method 1: Undo commits using the
VS Code Terminal - Method 2: Undo commits using
GitLens
-
To undo the last commit and keep the changes staged,
git reset --soft HEAD~1Your changes are staged now.
-
To stage more changes,
git add some-file -
To commit using the previous message,
git commit -C ORIG_HEAD
See Undo commit on the current branch.
- Method 1: Publish using the
VS Code Terminal - Method 2: Publish using
GitLens
-
To publish the branch to
origin,git push -u origin <branch>
- Open the
Source Control. - Click
GITLENSto open theGitLenspanel. - Click the
Commitsicon. - Click the
Publish Branchicon to the right ofPublish <branch> to GitHub. - Press
Enterto confirm.
- Method 1: Push using the
VS Code Terminal - Method 2: Push using
GitLens
-
To push commits to the remote,
git push
- Open the
Source Control. - Click
GITLENS. - Click the
Commitsicon. - Click the
Pushicon to the right ofCOMMITS.

