简体   繁体   中英

How do I go back to an earlier commit of a file using git?

I am working on a file where the my current commit ended up being bad and I want to go back to earlier commits for a specific file?

I did a search it looks like no one has answered a similar question (how to roll back changes in a file in a previous commit in git)....

Hopefully a simple answer?

git reset <commit hash> <filename>

git checkout <commit-id> file(s)

It will overwrite easily so beware with your file wildcards, which do can cover several files at once. For help, you can check out the git log messages with the wanted files as a filter:

git log <file>

Will show only those commits with log messages that involve the <file> searched.

Well, the main thing that is probably confusing you is that git keeps track of complate versions of the entire repository, not just one file. So what you have to do is figure out which version of the repository has the version of the file you are looking for, and roll back to that.

If you just want that old version of the file, but want all the other changes that the repository took in the meantime, you have to do a bit more work.

For complex operations like rollbacks, I'd generally suggest using one of the GUI tools (like git gui), as they let you visualize better what you are getting ready to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM