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 8937841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:29:03+00:00 2026-06-15T10:29:03+00:00

I am trying write a shell script that does the following: Checks the remote

  • 0

I am trying write a shell script that does the following:

  1. Checks the remote git repository for any changes to pull.
  2. If there are changes in the remote git repository pull those changes.
  3. Loops through the files that are new or have been modified.

Through my research I have found some of the necessary commands to do these things but I haven’t been able to get them to work together in a shell script.

Here is a script with some of the commands that I have:

#!/bin/sh

#Check if there are any changed files
git --git-dir="/dir/.git" fetch origin

if git --git-dir="/dir/.git" log HEAD..origin/master --oneline
then

#Output the modified files from the last pull
        git --git-dir="/dir/.git" diff --name-status ORIG_HEAD..

fi

The things that I have not been able to get working with the commands in this script are:

  1. The if statement to check if there are changes or not always is true. I have tried if statements with other git commands and they also are always true. It seems that git does not work like normal shell commands where you get a 0 or 1 response. How can I get a git command like this or other git commands to return the right responses in an if statement?
  2. How can I assign the variables output from the command to see the changed files to an array so that I can loop through them using a for?

If the commands in my script won’t really work in this case what is a better way to do this?

Edit: Sorry should have been more clear when I loop through the changed files I need to pull the changes from the remote repository before I loop through the files so that when I work with these files I have the latest changes.

  • 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-15T10:29:05+00:00Added an answer on June 15, 2026 at 10:29 am

    For your first question, you can use git diff --quiet (or git diff --exit-code, but generally when you’re using it for its exit code you want it not to print output anyhow, and git diff --quiet implies --exit-code) to determine if there have been any changes. That will give you a 1 value if there are changes, and a 0 if there are not. So if you want to have code that will run only if there are changes:

    if ! git --git-dir="/dir/.git" diff --quiet
    then
        # do stuff...
    fi
    

    For your second question, I’d recommend a while read ... loop to read lines from git diff-tree:

    git --git-dir="/dir/.git" diff-tree ORIG_HEAD.. | \
        while read srcmode dstmode srcsha dstsha status srcfile dstfile
        do
            # do something with $srcfile and $dstfile
        done
    

    Note that $srcmode will have an extra : at the beginning, and $dstfile will only have a value if the file was renamed. If you don’t want to worry about renames, pass in --no-renames, and instead of seeing renames you’ll see just the adds and deletes.

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

Sidebar

Related Questions

I'm trying to write a shell script that does a search and replace inside
I am trying to write a shell script to simulate the following problem: File
I'm trying to write a shell script that will make several targets into several
I'm trying to write a shell script that, when run, will set some environment
I am trying to write a wrapper shell script that caches information every time
I am trying to write a simple shell-script that prints out the first parameter
hi I am trying to write a bourne-shell script that takes a name as
I am trying to write a bourne-shell script that takes a directory as a
I am trying to write a bourne-shell script that takes a string as a
I'm a shell noob. I'm trying to write a simple script that'll take a

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.