262

有没有办法将这三个命令合二为一?

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

有时我只更改一个字母、CSS 填充或其他东西。不过,我必须编写所有三个命令来推动更改。有很多项目我只是一个推动者,所以这个命令会很棒!

4

34 回答 34

293

基于@Gavin的回答:

让lazygit 成为一个函数而不是一个别名,可以让你向它传递一个参数。我已将以下内容添加到我的 .bashrc(如果是 Mac,则为 .bash_profile):

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

这允许您提供提交消息,例如

lazygit "My commit msg"

当然,您可以通过接受更多参数来加强这一点,例如要推送到哪个远程位置或哪个分支。

于 2014-04-27T21:06:23.003 回答
113

我最终为我的.gitconfig文件添加了一个别名:

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

用法:git cmp "Long commit message goes here"

添加所有文件,然后使用提交消息的注释并将其推送到源。

我认为这是一个更好的解决方案,因为您可以控制提交消息是什么。

别名也可以从命令行定义,这会将其添加到您的.gitconfig

git config --global alias.cmp '!f() { git add -A && git commit -m "$@" && git push; }; f'
于 2016-01-27T22:48:29.937 回答
74

虽然我同意 Wayne Werner 的怀疑,但从技术上讲,这是一种选择:

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

它定义了一个运行commitpush. 将其用作git acp. 请注意,此类“shell”别名始终从您的 git 存储库的根目录运行。

另一种选择可能是编写一个执行推送的提交后挂钩。

哦,顺便说一句,您确实可以将参数传递给 shell 别名。如果要传递自定义提交消息,请改用:

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

(当然,现在,你需要给出一个提交信息git acp "My message goes here!":)

于 2013-10-25T17:06:37.140 回答
44

我在我的 .bash_profile 中使用它

gitpush() {
    git add .
    git commit -m "$*"
    git push
}
alias gp=gitpush

它执行就像

gp A really long commit message

source ~/.bash_profile保存别名后不要忘记运行。

于 2016-01-02T19:39:35.327 回答
39

我认为您可能会误解 git 的工作流程。(为了澄清/纠正我在评论中的意思,您不需要git add ., 因为commit -a通常用于相同的目的 - 如果文件已添加,则添加尚未暂存的任何更改)

通常你会做这样的事情:

# make some changes
$ git commit -a -m "Changed something"
# make some more changes
$ git commit -a -m "Changed something else"

洗,冲洗,重复,直到你完成了功能 X,或者你在一个停止点,或者你只是想让其他人看到你做了什么。然后你做

$ git push

Git 不是 SVN - 但您似乎正在尝试使用它。您可能会在本文末尾找到一些有用的资源。

于 2013-10-25T16:44:26.180 回答
32

您可以使用 bash 脚本,设置别名来启动任何命令或命令组

git commit -am "your message" && git push 
于 2014-12-24T04:18:19.017 回答
20

在 bash 中设置为别名:

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

叫它:

$ lazygit

(要使此别名永久化,您必须将其包含在您的 .bashrc 或 .bash_profile 中)

于 2014-02-23T22:10:20.697 回答
19

最简单的解决方案是:

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

git add已经包含在提交的 -a 参数中,但如果您愿意,可以将它们全部连接起来:

git add . && git commit -a -m "commit" && git push
于 2018-08-09T11:40:08.320 回答
10

在 Linux/Mac 中,这个非常实用的选项也应该有效

git commit -am "IssueNumberIAmWorkingOn --hit Enter key
> A detail here --Enter
> Another detail here --Enter
> Third line here" && git push --last Enter and it will be there

如果您正在处理本地创建的分支,请git push使用git push -u origin branch_name

如果你想在系统编辑器中编辑你的提交信息,那么

git commit -a && git push 

将打开编辑器,一旦您保存消息,它也会推送它。

于 2017-12-18T21:35:09.870 回答
8

你可以试试gitu

第一次(必须安装node js):

npm install -g git-upload

在那之后:

gitu COMMIT_MSG

一次发出这三个命令。

好消息是,当您重新安装系统或想要在不同的计算机上执行此操作时,您不必担心,并且不需要修改文件。 这也适用于不同的平台(不仅是 Linux 和 Mac,还适用于命令提示符下的 Windows,例如cmdand powershell),只是您必须安装npmand nodejsgit当然)。

于 2016-12-28T16:39:35.083 回答
7

如果文件已经被跟踪,那么你不需要运行git add,你可以简单地写git commit -am 'your message'

如果你不想写提交信息,你可以考虑做类似的事情

git commit --allow-empty-message -am ''

于 2013-10-26T23:48:41.917 回答
4

这个结果 -试试这个:用于 git add、git commit 和 git push 的简单脚本一个命令

在 Windows 上打开您的 CMD 并粘贴此答案

git commit -m "your message" . && git push origin master

这个例子我的图片截图: https ://i.stack.imgur.com/2IZDe.jpg

于 2019-07-17T02:37:16.283 回答
3

this answer中所述,您可以创建一个git 别名并为其分配一组命令。在这种情况下,它将是:

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

并将其与

git add-com-push
于 2015-06-23T14:20:42.597 回答
3

用下面的几行编写一个名为 gitpush.sh 的小脚本并将其添加到您的 ~ 目录中。

echo $1
git add .
git commit -m "$1"
git push

现在在 ~/.bashrc 中添加一个别名,如下所示:

alias gitpush='~/gitpush'

现在从任何 git 存储库中只写 gitpush "message" 。

于 2019-04-04T07:51:50.117 回答
3

如果您使用的是 Mac:

  1. 启动终端并输入cd ~/以转到您的主文件夹

  2. 键入touch .bash_profile以创建新文件。

  3. 使用您喜欢的编辑.bash_profile器进行编辑(或者您可以 open -e .bash_profile在 TextEdit 中键入以将其打开)。

  4. 将以下内容复制并粘贴到文件中:

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

在此之后,重新启动您的终端并简单地添加、提交和推送一个简单的命令,例如:

lazygit "This is my commit message"
于 2020-02-17T15:55:09.563 回答
3

对于 macOS 用户:

  1. 打开您的终端iTerm2或您使用的其他终端。

  2. 使用命令移动到您的用户配置文件文件夹~/。这是文件的默认文件夹.bash_profile

用户文件夹示例

  1. 键入nano .bash_profile此命令将.bash_profile在最容易使用的终端文本编辑器中打开文档(或创建它,如果它不存在) - nano

  2. 现在您可以对文件进行简单的更改。粘贴这些代码行以更改终端提示:

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

纳米文本编辑

  1. 现在通过键入保存您的更改,ctrl + o然后按回车键保存。nano然后键入退出ctrl + x

  2. 现在我们需要激活您的更改。输入source .bash_profile(或. ~/.bash_profile)并观察您的提示变化。

  3. 在 iTerm2 中Preferences/Profiles/General/Command设置为Login Shell和。因此,您无需在每次 macOS 重新启动后手动进行。 Send text at startsource ~/.bash_profileiTerm 2 偏好

凭证:https ://natelandau.com/my-mac-osx-bash_profile

于 2020-03-25T07:37:22.263 回答
3

我使用批处理文件:

@ECHO OFF
SET /p comment=Comment:
git add *
git commit -a -m "%comment%"
git push
于 2016-03-31T15:16:10.190 回答
2

对于 MAC VSC 用户,最佳设置是:

1) 按“shift+cmd+P”并输入:

Shell Command: install 'code' command in PATH

按 ENTER(这将安装代码命令以轻松访问 bash_profile)

2 ) 你现在可以运行:code ~/.bash_profile打开空的 bash_profile

3)在那里输入一个新功能:

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

4) 现在重新启动 VSC

5)进行更改,保存并键入lazygit message以同时运行三个命令

于 2020-02-11T17:33:21.097 回答
1

上面的脚本有一些问题:

shift “删除”参数 $1,否则,“push”将读取它并“误解它”。

我的提示:

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'

于 2015-12-06T14:07:04.460 回答
1

如果您使用的是鱼壳(基于 btse 的回答):

将此文件保存在 '~/.config/fish/functions' 中作为 'quickgit.fish'。如果目录不存在,则创建该目录。'--git-dir=$PWD/.git' 确保我们针对调用函数的 git 项目运行 git 命令

function quickgit # This is the function name and command we call
    git --git-dir=$PWD/.git add . # Stage all unstaged files
    git --git-dir=$PWD/.git commit -a -m $argv # Commit files with the given argument as the commit message
    git --git-dir=$PWD/.git push # Push to remote
end

重新启动终端,导航到项目,进行更改,现在您可以调用'quickgit“示例消息”'。现在将添加、提交和推送更改:)。

也可以在这里找到要点:https ://gist.github.com/Abushawish/3440a6087c212bd67ce1e93f8d283a69

于 2018-05-24T00:37:28.657 回答
1

如果你使用 VSCode,你可以下载这个扩展,它可以让你通过一个简单的键盘快捷键来完成。

于 2019-10-11T20:15:06.760 回答
1

当您想要 git commit 的类似 svn 的行为时,请在 .gitconfig 的 git 别名中使用它

commit = "!f() { git commit \"$@\" && git push; };f"

于 2016-08-16T11:30:36.003 回答
1

已经有很多好的解决方案,但这里有一个解决方案,我发现它更适合我想要的工作方式:

我在我的路径中放置了一个名为“git-put”的脚本,其中包含:

#!/bin/bash
git commit "$@" && git push -u

这让我可以运行:

git put -am“我的提交信息”

..添加所有文件,提交它们,然后推送它们。

(我还添加了“-u”,因为无论如何我都喜欢这样做,即使它与此问题无关。它确保始终设置上游分支以进行拉取。)

我喜欢这种方法,因为它还允许使用“git put”而不添加所有文件(跳过“-a”),或者我可能想要传递给提交的任何其他选项。此外,“put”是“push”和“commit”的简短组合

于 2019-01-24T16:52:23.230 回答
1

我喜欢运行以下命令:

git commit -am "message";git push
于 2019-06-06T06:04:26.633 回答
0

在 .bashrc 中定义函数

function gitall() {
    file=${1:-.}
    comment=${2:-update}
    echo $file
    echo $comment
    git add $file && git commit -m '$comment' && git push origin master
}

在你的终端

gitall

默认 gitall 将在当前 git repo 中添加所有内容

gitall some-file-to-add 'update file'

将添加某些文件进行更改,并使用自定义提交消息

于 2020-01-07T09:24:09.610 回答
0

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

alias census z cens; git add .; git commit -m "fast"; git push
于 2017-08-03T09:47:26.380 回答
0

我发现这个 yolo别名甚至在我懒惰的时候向提交提交随机评论都很棒。它开箱即用,效果非常好,所以我只是这样做git yolo,我的所有更改都会自动推送。

于 2019-07-17T05:20:32.957 回答
0

使用一个命令添加~/.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 个字母,cp可以在使用 git 存储库时使用。或者,您可以g改为使用一个字母来完成所有操作。

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

于 2018-02-05T16:07:12.287 回答
0

根据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
}
于 2018-02-10T23:46:58.197 回答
0

这非常适合命令分组。

分组命令

{ 列表; 在大括号之间放置一个命令列表会导致该列表在当前 shell 上下文中执行。没有创建子shell。列表后面的分号(或换行符)是必需的。

legit(){ git add --all; git commit -m "$1"; git push origin master; }
legit 'your commit message here'
于 2018-07-19T21:32:04.590 回答
0

请看我的回答,我将所有内容添加到一行中

alias gitcomm="echo 'Please enter commit message';read MSG ;git add --all;git commit -am=$MSG;git push"
于 2020-09-15T12:48:47.470 回答
0

我创建了一个专门的 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 脚本来简单地使用它,它使用 GitHub 的 Personal Access Token 进行身份验证,如果您还没有,您可以找到如何继续使用官方文档。

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

于 2022-02-21T06:38:59.440 回答
0

我为命令做了这个 .sh 脚本

#!/bin/sh
cd LOCALDIRECTORYNAME/  
git config --global user.email "YOURMAILADDRESS"
git config --global user.name "YOURUSERNAME"
git init
git status
git add -A && git commit -m "MASSAGEFORCOMMITS"
git push origin master
于 2017-03-31T12:32:30.153 回答
-3

1)首先初始化:git init

2)其次在暂存区添加文件: git add 。

3)做 git commit : git commit -m "message"

4)最后推送:git push -all

于 2021-11-19T05:25:19.163 回答