| by Arround The Web | No comments

How to Reset Submodule to Checkout State in Git

In Git, submodules are a way to include one Git repository as a subdirectory in another Git repository. Resetting a submodule repository in Git can help the developers for updating and modifying the submodule to a specific commit, fix the issue of a broken submodule, or undo changes to the submodule. It is an important tool for managing submodules in Git repositories.

This post will provide the method for resetting the submodule to the checkout state in Git.

How to Reset Submodules to Checkout State in Git?

Resetting a Git submodule to its checkout state is useful when users want to undo changes to a submodule or update it to the latest version.

To reset the submodule to the checkout state in Git, follow the given procedure:

    • Move to the Git local repository.
    • List available content using the “ls” command.
    • Navigate to the submodule directory.
    • Reset the submodule directory and check out the “main” state of the cloned repository.
    • Lastly, clean the sub-repository.

Step 1: Redirect to Git Local Directory

First of all, run the “‘cd” command along with the repository’s path and move toward it:

cd "C:\Users\user\Git\demo1"

 
Step 2: View Available Data

Next, list all the available content by executing the “ls” command:

ls

 
The below-stated output shows that all the data has been listed successfully. There are two submodule directories in this repository. We have selected the “submodule-demo1” repository for further use:


Step 3: Switch to Submodule Directory

Go to the submodule directory with the help of the “cd” command:

cd submodule-demo1

 

Step 4: Reset Submodule Directory

Now, reset the submodule directory by executing the “git reset –hard” command. It will reset the current branch, and clear all modifications from the working directory and staging area:

git submodule foreach git reset --hard

 
It can be noticed that all changes have been reset and HEAD move to the “testrepo” which is the submodule inside this repository:


Step 5: Checkout to Main Branch and Pull Changes

Next, switch to the main branch and pull all changes with the help of the “git submodule foreach” command along with the “git checkout main; git pull”:

git submodule foreach "git checkout main; git pull"

 
It can be noticed that the branch of the submodule repository has been up to date with the cloned repository “testrepo” successfully:


Step 6: Clean the Submodule

Lastly, clean the submodule by executing the “git clean -f” command along with the “git submodule foreach”:

git submodule foreach git clean -f

 ;
The resultant output indicates that the submodule repository has been cleaned and entered into the “testrepo”:


That’s all about resetting the submodule to the checkout state in Git.

Conclusion

To reset the submodule to the checkout state in Git, first, move to the Git local repository. Next, list all available data using the “ls” command and navigate to the submodule directory. After that, reset the submodule directory and check out the state of the cloned repository. Lastly, clean the sub-repository. This write-up elaborated on the method for resetting the submodule to the checkout state in Git.

Share Button

Source: linuxhint.com

Leave a Reply