I'm working on a Java project and I need to understand which files have been modified to fix bugs.
The versioning of the project I'm working on is managed through Git.
Until now I have been able to get the commit that was made to fix the bug, however starting from this how can I get the files that have been modified?
I probably have to run the diff
between the commit that was made to fix the bug and another commit, but what is this other commit?
Can I get it by going up the commit graph? If so, how?
All this has to be done in Java ... to interact with GitHub I'm using the jgit
library.
You can use the git diff
with --name-only
flag comparing two commits to check the changes between them.
For example, if you want to compare the changes in the last 5 commits you can use HEAD reference by doing:
git diff --name-only HEAD HEAD~5
where the first commit reference is the newest one.
If you want to compare the changed files from a specific commit with the HEAD commit, you can get it by doing:
git diff --name-only HEAD 8614c3d70397f03ab504de8d23e8255d52fa3ed1
where 8614c3d70397f03ab504de8d23e8255d52fa3ed1 is your commit hash by example.
Since you want to check the file that was changed in one specific commit, you can do using the same logic by doing:
git diff --name-only COMMIT_HASH COMMIT_HASH~1
So if the bugfix commit has the hash 8614c3d70397f03ab504de8d23e8255d52fa3ed1
for example, you can check the files changed in this commit by doing:
git diff --name-only 8614c3d70397f03ab504de8d23e8255d52fa3ed1 8614c3d70397f03ab504de8d23e8255d52fa3ed1~1