Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8122763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:56:12+00:00 2026-06-06T05:56:12+00:00

I am writing a Git pre-commit hook client script and I want to filter

  • 0

I am writing a Git pre-commit hook client script and I want to filter through committed files and do something with the deleted ones.

I have found that with the following I can loop over all files included in a commit with the following.

#!/bin/sh

for file in $( exec git diff-index --cached --name-only HEAD )
  do
    echo $file
done

To find deleted files I believe I can use git status --porcelain <file> since it displays the first character as D when the file is deleted.

So what I need is help crafting the syntax to check whether the first character in the output of git status --porcelain <file> is a D.

#!/bin/sh

for file in $( exec git diff-index --cached --name-only HEAD )
  do
    # This line doesn't work but it's the best I've came up with.
    if git status --porcelain $lessFile == *"D"*; then
      echo "$file was deleted"
    fi
done

Or let me know if I’m way off base here. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:56:15+00:00Added an answer on June 6, 2026 at 5:56 am

    try

     $( exec git status --porcelain | egrep "^D" | sed -e 's/^D  //' )
    

    it may have problem for filename with space. or better

    git diff-index --cached --name-status HEAD | awk '$1 == "D" { print $2 }' | while read file
    do
       echo $file
    done
    

    see also this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried writing a git update hook that prevents pushing commits that add files
I'm writing a bash script to add, commit, push all files in a directory.
I'm trying to figure out how to write a pre-commit hook for Git that
I have been writing a script to add untracked files using git add .
I'm writing a git-install.sh script: http://gist.github.com/419201 To get Git's latest stable release version number,
I am writing a bash script where I need to clone a git rep.
I am writing a script that requires checking whether a particular commit is a
I want to use PHP writing git hooks but have some problem. I use
I'm writing a script to run things like git status in each of the
I'm writing Python programs that run other programs, like: my-wrapper-program --foo --bar git commit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.