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 versio
I have to plug EasyGit here, which is a wrapper to make git more approachable to novices without confusing seasoned users. One of the things it does is give more meanings to git revert. In this case, you would simply say:
eg revert foo/bar foo/baz
Use git log
to obtain the hash key for specific version and then use git checkout <hashkey>
Note: Do not forget to type the hash before the last one. Last hash points your current position (HEAD) and changes nothing.
If you know how many commits you need to go back, you can use:
git checkout master~5 image.png
This assumes that you're on the master
branch, and the version you want is 5 commits back.
git-aliases, awk and shell-functions to the rescue!
git prevision <N> <filename>
where <N>
is the number of revisions of the file to rollback for file <filename>
.
For example, to checkout the immediate previous revision of a single file x/y/z.c
, run
git prevision -1 x/y/z.c
Add the following to your gitconfig
[alias]
prevision = "!f() { git checkout `git log --oneline $2 | awk -v commit="$1" 'FNR == -commit+1 {print $1}'` $2;} ;f"
The command basically
- performs a
git log
on the specified file and- picks the appropriate commit-id in the history of the file and
- executes a
git checkout
to the commit-id for the specified file.
Essentially, all that one would manually do in this situation,
wrapped-up in one beautiful, efficient git-alias - git-prevision
git checkout -- foo
That will reset foo
to HEAD. You can also:
git checkout HEAD^ foo
for one revision back, etc.
In order to go to a previous commit version of the file, get the commit number, say eb917a1 then
git checkout eb917a1 YourFileName
If you just need to go back to the last commited version
git reset HEAD YourFileName
git checkout YourFileName
This will simply take you to the last committed state of the file