Git clean your repository working tree before checkout – Code Example

Total
0
Shares

VSCode throws error “clean your repository working tree before checkout” when you have unstaged changes and trying to push your code.

In this article we will look at the ways in which you can resolve this issue.

Why Vscode throws this error?

First of all, this is a git error. It is thrown by Vscode to indicate that the state of repository is having some issues and you need to check that out. Generally this happens due to unstaged changes in your workspace and you still try to push.

The error dialog appears like this –

Please clean your repository working tree before checkout error dialog vscode.

Solutions

Few solutions to this problem are –

  1. Stash your changes, pull the code and pop the stash.
  2. Create a temporary branch and push code there.
  3. Force push code using -f flag.
  4. Remove unstaged changes.

Let’s check each solution in details –

Stash your changes, pull the code and pop the stash

To stash in Vscode –

  1. open source control window
  2. Click on 3 dots (...). It will open a menu dialog.
  3. Choose stash from it and it will open a sub-menu.
  4. Select Stash from sub-menu.

Check it in vscode screenshot –

stash in vscode

After stash, you can pull the code from repository. This will keep your local code in sync and up to date with remote.

Now you can unstash your code and merge with it. Just like we stash in vscode, we can choose pop stash from sub-menu.

Check it out in screenshot –

pop stash in vscode

You can also use the command line for the same.

Command for git stash –

git stash -a

Command for git pop –

git stash pop

Create a temporary branch and push code there

Another solution is to create a temporary branch and commit your code there. You can do this using vscode UI or command line.

Using Vscode UI, you can create a branch by –

  1. Open Source Control panel
  2. Select 3 dots. This will open the menu dialog.
  3. Here you will see a menu item as branch. Selecting it will open a sub-menu.
  4. Select Create Branch from sub-menu.

Check it out in screenshot –

create branch from vscode

After creating branch, you can switch to it and commit your code into that. Afterwards, switch back to the main branch and sync your local repo with remote and then merge the code.

To merge branch from vscode UI, choose Merge Branch menu item from sub-menu. Check this screenshot –

merge branch from vscode UI

After completing your work, you can delete your branch using Vscode UI –

delete branch using Vscode UI

Now let’s check it with console commands.

Creating and switching to a new branch –

git switch -c new-branch

Commit your code to this new-branch –

git commit -m "this is some message"

Switch to main branch –

git switch main

Merge new-branch with main –

git merge new-branch

Delete your new-branch –

git branch -d new-branch

Force push code using -f flag

If your working tree is clean but still you are getting the error to clean your working tree before checkout, then you can try force push. Here is the command –

git push -f origin

Remove unstaged changes – git clean

If the unstaged changes are of less use to you then you can remove them. You can do it from vscode UI as well as console.

From Vscode UI –

  1. Go to source control panel
  2. Click on 3 dots.
  3. Select Changes option from menu
  4. Select Discard All Changes from sub-menu.

Check this out in screenshot –

Discard all unstage changes using vscode UI

The same could be done using command line –

git clean -fdx

Conclusion

In this article we saw different ways to solve vscode git error “clean your repository working tree before checkout”. After applying these solutions, you will be able to completely resolve your issue.