Ferit
Ferit

Reputation: 9717

How to Commit For Each File Recursively with Git in Bash?

I am trying to push a lot of big binary files to GitHub and it always fails because of big commit sizes.

So I decided to write a bash script which adds and commits each file recursively under given directory, so I can push them one by one.

This is what I have tried:

#!/bin/sh

for FILE in ${PROJECT_DIR}/*
do
    echo ${FILE}
    git add ${FILE}
    git commit -m "initial commit ${FILE}" 
done

But when file names have spaces or unicode characters, it fails.

I am looking for a robust script for this purpose.

Upvotes: 1

Views: 192

Answers (1)

Inian
Inian

Reputation: 85780

The problem is with lack of appropriate quotes in your git add command. Not enclosing it within double-quotes leaves the variable susceptible to Word-Splitting by the shell i.e. splitting of a string into individual words by the delimiter (default being the whitespace)

shopt -s globstar        
for fileToCommit in ${PROJECT_DIR}/**/*; do
    test -f "$fileToCommit" || continue 
    printf "%s\n" "${fileToCommit}"
    git add "${fileToCommit}"
    git commit -m "initial commit ${fileToCommit}" 
done

Upvotes: 3

Related Questions