git add, commit and push commands in one?

前端 未结 30 1308
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 03:15

Is there any way to use these three commands in one?

git add .
git commit -a -m \"commit\" (do not need commit message either)
git push

Som

相关标签:
30条回答
  • 2020-12-02 03:55

    For MAC VSC users the best setup is:

    1) press 'shift+cmd+P' and type:

    Shell Command: install 'code' command in PATH
    

    Press ENTER (this will install code command to get to the bash_profile easily)

    2 ) you can now run: code ~/.bash_profile to open the empty bash_profile

    3) enter a new function in there:

    function lazygit() {
        git add .
        git commit -m "$*"
        git push
    }
    

    4) now restart VSC

    5) make a change, save it and type lazygit message to run the three commands concurrently

    0 讨论(0)
  • 2020-12-02 03:56

    There are some issues with the scripts above:

    shift "removes" the parameter $1, otherwise, "push" will read it and "misunderstand it".

    My tip :

    git config --global alias.acpp '!git add -A && branchatu="$(git symbolic-ref HEAD 2>/dev/null)" && branchatu=${branchatu##refs/heads/} && git commit -m "$1" && shift && git pull -u origin $branchatu && git push -u origin $branchatu'

    0 讨论(0)
  • 2020-12-02 03:57

    Simpliest solution would be to:

    git commit -a -m "commit" && git push
    

    git add is already contained in -a parameter of commit, but if you want you can connect them all:

    git add . && git commit -a -m "commit" && git push
    
    0 讨论(0)
  • 2020-12-02 03:58

    While I agree with Wayne Werner on his doubts, this is technically an option:

    git config alias.acp '! git commit -a -m "commit" && git push'
    

    Which defines an alias that runs commit and push. Use it as git acp. Please be aware that such "shell" aliases are always run from the root of your git repository.

    Another option might be to write a post-commit hook that does the push.

    Oh, by the way, you indeed can pass arguments to shell aliases. If you want to pass a custom commit message, instead use:

    git config alias.acp '! acp() { git commit -a -m "$1" && git push ; } ; acp'
    

    (Of course, now, you will need to give a commit message: git acp "My message goes here!")

    0 讨论(0)
  • 2020-12-02 03:59

    Building off the lazygit answer, the following solution adds a user check to verify the changes before pushing. It will revert the commands if cancelled. And all that will happen if and only if there are changes in the local repo.

    ### SAFER LAZY GIT
    function lazygit() {
      git add .
      if git commit -a -m "$1"; then
        read -r -p "Are you sure you want to push these changes? [y/N]} " response
        case "$response" in
          [yY][eE][sS]|[yY])
            git push
            ;;
          *)
            git reset HEAD~1 --soft
            echo "Reverted changes."
            ;;
        esac
      fi
    }
    
    0 讨论(0)
  • 2020-12-02 04:00

    I ended up adding an alias to my .gitconfig file:

    [alias]
        cmp = "!f() { git add -A && git commit -m \"$@\" && git push; }; f"
    

    Usage: git cmp "Long commit message goes here"

    Adds all files, then uses the comment for the commit message and pushes it up to origin.

    I think it's a better solution because you have control over what the commit message is.

    The alias can be also defined from command line, this adds it to your .gitconfig:

    git config --global alias.cmp '!f() { git add -A && git commit -m "$@" && git push; }; f'
    
    0 讨论(0)
提交回复
热议问题