Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - Find commit where file was added

People also ask

How do you see which files were changed in a commit?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

Where do files go when I git commit?

Git is a decentralized version control system, which means that committing does not share your code with anyone else. Instead, when you commit, the files stay locally on your computer and are committed to your local version of the git repository. These files only move to other machines when you push your code.


Here's simpler, "pure Git" way to do it, with no pipeline needed:

git log --diff-filter=A -- foo.js

Check the documentation. You can do the same thing for Deleted, Modified, etc.

https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203

I have a handy alias for this, because I always forget it:

git config --global alias.whatadded 'log --diff-filter=A'

This makes it as simple as:

git whatadded -- foo.js

The below one liner will recursively search through sub directories of the $PWD for foo.js without having to supply and absolute or relative path to the file, nor will the file need to be in the same directory as the $PWD

git log --diff-filter=A -- **foo.js

git log --follow --find-renames=40% --oneline -- foo.js | tail -n 1

The following may not be of your interest, but I think it will help you in the future and is part of the debugging ecosystem in Git:

You can use git-blame to show what revision and author last modified each line of a file, especially a file annotation. Visit https://git-scm.com/book/en/v2/Git-Tools-Debugging-with-Git

For example,

git blame -L 174,190  xx.py

The -L option is to restrict the output of the annotation to lines 174 through 190, so you will see the authors and the commit hash, etc from line 174 until 190 for the file xx.py