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
git-aliases, awk and shell-functions to the rescue!
git prevision
where
is the number of revisions of the file to rollback for file
.
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