I am trying write a shell script that does the following:
- Checks the remote git repository for any changes to pull.
- If there are changes in the remote git repository pull those changes.
- 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:
- 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?
- 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.