DEV Community

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

Posted on • Edited on

3

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 😃

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay