"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 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 slate.

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 versions 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 confirmation. That way you can add an alias or just copy and paste the command every time 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 echoed 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 me know if you have any questions or thoughts in the comments below.


Let us help you on your journey to Quality Faster

We at Xolvio 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.

Feel free to schedule a call or send us a message below to see how we can help.

User icon
Envelope icon

or

Book a call
+
Loading Calendly widget...
  • Add types to your AWS lambda handler

    Lambdas handlers can be invoked with many different, but always complex, event arguments. Add to that the context, callback, matching return type and you basically start listing all the different ways that your function can fail in production.

  • How to expose a local service to the internet

    From time to time you might need to expose your locally running service to the external world - for example you might want to test a webhook that calls your service. To speed up the test/development feedback loop it would be great to be able to point that webhook to your local machine.

  • For loops in JavaScript (vs _.times)

    From time to time I still see a for loop in JavaScript codebases. Linters are frequently angry about them. Let's see how we can replace them.