Select Page

Introduction

The git checkout command is responsible for changing your current branch into something else inside a terminal. When you create a new branch using git, you’d need to use the checkout command to navigate to it. However, the checkout command is not only used for navigating to branches, it actually has a lot more uses than you might expect. 

Another crucial use case of the checkout command is to restore changed or deleted files, as well as to remove the untracked or unstaged files, and we’ll tell you how to do that right here in this article!. Also, if certain things you want to achieve are not possible with the checkout command, it most certainly is with other similar commands. So get your notepads ready!


Git checkout all files

The easiest way to go about checking out all the files that you have made changes in is to use the following command from the root of your project’s directory:

$ git checkout -- .

This will checkout all the files currently present in the HEAD. Another common way to achieve a similar result is to use this command instead:

$ git reset --hard

Git checkout all branches

There is no direct command available in git to achieve this. However, a generous guy with the username of “Wilmarvh” in GitHub was kind enough to share a script for this. It’s a bash script that you can execute from the command line. To use this, first visit the following link under his GitHub page:

https://gist.github.com/wilmarvh/95fe7daed6ee6a63d811677e040ae421

From here, you can either copy the script and create your own file with the .sh extension and its permission for executing in your terminal shell, or you can also download the script using the “Download ZIP” to download a zipped version of the script. Now extract the zip file, and then copy this script file to the root directory of your project. From there, you can execute the script using:

$ ./git checkout-all-branches.sh

Git checkout all deleted files

If you haven’t already committed changes after deleting files, then using the same command as the first section should help you recover the deleted files instantly:

$ git checkout -- .

However, if you’ve already committed after deleting the files, then you’ll want to use:

$ git reset --hard HEAD~

Git checkout all submodules

Git has a dedicated submodule command available for this task. Simply navigate to the root of the project directory that you have cloned and run:

$ git submodule update --init --recursive

Git checkout all unstaged files

Checking out the unstaged files are exactly the same as the first section, which is to use:

$ git checkout -- .

It does seem kinda weird how a single command is handling different tasks, but in git, it is what it is. One downside to this is that if you have changes made to files, and also some unstaged files to be removed at the same time, then this command will do both, which would be a problem in case you don’t want that. 


Git checkout all untracked files

If you want to remove only the untracked files and not the staged ones, then the best command for you to use is:

$ git clean

Git checkout all files from current branch

This is probably getting repetitive, but once again, the command from the first section is the what you’re looking for:

$ git checkout -- .

Similarly, you may also wish to use the reset command:

$ git reset --hard

Git checkout all files from another branch

To checkout all the files from another branch than your current one, then all you’d need to do is specify the branch name and use:

$ git checkout <branch_name> .

As always, you can also use the reset command, the only difference being that you need to include the name of the branch:

$ git reset --hard <branch_name>

Git checkout all files with extension

Git understands wildcards, which is probably a big plus. What this means is that you won’t pipe in a whole different command like grep or sed in order to first extract the file types and then do a checkout on them. Instead, you can use the wildcard (*) and specify the extension, and use:

$ git checkout -- *.<extension>

For example, if you wish to checkout all the files that have the extension .js, then you’d simply want to use this command from the root of your project directory:

$ git checkout -- *.js

Git checkout all local changes

You can use “git reset –hard” to remove all the local changes made to files, but there’s a problem. Git reset will only work given that the changes have been added to the index. Say for example, you just created a new file, but did not write anything to it, and you use the git reset command. What you’ll see is that the files that had been changed previously (which means they have been added to the index) will be the only ones being removed, while the new file you just created will stay untouched, as it has not yet been added to the index in any means. 

To remove all the local changes including the new file you just created, the best command to use would be:

$ git clean -fxd

Conclusion

This concludes all the ways you could use the git checkout command to achieve different results based on your scenario. One last tip I’d like to leave here is the fact that just like you can remove files using “git checkout — .” you can also use it to restore the files you may have deleted, as it essentially means to restore the “previous state” of your project. If the last thing you did was to remove files, then using the command will restore them, but if the last thing you did was to make changes to a file, then using the command will remove them. Pretty banal and complicated stuff, isn’t it?