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 –
Solutions
Few solutions to this problem are –
- Stash your changes, pull the code and pop the stash.
- Create a temporary branch and push code there.
- Force push code using
-f
flag. - Remove unstaged changes.
Let’s check each solution in details –
Stash your changes, pull the code and pop the stash
To stash in Vscode –
- open
source control
window - Click on 3 dots (
...
). It will open a menu dialog. - Choose
stash
from it and it will open a sub-menu. - Select
Stash
from sub-menu.
Check it in vscode screenshot –
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 –
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 –
- Open
Source Control
panel - Select 3 dots. This will open the menu dialog.
- Here you will see a menu item as
branch
. Selecting it will open a sub-menu. - Select
Create Branch
from sub-menu.
Check it out in screenshot –
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 –
After completing your work, you can delete your 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 –
- Go to
source control
panel - Click on 3 dots.
- Select
Changes
option from menu - Select
Discard All Changes
from sub-menu.
Check this out in screenshot –
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.