Git add and commit in one command

后端 未结 27 2266
旧巷少年郎
旧巷少年郎 2020-11-28 00:28

Is there any way I can do

git add -A
git commit -m \"commit message\"

in one command?

I seem to be doing those two commands a lot,

相关标签:
27条回答
  • 2020-11-28 00:55

    Just combine your commands:

    git add -A && git commit -m "comment" 
    
    0 讨论(0)
  • 2020-11-28 00:57

    The most simply you can do it is :

    git commit -am "Your commit message"
    

    I dont understand why are we making this tricky.

    0 讨论(0)
  • 2020-11-28 00:57

    In case someone would like to "add and commit" for a single file, which was my case, I created the script bellow to do just that:

    #!/bin/bash
    
    function usage {
        echo "Usage: $(basename $0) <filename> <commit_message>"    
    }
    
    function die {
        declare MSG="$@"
        echo -e "$0: Error: $MSG">&2
        exit 1
    }
    
    (( "$#" == 2 )) || die "Wrong arguments.\n\n$(usage)"
    
    FILE=$1
    COMMIT_MESSAGE=$2
    
    [ -f $FILE ] || die "File $FILE does not exist"
    
    echo -n adding $FILE to git...
    git add $FILE || die "git add $FILE has failed."
    echo done
    
    echo "commiting $file to git..."
    git commit -m "$COMMIT_MESSAGE" || die "git commit has failed."
    
    exit 0
    

    I named it "gitfile.sh" and added it to my $PATH. Then I can run git add and commit for a single file in one command:

    gitfile.sh /path/to/file "MY COMMIT MESSAGE"
    
    0 讨论(0)
提交回复
热议问题