DEV Community

Cover image for Check file's git history even if renamed/moved
Georgios Kampitakis
Georgios Kampitakis

Posted on • Updated on

Check file's git history even if renamed/moved

From time to time, I find myself that I want to rename or move a file to a different folder, but I am reluctant. The reason is the git history of a file is more important than you might think, so moving it or renaming it, will mean you are going to lose the file history.

How to check a file history even if renamed/moved

git has a really useful flag that can help us.

You can check a file's history with git log and pass the --follow flag.

In practise for a project go-snaps, if you run

# passing --pretty=oneline for brevity 
git log --pretty=oneline internal/test/test.go
Enter fullscreen mode Exit fullscreen mode

will show

<sha-id> chore: add examples for MatchJSON
<sha-id> feat: implement MatchJSON snapshot function (#49)
Enter fullscreen mode Exit fullscreen mode

but running with the --follow flag

git log --pretty=oneline --follow internal/test/test.go
Enter fullscreen mode Exit fullscreen mode

will result in

<sha-id>  chore: add examples for MatchJSON
<sha-id>  feat: implement MatchJSON snapshot function (#49)
<sha-id>  chore: change file structure
<sha-id>  fix: update deps and add more tests (#39)
Enter fullscreen mode Exit fullscreen mode

There can be more things before moving a file as you can see in this example.

Why file history can be important you ask

In a commit ( or a series of commits ) there can be a lot of information that can explain decisions that were taken and why the code has evolved as it is right now. This information can be as valuable as the code itself so you can understand why I find --follow useful.


That was my TIL 😃

Top comments (0)