Programmer's must-have tip: Efficiently reverting a single file to a specific commit in Git and GitHub

Git and GitHub are used to store your old code, allowing you to roll back and safely restore the previous accurate code if something goes wrong.

Knowing how to revert a single file to a specific commit also becomes critical when collaborating with other developers. This is because, while working on a feature, you may need to modify unrelated files to test it. Manually changing every line of code in these files and creating new commits can mess up the commit history. Restoring the files is a cleaner way.

Find commit id

First, navigate to the shared repository on GitHub and find the file you want to restore. Above the file, you should see a 7-digit commit ID and a date indicating the last commit that modified the file. Note or copy this commit ID as follows:

How do I now check out the actual files of the different commits? First click on "history" and expand, you will see all commits related to this file:

Now select whatever commit id you want to revert, and click the "..." button, then select "View Files", and check the actual files:

locate file path

Next, you need the path from your working directory to the file. This can be found on the same GitHub screen where you found the commit ID of the file. Make sure to only copy the path without the working directory name as it will be the directory you are in when using this file path.

recover files

After opening a terminal and setting up a working directory, use the git checkout command to restore the files. The format of the git command should look like this:

git checkout [commit ID] -- path/to/file
Or for this particular case, it would be:
git checkout 4a023a6 -- gpt4-novel/readme

commit changes

After restoring the files, it is necessary to commit the changes, which in this case is a restoration of a single file. This can be done using the standard commit command:

git commit -m 'commit message'

Finally, the commit is pushed to the remote repository so that the GitHub version of the branch matches the local version.

English original

AI Good Book Recommendations
AI is changing with each passing day, but high-rise buildings cannot be separated from a good foundation. Are you interested in learning about the principles and practice of artificial intelligence? Look no further! Our book on AI principles and practices is the perfect resource for anyone looking to gain insight into the world of AI. Written by leading experts in the field, this comprehensive guide covers everything from the basics of machine learning to advanced techniques for building intelligent systems. Whether you are a beginner or an experienced AI practitioner, this book has you covered. So why wait?

The principles and practices of artificial intelligence comprehensively cover the classics of various important systems of artificial intelligence and data science

Peking University Press, Principles and Practice of Artificial Intelligence Artificial intelligence and data science from entry to proficiency Detailed explanation of machine learning deep learning algorithm principles
 

Guess you like

Origin blog.csdn.net/robot_learner/article/details/130144088