Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you go about reverting a single file to previous commit state using git? [duplicate]

I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous version.

I have done a git log along with a git diff to find the revision I need, but just have no idea how to get the file back to its former state in the past.

like image 443
Hates_ Avatar asked Oct 18 '08 23:10

Hates_


People also ask

How do I revert to a previous state in git?

git reset --hard This command reverts the repo to the state of the HEAD revision, which is the last committed version. Git discards all the changes you made since that point. Use the checkout command with two dashes, then the path to the file for which you want to revert to its previous state.

How do I revert to a previous version of a file in git?

To move HEAD around in your own Git timeline, use the git checkout command. There are two ways to use the git checkout command. A common use is to restore a file from a previous commit, and you can also rewind your entire tape reel and go in an entirely different direction.

How to revert changes to a file in Git?

To revert the changes to a file in an arbitrary commit, git revert $thecommit # revert the whole commit git reset --hard @ {1} # in what turns out to have been a throwaway commit git checkout @ {1} $thatfile # take what you want but if the unwanted changes are in the most recent commit you can just check out the unaltered version directly with

How to go back to previous commit in Git?

Whichever option you use, take a note of the ID of the commit you want to revert to. Use git checkout & the ID (in the same way you would checkout a branch) to go back:

How do I find the version of a file in Git?

First of all, git doesn't keep version numbers for individual files. It just tracks content - a commit is essentially a snapshot of the work tree, along with some metadata (e.g. commit message). So, we have to know which commit has the version of the file we want.

How do I revert a file to a previous commit?

If you want to revert a particular file to a previous commit, you must first see all commits made to that file. In a situation where the file is located in another folder, you can either navigate your terminal to the folder or use the file path in the command as seen below:


2 Answers

Assuming the hash of the commit you want is c5f567:

git checkout c5f567 -- file1/to/restore file2/to/restore 

The git checkout man page gives more information.

If you want to revert to the commit before c5f567, append ~1 (where 1 is the number of commits you want to go back, it can be anything):

git checkout c5f567~1 -- file1/to/restore file2/to/restore 

As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).


There is also a new git restore command that is specifically designed for restoring working copy files that have been modified. If your git is new enough you can use this command, but the documentation comes with a warning:

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

like image 164
Greg Hewgill Avatar answered Sep 23 '22 23:09

Greg Hewgill


You can quickly review the changes made to a file using the diff command:

git diff <commit hash> <filename> 

Then to revert a specific file to that commit use the reset command:

git reset <commit hash> <filename> 

You may need to use the --hard option if you have local modifications.

A good workflow for managaging waypoints is to use tags to cleanly mark points in your timeline. I can't quite understand your last sentence but what you may want is diverge a branch from a previous point in time. To do this, use the handy checkout command:

git checkout <commit hash> git checkout -b <new branch name> 

You can then rebase that against your mainline when you are ready to merge those changes:

git checkout <my branch> git rebase master git checkout master git merge <my branch> 
like image 24
Chris Lloyd Avatar answered Sep 22 '22 23:09

Chris Lloyd