有什么方法可以同时使用这三个命令吗?
git add .
git commit -a -m "commit" (do not need commit message either)
git push
有时我只改变一个字母,CSS填充之类的。不过,我必须编写所有三个命令来推动更改。有许多项目,我只是一个推动者,所以这个命令将是了不起的!
有什么方法可以同时使用这三个命令吗?
git add .
git commit -a -m "commit" (do not need commit message either)
git push
有时我只改变一个字母,CSS填充之类的。不过,我必须编写所有三个命令来推动更改。有许多项目,我只是一个推动者,所以这个命令将是了不起的!
我认为你可能误解了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——但似乎您正试图将它作为SVN来使用。您可能会在本文末尾找到一些有用的参考资料。
虽然我同意韦恩·沃纳的怀疑,但从技术上讲,这是一种选择:
git config alias.acp '! git commit -a -m "commit" && git push'
它定义了一个别名,用于运行提交和推送。使用它作为git acp。请注意,这样的“shell”别名总是从git存储库的根目录运行。
另一种选择可能是编写一个执行推送的post-commit钩子。
哦,顺便说一下,您确实可以将参数传递给shell别名。如果你想传递一个自定义提交消息,可以使用:
git config alias.acp '! acp() { git commit -a -m "$1" && git push ; } ; acp'
(当然,现在,你需要给出一个提交消息:git acp“我的消息在这里!”)
如果文件已经被跟踪,那么你不需要运行git add,你可以简单地写git commit -am 'your message'
如果不想写提交消息,可以考虑这样做
Git commit——allow-empty-message -am "
在bash中设置为别名:
$ alias lazygit="git add .; git commit -a -m '...'; git push;";
叫它:
$ lazygit
(要使这个别名永久存在,你必须将它包含在你的.bashrc或.bash_profile中)
基于@Gavin的回答:
将lazygit作为一个函数而不是一个别名,这样就可以给它传递一个参数。我已经在我的.bashrc(或.bash_profile如果Mac)中添加了以下内容:
function lazygit() {
git add .
git commit -a -m "$1"
git push
}
这允许您提供一个提交消息,例如
lazygit "My commit msg"
当然,您还可以通过接受更多的参数来进一步增强这一点,比如要推到哪个远程位置,或者哪个分支。
正如回答中提到的,您可以创建一个git别名并为其分配一组命令。在这种情况下,它将是:
git config --global alias.add-com-push '!git add . && git commit -a -m "commit" && git push'
用在
git add-com-push
上面的脚本有一些问题:
Shift“删除”参数$1,否则,“push”将读取它并“误解它”。
我的建议:
Git配置——全局别名。acpp”!git add -A && branchatu="$(git symbol -ref HEAD 2>/dev/null)"& & branchatu = $ {branchatu # # refs /头/} && git commit -m "$1" && shift && git pull -u origin $branchatu && git Push -u origin $branchatu'
我在.bash_profile中使用它
gitpush() {
git add .
git commit -m "$*"
git push
}
alias gp=gitpush
它像这样执行
gp A really long commit message
不要忘记运行source ~/。保存别名后的Bash_profile。
我最终在我的.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'
我使用批处理文件:
@ECHO OFF
SET /p comment=Comment:
git add *
git commit -a -m "%comment%"
git push
当你想要类似svn的git提交行为时,在你的.gitconfig中的git别名中使用这个
Commit = "!f() {git Commit \"$@\" && git push;}; f”
你可以试试gitu。
第一次(必须安装node js):
npm install -g git-upload
后:
gitu COMMIT_MSG
同时发出这三个命令。
好处是,当您重新安装系统或当您想在不同的计算机上执行此操作时,您不必担心,并且不需要修改文件。 这也适用于不同的平台(不只是Linux和Mac,也可以在命令提示符下的Windows,如cmd和powershell),只是你必须安装npm和nodejs(当然是git)。
我为命令执行了这个.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
由于问题没有指定哪个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
在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 -u origin branch_name来更改git push片段
如果你想在系统编辑器中编辑你的提交信息,那么
git commit -a && git push
将打开编辑器,一旦你保存消息,它也会推送它。
加入~/。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
在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
}
如果你用的是fish shell(基于btse的答案):
将该文件保存在` ~/。Config /fish/functions'为'quickgit.fish'。如果目录不存在,请创建该目录。 ”——git-dir = $ PWD /。确保我们在调用函数的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”示例消息“”。 更改现在将被添加、提交和推送:)。
也可以在这里找到Gist: https://gist.github.com/Abushawish/3440a6087c212bd67ce1e93f8d283a69
这非常适合命令分组。
分组命令
{列表;} 将命令列表放在花括号之间会导致该列表在当前shell上下文中执行。没有创建子shell。列表后面必须有分号(或换行符)。
legit(){ git add --all; git commit -m "$1"; git push origin master; }
legit 'your commit message here'
最简单的解决方法是:
git commit -a -m "commit" && git push
Git add已经包含在commit的-a参数中,但如果你想要,你可以把它们都连接起来:
git add . && git commit -a -m "commit" && git push
已经有很多好的解决方案了,但下面这个解决方案更适合我想要的工作方式:
我在路径中放置了一个名为“git-put”的脚本,其中包含:
#!/bin/bash
git commit "$@" && git push -u
这允许我运行:
Git put -am“我的提交信息”
..要添加所有文件,提交并推送它们。
(我还添加了“-u”,因为我喜欢这样做,即使它与这个问题无关。确保上游分支始终处于可拉状态。)
我喜欢这种方法,因为它还允许在不添加所有文件的情况下使用“git put”(跳过“-a”),或者使用我可能想要传递给提交的任何其他选项。另外,put是push和commit的合成词
编写一个名为gitpush.sh的小脚本,并将其添加到~目录中。
echo $1
git add .
git commit -m "$1"
git push
现在在~/中添加一个别名。Bashrc如下:
alias gitpush='~/gitpush'
现在从任何git存储库只写gitpush "message"。
这个结果 -试试这个:简单的脚本一个命令git添加,git提交和git推送
在Windows上打开CMD并粘贴此答案
Git提交-m“你的消息”。git push origin master
这个例子我的图片截图: https://i.stack.imgur.com/2IZDe.jpg
在.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'
是否会添加某些文件更改,并使用自定义提交消息
对于MAC VSC用户来说,最好的设置是:
1)按'shift+cmd+P'并输入:
Shell Command: install 'code' command in PATH
按ENTER(这将安装代码命令,以便轻松获得bash_profile)
2)你现在可以运行:code ~/。打开空的Bash_profile
3)在那里输入一个新函数:
function lazygit() {
git add .
git commit -m "$*"
git push
}
4)现在重启VSC
5)进行更改,保存并输入lazygit message同时运行这三个命令
如果你用的是Mac电脑:
打开终端,输入cd ~/进入主文件夹 输入touch .bash_profile创建新文件。 使用您最喜欢的编辑器编辑.bash_profile(或者您可以直接键入 open -e .bash_profile在TextEdit中打开它)。 复制并粘贴下面的文件:
函数lazygit() { Git添加。 Git commit -m "$1" git推 }
在这之后,重启你的终端,简单地添加,提交和推送一个简单的命令,例如:
lazygit "This is my commit message"
macOS用户:
打开您的终端或iTerm2或您使用的其他终端。 使用~/命令移动到用户配置文件文件夹。它是.bash_profile文件的默认文件夹:
输入nano. bash_profile这个命令将在最容易使用的终端nano文本编辑器中打开.bash_profile文档(如果它还不存在,也可以创建它)。 现在您可以对文件进行简单的更改。粘贴以下代码行来更改终端提示符:
function lazygit() {
git add .
git commit -a -m "$1"
git push
}
现在通过输入ctrl + o保存您的更改,并点击返回保存。然后输入ctrl + x退出nano。 现在我们需要激活您的更改。输入source .bash_profile(或。~/.bash_profile)并注意提示符的变化。 在iTerm2的Preferences/Profiles/General/Command中设置为Login Shell并在start时发送文本到source ~/.bash_profile。因此,您不需要在每次macOS重新启动后手动进行设置。
凭证:https://natelandau.com/my-mac-osx-bash_profile
请看我的回答,我把所有的东西都加到了一行中
alias gitcomm="echo 'Please enter commit message';read MSG ;git add --all;git commit -am=$MSG;git push"
我创建了一个专用的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的个人访问令牌来进行身份验证,如果你还没有一个,你可以找到如何使用这个官方文档。
我希望它能解决你的问题。
你好,你只需要添加unalias [nano或vi] ~/.bashrc
echo Enter You commit message && read msg && git add . && git commit -m "$msg" && git push origin main