How to checkout old git commit including all submodules recursively?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Git is a powerful version control system widely used by developers around the world. It allows you to track changes to your code, collaborate with others, and maintain a history of all modifications made to your projects. One of the key features of Git is its ability to manage submodules—repositories nested inside another repository, which could be essential when working with complex projects. Occasionally, you may need to check out an old commit, including all submodules, to investigate past states of your project. In this article, we'll guide you through the process of checking out an old Git commit including all submodules recursively.
Understanding Submodules in Git
Submodules are like pointers to specific commits of a repository. They are beneficial when a project depends on another repository, allowing each of your Git repositories to act as a component within a larger project. However, managing submodules can sometimes be tricky, especially when trying to synchronize submodules with a particular commit in your main repository.
Key Concepts
- Commit Hash: A unique identifier generated by Git for each commit.
- Submodule: A repository nested inside another repository.
- Recursive Checkout: Checking out not just the parent repo but also its submodules to specific commits.
Checking Out an Old Git Commit
To revisit an old state of your project in Git, including all submodules, follow the steps outlined below.
Step-by-Step Process
- Identify the Target Commit:
- First, determine which specific commit you wish to check out. You can do this by using the `git log` command in your terminal:
- This will display the commit history along with each commit's hash, author, date, and message.
- Once you've identified the commit hash you want to check out, use:
- Replace ```<commit_hash>``` with the actual hash of the commit you are interested in.
- After checking out the main repository, the submodules may still need to be updated to reflect the state as of that commit. Use:
- This command will initialize, update, and fetch all submodules recursively to match the state in the checked-out commit of the parent repo.
- Confirm that both the main project and submodules have been correctly checked out by listing and examining the directory contents:
- Detached HEAD State: When you checkout an older commit, the HEAD is in a detached state. To make alterations, you will need to create a new branch or switch back to an existing one.
- Exploring Submodules: To explore submodules without checking out, `git submodule foreach 'git log'` can help browse each submodule's commit history.
- Commit Messages: Writing detailed and informative commit messages can significantly ease navigation of your project history.
Related reading
- How to code the maximum set packing algorithm?
- How to compare objects by multiple fields
- How to compare two dates?
- How to compute intersection of N sorted sets?
- How to cherry-pick a range of commits and merge them into another branch?
- How to cherry-pick a range of commits and merge them into another branch?
- How to compute shortest unique prefixes of a set of strings?
- How to compute the absolute minimum amount of changes to convert one sortorder into another?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.