简体   繁体   中英

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.

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 ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM