Is there a shortened version of the previous version of the file in Git?

I want something like -
git checkout FHEAD~ path/filename

What I'm doing now is to find the previous version of the file through -
git log -1 --skip=1 path/filename

and then git checkout <previous-commit-hash> path/filename

+6
source share
1 answer

A trivial example of an alias for retrieving a previous version of a file.

The trick here is to format the log output with --format to return a hash commit

 [alias] prev = "!f(){ git checkout $(git log -1 --format="%H" --skip=1 -- ${GIT_PREFIX:-.}/$1) -- ${GIT_PREFIX:-.}/$1; git reset ${GIT_PREFIX:-}/$1; }; f" 

Using git log -1 ensures that git prev always returns the original parent file. Choosing the next parent can be more difficult.

Note that a file once written out is immediately added to the index. It does not merge with your existing copy and does not warn you to overwrite local changes. I turned on the git - reset call to make sure that the verification file is only in the working directory and not in the index.

Using GIT_PREFIX allows GIT_PREFIX to enter relative paths, although only relatively recent versions of git support GIT_PREFIX.

change ..

The same with git rev-list .. a very powerful git command designed to move git's ancestral graphs when creating scripts and automation ...

 [alias] prev = "!f(){ git checkout $(git rev-list --max-count=1 --skip=1 HEAD -- ${GIT_PREFIX:-.}/$1) -- ${GIT_PREFIX:-.}/$1; git reset ${GIT_PREFIX:-}/$1; }; f" 
0
source

Source: https://habr.com/ru/post/976824/


All Articles