Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Dry running Git commands

There are some things that you can do in Git that you might want to test out before actually running them. For instance, you might want to check what will be added to the Git index before running the git add command.

There’s an option called --dry-run that you can use with a few of the Git commands to let you do exactly that. In this article, let’s see how you can use the --dry-run option with a few of the Git commands.

The git add command

So, for instance, if you want to see what will be added to the Git index before running the git add command, you can do so by running the following command.

git add . --dry-run

This will give you the following output.

git add --dry-run

As you can tell, the --dry-run option will show you which files will be added to the Git index before actually running the git add command.

The git clean command

Similarly, you can use the --dry-run option with a lot more destructive commands such as the git clean command to see what will be removed from the working directory before actually running the git clean command.

git clean -df --dry-run

This will give you the following output.

git clean --dry-run

As you can tell, the above command will show that the route.php is not tracked by Git and will be removed from the working directory. So, you’ll be sure that you won’t lose any important files before actually running the git clean command.

The git commit command

You can also use the --dry-run option with the git commit command to see what will be committed before actually running the git commit command.

In this case, we can dry run with various options such as --short and --long to see what will be committed.

git commit --short --dry-run

This will give you the following output.

git commit --short --dry-run

The --short option will show you the commit message and the files that will be committed in a compact format.

The --long option would show the same but with a little more detail.

The git mv command

The --dry-run option can be used with the git mv command to see which files will be moved before actually running the git mv command.

git mv --dry-run

This will give you the following output.

git mv --dry-run

As you can tell, the --dry-run option will show you what it will do before actually running the git mv command which is pretty useful for a destructive command like git mv.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?