While pushing the data from staging to the working repository for saving purposes, developers need to commit them along with the commit message for later use. Git commits SHA-hash refers to a snapshot of a file or a group of files in the Git repository. Additionally, developers are allowed to switch to a particular commit.
This guide explained the process of switching the current Git repository to a particular commit.
How to Switch the Git Repository to a Particular Commit?
To switch from the Git Local repository to the desired commit, use the below procedure:
- Navigate to the Git repository.
- View its log history.
- Choose a particular commit and copy its SHA-hash.
- Use the βgit checkout <commit-id>β command.
- Switch back quickly to the Git repository, run the βgit switch –β command.
Step 1: Switch to Git Repository
First, use the βcdβ command and go to the particular Git local repository by providing its path:
Step 2: Check Git Commit Log History
Then, check the log history of the current repository through the βgit log .β command:
Here, we have selected the highlighted commit id for switching:
Step 3: Checkout to Particular Commit
Next, execute the βgit checkoutβ command along with the selected commit id:
According to the below output, the HEAD pointer moves to the desired commit id:
Step 4: Switch HEAD Pointer to Previous Position
If you want to move the HEAD pointer to the previous position quickly then execute the provided command:
It can be observed that HEAD switched back to the repository branch:
Thatβs all! We have efficiently compiled the process of switching the current Git repository to a particular commit.
Conclusion
To switch from the current Git repository to a particular commit, first, navigate to the Git desired repository and view its log history. Then, select a particular commit and copy its commit id. Next, run the βgit checkout <commit-id>β command. If required to switch back immediately to the Git repository, execute the βgit switch –β command. This guide illustrated the method of switching the current Git repository to a particular commit.