繁体   English   中英

Git 在一个命令中添加和提交

[英]Git add and commit in one command

有什么办法我可以做

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

在一个命令中?

我似乎经常执行这两个命令,如果 Git 有一个像git commit -Am "commit message"这样的选项,它会让生活变得更加方便。

git commit-a修饰符,但它与git add -A在提交之前的作用不完全相同。 git add -A添加新创建的文件,但git commit -am不添加。 做什么?

您可以使用 git 别名,例如

git config --global alias.add-commit '!git add -A && git commit'

并使用它

git add-commit -m 'My commit message'

编辑:恢复为刻度 ('),否则 Linux 上的 shell 扩展将失败。 在 Windows 上,应该使用双引号 (")代替(在评论中指出,未验证)。

git commit -am "message"

是告诉 git 删除您已删除的文件的简单方法,但我通常不推荐这种包罗万象的工作流程。 在最佳实践中,Git 提交应该是相当原子的,并且只影响几个文件。

git add .
git commit -m "message"

是添加所有新文件或修改文件的简单方法。 此外,包罗万象的资格也适用。 上述命令不会删除没有git rm命令删除的文件。

git add app
git commit -m "message"

是一种将所有文件从单个目录添加到索引的简单方法,在本例中为app目录。

要将其保持在一行中,请使用:

git add . && git commit -am "comment"

这一行将添加和提交所有更改和添加的文件到存储库。

最简单的方法是:

git commit -am "Your commit message"

我不明白为什么我们要搞得这么棘手。

仅调整Ales 的回答和Courtimaslinux bash评论:

要将其保持在一行中,请使用:

git commit -am "comment"

此行将添加并提交所有更改到存储库。

只要确保没有 git 尚未获取的新文件 否则你需要使用:

git add . ; git commit -am "message"

只需结合您的命令:

git add -A && git commit -m "comment" 

在更高版本的 git 中,您可以像这样添加和提交

git commit -a -m "commit message"

另外你一个别名:

[alias]
    ac = commit -a -m

然后你可以像这样使用它:

git ac "commit message"

在我的 Windows 机器上,我设置了这个.bashrc别名,使整个过程更加简单。

  • 创建/定位您的.bashrc - 参考 SO 线程
  • 将以下行添加到文件中

    alias gacp='echo "enter commit message : " && read MSG && git add . && git commit -m "$MSG" && git push'

    它确实 git add commit 和 push 。 以任何方式调整它,假设您不希望 push 命令删除该部分

  • 重新加载.bashrc / 关闭并重新打开你的 shell

  • 现在您可以使用gacp命令完成整个过程。

很确定你可以使用:

git commit -am "commit all the things"

只需使用:

git commit -m "message" .     

注意“.” 最后......也可以是文件/目录的路径

我希望这对某人有所帮助,请随时编辑或改进。 我不确定最快的方法是什么,但这肯定会通过对 Git使用“ohmyzsh”来简化我的代码提交过程。

https://ohmyz.sh/

  • git add . 缩写为ga .
  • git commit -m "message"缩短为gc -m "message"
  • git push缩写为gp
  • git fetch缩短为gf
  • git pull origin master缩写为ggl master
  • git push origin master缩写为ggp master
  • git checkout -b缩短为gcb
  • git merge缩写为gm
  • git remote缩短为gr
  • git status缩写为gst

在此处输入图片说明

我在我的.bash_profile.profile.zprofile或任何在登录 shell 中获取的东西中有这个函数:

function gac () {
  # Usage: gac [files] [message]
  # gac (git add commit) stages files specified by the first argument
  # and commits the changes with a message specified by the second argument.
  # Using quotes one can add multiple files at once: gac "file1 file2" "Message".
  git add $1 && git commit -m "$2"
}

如果您键入:

git config --global alias.a '!git add -A && git commit -m'

一次,您只需要输入

git a

每次:

git a 'your comment'

我做壳

#!/bin/sh

clear

git add -A 
git commit -a -m "'$*'"

保存例如 git.sh 并稍后调用:

sh git.sh your commit message

你可以使用-a

git commit -h

...
Commit contents options
    -a, -all    commit all changed files
...

git commit -a # It will add all files and also will open your default text editor.
  1. 在 bash 中创建别名: alias gac="git add -A && git commit -m"

    (我选择调用快捷方式“gac”,但您不必这样做)

  2. 使用它: gac 'your commit message here'

我使用以下内容(两者都在进行中,所以我会尽量记住更新它):

# Add All and Commit
  aac = !echo "Enter commit message:" && read MSG && echo "" && echo "Status before chagnes:" && echo "======================" && git status && echo "" && echo "Adding all..." && echo "=============" && git add . && echo "" && echo "Committing..." && echo "=============" && git commit -m \"$MSG\" && echo "" && echo "New status:" && echo "===========" && git status

# Add All and Commit with bumpted Version number
  aacv = !echo "Status before chagnes:" && echo "======================" && git status && echo "" && echo "Adding all..." && echo "=============" && git add . && echo "" && echo "Committing..." && echo "=============" && git commit -m \"Bumped to version $(head -n 1 VERSION)\" && echo "" && echo "New status:" && echo "===========" && git status

使用echo "Enter commit message:" && read MSG部分,灵感来自 Sojan V Jose

我很想在那里得到一个if else语句,这样我就可以得到 aacv 来问我是否要在完成后部署,如果我输入“y”就为我做这件事,但我想我应该把它放在我的.zshrc文件

我使用以下别名添加所有和提交

git config --global alias.ac '!git add -A && git commit -a'

然后,通过键入:

git ac

我有一个 vim 窗口,可以为我的提交消息获取更多编辑工具。

如果有人想为单个文件“添加和提交”,这是我的情况,我创建了下面的脚本来做到这一点:

#!/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

我将其命名为“gitfile.sh”并将其添加到我的 $PATH 中。 然后我可以在一个命令中运行 git add 并提交单个文件:

gitfile.sh /path/to/file "MY COMMIT MESSAGE"

对于人群中习惯了 Subversion 之类的东西的银背......在这个词的旧意义上做一个“提交”(即 - 或在 git 说话中 - 添加,提交和推送)在一个单步执行我通常会在我的项目的根目录中添加类似以下内容的内容(作为 Windows 中的 bat 文件,例如 git-commit.bat)。 然后,当我想添加、提交和推送时,我只需输入诸如git-commit "Added some new stuff" ,然后所有内容都将发送到远程仓库。

此外,通过这种方式,项目中的任何人都可以使用相同的内容,而无需在本地更改任何内容。

我通常也会运行一次git config credential.helper store ,所以在运行时我不需要提供 uid/pwd。

::
:: add, commit, and push to git
::

@echo off
echo.
echo.
echo 
echo Doing commit...
git add -A && git commit -m %1
echo.
echo.
echo Doing push...
git push
echo.
echo.
echo Done.
echo.

有什么办法可以做吗

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

在一个命令中?

我似乎经常执行这两个命令,并且如果Git具有git commit -Am "commit message"类的选项,它将使生活变得更加便捷。

git commit具有-a修饰符,但与提交之前执行git add -A git add -A添加新创建的文件,但是git commit -am不添加。 什么事啊

我使用这个 git 别名:

git config --global alias.cam '!git commit -a -m '

所以,而不是打电话

git add -A && git commit -m "this is a great commit"

我只是做:

git cam "this is a great commit"

您可以使用git commit -am "[comment]" // 最佳解决方案或 git add 。 && git commit -m "[评论]"

首先检查你有什么别名...

git config --get-regexp alias

如果它不存在,您可以创建自己的(参考: https : //git-scm.com/book/en/v2/Git-Basics-Git-Aliases

// git 添加

git config --global alias.a '!git add -A'

// git提交

git config --global alias.c '!git commit'

// git commit -m

git config --global alias.cm '!git commit -m'

// git 添加提交

git config --global alias.ac '!git add -A && git commit'

// git 添加提交 -m

git config --global alias.acm '!git add -A && git commit -m'

例如,如果您使用最后一个...

git acm 'My commit'

有人说git commit -am可以解决问题。 这将不起作用,因为它只能提交对跟踪文件的更改,但不会添加新文件。 来源

经过一些研究,我发现没有这样的命令可以做到这一点,但您可以根据您的操作系统在.bashrc.bash_profile上编写脚本。

我将分享我使用的一个:

function gac {
  if [[ $# -eq 0 ]]
    then git add . && git commit
  else
    git add . && git commit -m "$*"
  fi
}

有了这个,您的所有更改都将被添加和提交,您只需键入gac ,系统将提示您编写提交消息

或者你可以直接输入你的提交信息gac Hello world ,你的所有更改都会被添加并且你的提交信息将是Hello world ,注意没有使用""

这回答了标题中的问题......不是描述中的问题,但我认为一些最终来到这里的人可能会发现这很有用。

以下 bash 脚本在一个命令中添加和提交文件。 它不会添加所有文件,它只会添加您在命令行上指定的文件。 如果没有在命令行上指定文件,这可以很容易地修改为添加所有文件。 但是,这对我来说似乎有点危险,所以我没有这样做。

#!/bin/bash

if [[ $# -lt 2 ]]
then
    echo "Usage:  $(basename $0) FILENAME+ \"COMMIT_MESSAGE\""
    echo 
    echo 'Shorthand for "git add FILENAME_0 FILENAME_1 .. FILENAME_n && git commit -m "COMMIT MESSAGE".'
    echo 'You must specify at least one filename, and supply one single commit message.'
    echo 
else    
    git add ${*: 1: $#-1} && git commit -m "${*: -1}"
fi

将它保存在一个名为 gac 的文件中,然后像这样使用它

gac file_a file_b file_c "adding three files because.. reasons"

在此线程中借鉴@LuisEnMarroquin 的工作。

  1. 输入git add . && git commit -am git add . && git commit -am并输入
  2. 输入history并输入
  3. 记住上面命令的数字,假设数字是543
  4. 从现在开始,对于每个提交类型!543 "your comment here"即感叹号+数字和一个空格和你的评论。

有什么办法可以做吗

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

在一个命令中?

我似乎经常执行这两个命令,并且如果Git具有git commit -Am "commit message"类的选项,它将使生活变得更加便捷。

git commit具有-a修饰符,但与提交之前执行git add -A git add -A添加新创建的文件,但是git commit -am不添加。 什么事啊

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

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

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

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

这是上面Lenar Hoyt 的 gac 提案的增强版本:

function gac() {

  if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    # displays help with
    # gac | gac -h | gac --help
    echo "------"
    echo "Cannot commit without comments. Semantic reminder:"
    echo "chore:        c" 
    echo "docs:         d" 
    echo "feat:         f" 
    echo "refactor:     r" 
    echo "style:        s"
    echo "test:         t" 
    echo "fix:          x"
    echo "------"
    return 1
  fi  

  SHORTCUT=$1
  shift ;
  COMMENT=$@

  # Chore
  if [ "$SHORTCUT" = "c" ]; then
    SHORTCUT="chore:"

  # Write or edit existing documentation
  elif [ "$SHORTCUT" = "d" ]; then
    SHORTCUT="docs:"

  # Add new feature
  elif [ "$SHORTCUT" = "f" ]; then
    SHORTCUT="feat:"

  # Refator your code base
  elif [ "$SHORTCUT" = "r" ]; then
    SHORTCUT="refactor:"

  # Styling actions
  elif [ "$SHORTCUT" = "s" ]; then 
    SHORTCUT="style:"

  # Test your code
  elif [ "$SHORTCUT" = "t" ]; then 
    SHORTCUT="test:"

  # Working on a feature
  elif [ "$SHORTCUT" = "x" ]; then 
    SHORTCUT="fix:"
  fi
  
  # res with or without semantic
  git add -A && git commit -m "$SHORTCUT $COMMENT"
  return 1
}

它将使您可以访问以下命令:

gac
# print available semantics

gac c <your message>
# git add -A && git commit -m "chore: <your message>"

gac d <your message>
# git add -A && git commit -m "docs: <your message>"

gac f <your message>
# git add -A && git commit -m "feat: <your message>"

gac r <your message>
# git add -A && git commit -m "refactor: <your message>"

gac s <your message>
# git add -A && git commit -m "style: <your message>"

gac t <your message>
# git add -A && git commit -m "test: <your message>"

gac x <your message>
# git add -A && git commit -m "fix: <your message>"

gac <your message>
# git add -A && git commit -m "<your message>"

要将其保持在一行中,请使用:

gacm "your comment"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM