Please clean your repository working tree before checkout

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
4 min

banner

# Please clean your repository working tree before checkout

The VS Code Git error "Please clean your repository working tree before checkout" occurs when you make changes locally without committing them and try to sync with the remote repository.

To solve the error, stash your changes or commit them before syncing with the remote.

# Commit your changes before syncing

If you are able to, commit your changes before syncing with the remote:

  1. Click on the Source Control icon in the left sidebar or press Ctrl + Shift + G.

  2. Type a commit message and click on the Commit button.

type commit message and click commit

You can also use commands to commit your changes.

shell
# Add the changes to the staging area git add . # Commit your changes git commit -m 'your commit message'

Once you commit your changes, you will be able to sync with the remote.

However, in some cases, you might not want to commit your changes yet.

# Stash your changes

To stash your changes:

  1. Click on the Source Control icon in the left sidebar or press Ctrl + Shift + G.

stash changes vscode

  1. Click on the ellipsis (...) icon in the Source Control panel.

  2. Hover over Stash and select Stash (Include Untracked).

You can also use the Command Palette to stash your changes.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type git stash.

git stash include untracked

  1. Select Git stash (Include untracked).

You can also use the command line to stash your changes.

Open your terminal and issue the following command.

shell
git stash

# Sync with the remote

Once you stash your changes, sync the changes from the remote repository by clicking on the Synchronize Changes button in the bottom left corner.

sync changes with remote

You can also use the Command Palette to sync your changes:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type git sync and select the Git: Sync command.

git sync changes using command palette

# Pop the Stashed changes

Once you sync with the remote, you have to pop the stashed changes.

  1. Click on the Source Control icon in the left sidebar or press Ctrl + Shift + G.

click pop stash

  1. Click on the ellipsis (...) icon in the Source Control panel.

  2. Hover over Stash and select Pop Stash....

  3. Select the stash from the dropdown menu.

The stash will get applied and you will be able to see the changes you previously stashed.

You can also use a command to pop the stash.

shell
git stash pop

using git stash pop command

# Alternatively, you can create a temporary branch

  1. Click on the Source Control icon in the left sidebar or press Ctrl + Shift + G.

vscode git create branch

  1. Click on the ellipsis (...) icon in the Source Control panel.

  2. Type a name for your new branch and hit Enter.

type name for new branch

You can also create a temporary branch by using the command line.

shell
git checkout -b new-branch

Once you create the new branch, make sure you've switched to it.

make sure switched to new branch

Now you have to commit your changes.

  1. Click on the Source Control icon in the left sidebar or press Ctrl + Shift + G.

  2. Type a commit message and click on the Commit button.

type commit message and click commit

You can also use commands to commit your changes.

shell
# Add the changes to the staging area git add . # Commit your changes git commit -m 'your commit message'

Now you can switch to your previous branch and sync with the remote.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type git checkout and select Git checkout to:.

git checkout to

  1. Select the branch you want to switch to.

You can also switch to a different branch by clicking on the name of the current branch in the bottom left corner.

make sure switched to new branch

Once you switch to your previous branch, sync the changes from the remote repository by clicking on the Synchronize Changes button in the bottom left corner.

sync changes with remote

You can also use the Command Palette to sync your changes:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type git sync and select the Git: Sync command.

git sync changes using command palette

Once you sync your changes, you can merge the temporary branch into your existing branch if you want to apply the changes.

  1. Click on the Source Control icon in the left sidebar or press Ctrl + Shift + G.

merge branch

  1. Click on the ellipsis (...) icon in the Source Control panel.

  2. Hover over Branch and click on Merge Branch....

  3. Select your temporary branch from the list.

  4. Verify the branch has been merged successfully.

I've also written an article on how to delete git branches in VS Code.

If you need to configure VS Code as your default Git editor, difftool and mergetool, click on the link and follow the instructions.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.