Get the name of the file that was changes recently in a git folder

1.1k Views Asked by At

I am trying to get the name of a file name that has the recent change. For example a folder has three files file1.txt, file2.txt and file3.txt. I am trying to have a powershell script that captures the name of a file based on the git log or rev-list and select the name of it. Also if someone adds another file called file4 then it has to be selected by the variable.

cd folder
  $latest_file = git log -n1  --pretty="format:" --name-only -- *.txt
# or $latest_file = git show --name-only $(git rev-list -1  --all *.sql)
cp $latest_file "some/another/folder/"

The problem with git log -n1 is that it can capture the name of the file only if file1.txt is changed while if I change file2.txt then it fails,however if I change git log -n2 then it captures the chagne in file2 but then if file1 is changed or if file4 is added then it fails.

Problem with git rev-list -a --all is that it gives more output then just a file name and don't know how to use pretty format like in log .

Thank you!

1

There are 1 best solutions below

3
On BEST ANSWER

I think I got it working by editing the code as below:

cd folder
   $latest_file = git log -1 --pretty="format:" --name-only *.txt

cp $latest_file "some/another/folder/"

Every time I post a question, I find answers within some hours :D