"Works on my machine" / how to reset your local git repository without cloning again.

Tldr: Remove all untracked/ignored files and directories excluding .idea and .vscode folders, list things to remove and ask for a confirmation first:

git clean -x -d -n -e .idea -e .vscode | awk '{print $3}' | xargs -p rm -rf

What?

You know the nightmare - the tests are green locally, but they fail on CI/on your colleague machine/maybe even production.

The first step to debug the issue for a lot of the developers I've worked with is to make sure they have a clean state (git status), then reinstall the dependencies (with JS projects - usually by removing the node_modules first). If that doesn't help they clone the repository so they can start from a completely clean state.

Sounds familiar?

Behold - there is no need for that madness. Let's clean the existing repo in-place, seeing all the potential culprits.

How?

Try:

git clean -x -d -n

It should show something like:

Would remove dist/
Would remove node_modules/
Would remove .idea/

Note: -x considers ignored files as well, -d removes whole ignored/untracked directories

If you are fine with the list, remove the -n (dry run), add -f (force):

git clean -x -d -f

Done!

How about if you see something in the list you don't want to remove. In my case - I don't want to remove the .idea folder, it has no influence on the tests or my app runtime, and removing it while having the project open causes Webstorm to behave in a weird way.

Use -e for that:

git clean -x -d -n -e .idea

What's a bit annoying though now is that you have to keep switching between the two version of the command (-n and -f). If you want a quick way of getting the work done it would be nice if the command just listed what it wants to remove, and then ask you for a confirmation. That way you can add an alias or just copy and paste the command everytime you need to use it, without changing anything. Let's use some Unix magic for that.

We can pipe the result of the git clean command to awk to get the path to a file/directory that would get removed:

git clean -x -d -n -e .idea -e node_modules | awk '{print $3}'

From there, we can pipe it to xargs to pass the paths as arguments to rm utility, we use -p xargs flag to have it ask for confirmation before deleting anything:

git clean -x -d -n -e .idea | awk '{print $3}'  | xargs -p rm -rf

It should show something like:

rm -rf dist/ hello node_modules/?...

Press "y" and enter to execute the echo'ed command.

Alias it

If you are like me and find yourself using this command frequently, it might be helpful to alias it:

git config --global alias.reset-repo "! git clean -x -d -n -e .idea | awk '{print \$3}' | xargs -p rm -rf"

Note the \ before $ sign. Also, if you don't use jetbrains IDE - either remove the -e .idea or change it to something that might be useful to you.

Now whenever you need to do some serious cleaning run:

git reset-repo

Please let us know if you find this helpful, if you have a better way, or whatever comes to your mind after reading. :-)

Thanks!

Łukasz


Let us help you on your journey to Quality Faster

We at Xolv.io specialize in helping our clients get more for less. We can get you to the holy grail of continuous deployment where every commit can go to production — and yes, even for large enterprises.

Here are the things we offer:

Services (we help you)

  • Custom Software Development
    Whether you're looking to create a proof-of-concept or build an entire new capability, we can help you get there at speed and with high quality.
  • Apollo GraphQL Consulting
    We can help in all areas related to Apollo GraphQL and surrounding technologies including Apollo Federation, One-graph unification, Domain Driven Design, µService deployment, automated testing, security, performance, and more.
  • Continuous Delivery Coaching
    We have made it our business to help organizations modernize their technologies stacks and processes to gain speed, stability, and quality improvements, especially in enterprises where legacy systems seem impossible to deal with.

Products (you help yourself)

  • XSpecs
    XSpecs is a full-team collaborative toolset that simplifies the writing of specifications and integrates them into your team's existing workflow. This helps you improves velocity, decreases bugs, and increases quality.
  • Quality Faster
    A complete full-team, full-cycle and full-stack guide to help you and your team increase the speed at which you deliver software while simultaneously increasing quality.

Have something else in mind? Get in touch.


  • Testing with DynamoDB Toolbox

  • Creating Tables with DynamoDB Toolbox

  • Instantly update your Lambda functions (development)