我能做点什么吗

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不添加。什么?


当前回答

我使用以下方法(这两种方法都在进行中,所以我会尽量记得更新):

# 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

呼应“Enter commit message:”&&读取MSG部分,灵感来自Sojan V Jose

我想在那里得到一个if else语句,这样我就可以让aacv问我,如果我想部署时,如果我键入'y',为我这样做,但我想我应该把它放在我的.zshrc文件

其他回答

以下是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>"

如果你输入:

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

一次,你只需要打字

git a

每一次:

git a 'your comment'

这回答了标题中的问题。不是描述里的问题,但我想在座的各位可能会觉得这个问题很有用。

下面的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的作品。

首先检查你有哪些别名……

git config --get-regexp alias

如果没有,你可以创建自己的(参考:https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)

去添加

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

// git提交

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

// git提交-m

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

// git添加提交

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

// git add commit -m

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

例如,如果你用最后一个…

git acm 'My commit'

在bash中创建别名: alias gac="git add -A && git commit -m" (我选择称这个快捷方式为“gac”,但你不必这么做) 使用它:gac '你的提交信息在这里'