有什么方法可以同时使用这三个命令吗?

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

有时我只改变一个字母,CSS填充之类的。不过,我必须编写所有三个命令来推动更改。有许多项目,我只是一个推动者,所以这个命令将是了不起的!


当前回答

我创建了一个专用的bash脚本来自动化所有这些,因为它每次都给我带来很多麻烦。

它看起来是这样的

#!/bin/sh
# Shell Script to add all files, commit them with a commit message from user and then push them to remote GitHub repo
echo "*** Automating Git Add, Commit and Push ***"

#Ask for Username
echo "Enter your GitHub username: "
read username

#Ask for User Github's personal access token
echo "Enter your GitHub personal access token: "
read token

# Ask for repository name
echo "Enter repository name"
read repoName
# Check if repository exists at GitHub
curl "https://api.github.com/repos/${username}/${repoName}.git"
# If repository exits then

if [ $? -eq 0 ]; then
    cd $repoName
    # Display unstaged files
    git status
    git remote set-url origin https://${token}@github.com/${username}/${repoName}.git
    # If there are any uncommited and unstatged files, ask user to commit them
    if [ "$(git status --porcelain)" ]; then
        echo "There are uncommited and unstatged files. Commit them before pushing"
        echo "Enter commit message"
        read commitMessage
        git add .
        git commit -m "$commitMessage"
        git push
        echo "Files pushed to GitHub"
        # else push all commited and staged files to remote repo
    else
        git push
        echo "Files pushed to GitHub"
        
    fi
    #Echo message if there is no files to commit, stage or push
    if [ "$(git status --porcelain)" ]; then
        echo "There are no files to commit, stage or push"
    fi
else
    echo "Repository does not exist"
fi


# End of script

你可以简单地通过制作Script .sh(或任何你想要命名的)BASH Script来使用它,它使用GitHub的个人访问令牌来进行身份验证,如果你还没有一个,你可以找到如何使用这个官方文档。

我希望它能解决你的问题。

其他回答

由于问题没有指定哪个shell,下面是基于前面答案的shell版本。这将放入shell别名文件,该文件可能在~/.emacs中。d/eshell/alias我已经添加了第一部分z https://github.com/rupa/z/,它可以让你快速cd到一个目录,这样无论你的当前目录是什么都可以运行。

alias census z cens; git add .; git commit -m "fast"; git push

在bash中设置为别名:

$ alias lazygit="git add .; git commit -a -m '...'; git push;";

叫它:

$ lazygit

(要使这个别名永久存在,你必须将它包含在你的.bashrc或.bash_profile中)

在lazygit答案的基础上,下面的解决方案添加了一个用户检查,以在推送之前验证更改。如果取消,它将恢复命令。当且仅当本地回购发生变化时,所有这些都会发生。

### 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
}

加入~/。Bash_profile用于添加、提交和推送:

function g() { git commit -a -m "$*"; git push; }

用法:

g your commit message
g your commit message 'message'

不需要引号,尽管不能在提交消息中使用分号或括号(允许使用单引号)。如果你想使用以上任何一种方法,只需在你的邮件中加上双引号,例如:

g "your commit message; (message)"

要在你的消息中创建评论,请:

g "your commit message:
> your note"

还有一个函数用于以类似的方式添加和提交:

function c() { git add --all; git commit -m "$*"; }

和g函数的工作原理完全一样并且有相同的约束条件。用c代替。 如。

c your commit message

你也可以添加一个别名来推送到远程:

alias p='git push'

用法:

p

这相当于2个字母,c和p,您在使用git存储库时使用。或者你可以用g来代替只用一个字母。

别名和函数的完整列表: https://gist.github.com/matt360/0c5765d6f0579a5aa74641bc47ae50ac

正如回答中提到的,您可以创建一个git别名并为其分配一组命令。在这种情况下,它将是:

git config --global alias.add-com-push '!git add . && git commit -a -m "commit" && git push'

用在

git add-com-push