0

I am making a small automation to add all the .java files in my current directory but it has some flaws.

It pushes each file instead of pushing them all at once, it's okay If it asks the commit message for each file but I tried to git push the files outside the for loop.

#!/bin/bash
 
javafile=*.java
 
for i in $javafile;
do
        if [[ "$i" == "$javafile" ]]
        then
                echo "No .java files"
        else
                git add $i
                echo
                echo "File $i added"
                echo
                echo "Write a message to commit"
                read message
                git commit -m "$message"
                git push origin master
                echo
                echo "#############################################"
                echo "$i pushed successfully"
                echo "#############################################"
        fi
done
8
  • 2
    Do the git add in the loop and then the commit and push outside. Commented Feb 15, 2021 at 14:54
  • 3
    Committing each file separately destroys half the point of using a changeset-based system like git. Much better to do just one commit for your entire unit-of-work. Commented Feb 15, 2021 at 15:19
  • What @CharlesDuffy said. And: Pushing automatically after each commit also throws away other useful git functionality like amending erroneous commits before sharing your work with others. Commented Feb 15, 2021 at 15:33
  • Also, note that it should be read -r message not just read message; as it is, any backslashes in your message will be destroyed. Commented Feb 15, 2021 at 15:35
  • 1
    Anyhow, you're saying that the problem is that git push origin master is inside the loop, but you aren't showing us how you tried to take it out of the loop, or what problem you had when doing that; so it's not clear what you're actually asking here. Why doesn't the most obvious solution (deleting the line from where it is now, and re-adding it at the end of the file) work for you? Commented Feb 15, 2021 at 15:38

1 Answer 1

3

The problem is the git push origin master inside the loop, consider the following script;

#!/bin/bash
 
javafile=*.java
filesCommit=0

# For each java file
for i in $javafile; do
    if [[ "$i" == "$javafile" ]]; then
        echo "No .java files"
    else
        # Add current file
        git add $i
        echo
        echo "File $i added"

        # Ask for commit message
        echo
        echo "Write a message to commit"
        read message

        # Commit single file with message
        git commit -m "$message"

        # Bumb counter to remember number of items
        ((filesCommit++))
    fi
done

# If we've had atleast 1 file, push
if [[ "$filesCommit" -ge 0 ]]; then
    git push origin master
    echo
    echo "#############################################"
    echo "Pushed successfully; $filesCommit files"
    echo "#############################################"
fi

Here I'm using a variable to count the number of files we've commited. Then, after the loop, we can push all those files

Sign up to request clarification or add additional context in comments.

2 Comments

That actually did it. Thank you for your contribution, a lot of people would've downvoted. Thanks :D
Glad it helped! Please consider upvoting or accepting any answers that might have helped you. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.