Git add and commit in one command
Is there any way I can do
git add -A
git commit -m "commit message"
in one command?
I seem to be doing those two commands a lot, and if Git had an option like git commit -Am "commit message"
, it would make life that much more convenient.
git commit
has the -a
modifier, but it doesn't quite do the same as doing g开发者_Go百科it add -A
before committing. git add -A
adds newly created files, but git commit -am
does not. What does?
You can use git aliases, e.g.
git config --global alias.add-commit '!git add -A && git commit'
and use it with
git add-commit -m 'My commit message'
EDIT: Reverted back to ticks ('), as otherwise it will fail for shell expansion on Linux. On Windows, one should use double-quotes (") instead (pointed out in the comments, did not verify).
git commit -am "message"
is an easy way to tell git to delete files you have deleted, but I generally don't recommend such catch-all workflows. Git commits should in best practice be fairly atomic and only affect a few files.
git add .
git commit -m "message"
is an easy way to add all files new or modified. Also, the catch-all qualification applies. The above commands will not delete files deleted without the git rm
command.
git add app
git commit -m "message"
is an easy way to add all files to the index from a single dir, in this case the app
dir.
The most simply you can do it is :
git commit -am "Your commit message"
I dont understand why are we making this tricky.
To keep it in one line use:
git add . && git commit -am "comment"
This line will add and commit all changed and added files to repository.
Only adapting the Ales's answer and the courtsimas's comment for linux bash:
To keep it in one line use:
git commit -am "comment"
This line will add and commit all changed to repository.
Just make sure there aren't new files that git hasn't picked up yet. otherwise you'll need to use:
git add . ; git commit -am "message"
Just combine your commands:
git add -A && git commit -m "comment"
In the later version of git you can add and commit like this
git commit -a -m "commit message"
Additionally you an alias:
[alias]
ac = commit -a -m
Then you can use it like this:
git ac "commit message"
I hope this helps someone and please feel free to edit or improve. I'm not sure what the fastest way is but this certainly simplifies my code commit process by using "ohmyzsh" for Git.
https://ohmyz.sh/
git add .
is shortened toga .
git commit -m "message"
is shortened togc -m "message"
git push
is shortened togp
git fetch
is shortened togf
git pull origin master
is shortened toggl master
git push origin master
is shortened toggp master
git checkout -b
is shortened togcb
git merge
is shortened togm
git remote
is shortened togr
git status
is shortened togst
On my windows machine I have set up this .bashrc
alias to make the entire process more simple.
- create / locate your
.bashrc
- refer SO thread add the following line to file
alias gacp='echo "enter commit message : " && read MSG && git add . && git commit -m "$MSG" && git push'
it does git add commit and push . tweak it in any manner, say you don't want the push command remove that part
reload
.bashrc
/ close and reopen your shell- now you can do the entire process with
gacp
command .
pretty sure you can use:
git commit -am "commit all the things"
I have this function in my .bash_profile
or .profile
or .zprofile
or whatever gets sourced in login shells:
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"
}
Just use:
git commit -m "message" .
Notice the "." at the end... which can also be a path to a file/directory
If you type:
git config --global alias.a '!git add -A && git commit -m'
once, you will just need to type
git a
every time:
git a 'your comment'
I do a shell
#!/bin/sh
clear
git add -A
git commit -a -m "'$*'"
save for example git.sh and later call:
sh git.sh your commit message
Some are saying that git commit -am
will do the trick. This won't work because it can only commit changes on tracked files, but it doesn't add new files. Source.
After some research I figured that there is no such command to do that, but you can write a script on your ~/.bashrc
, ~/.bash_profile
or ~/.zshrc
depending on your OS.
I will share the one I use:
function gac {
if [[ $# -eq 0 ]]
then git add . && git commit
else
git add . && git commit -m "$*"
fi
}
With this all your changes will be added and committed, you can just type gac
and you will be prompted to write the commit message
Or you can type your commit message directly gac Hello world
, all your changes will be added and your commit message will be Hello world, note that ""
are not used
You ca use -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.
In case someone would like to "add and commit" for a single file, which was my case, I created the script bellow to do just that:
#!/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
I named it "gitfile.sh" and added it to my $PATH. Then I can run git add and commit for a single file in one command:
gitfile.sh /path/to/file "MY COMMIT MESSAGE"
Create an alias in bash:
alias gac="git add -A && git commit -m"
(I chose to call the shortcut 'gac' but you don't have to)
Use it:
gac 'your commit message here'
I use the following (both are work in progress, so I'll try to remember to update this):
# 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
With the echo "Enter commit message:" && read MSG
part inspired by Sojan V Jose
I'd love to get an if else
statement in there so I can get aacv to ask me if I want to deploy when it's done and do that for me if I type 'y', but I guess I should put that in my .zshrc
file
I use the following alias for add all and commit:
git config --global alias.ac '!git add -A && git commit -a'
Then, by typing:
git ac
I get a vim window to get more editing tools for my commit message.
Check first what aliases you have...
git config --get-regexp alias
If it's not there you can create your own (reference: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)
// git add
git config --global alias.a '!git add -A'
// git commit
git config --global alias.c '!git commit'
// git commit -m
git config --global alias.cm '!git commit -m'
// git add commit
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'
For instance, if you use the last one...
git acm 'My commit'
For the silver backs in the crowd that are used to things like Subversion... to do a "commit" in the old sense of the word (i.e. -- or in git speak -- to add, commit, and push) in a single step I generally add something like the following in the root of my project (as a bat file in windows e.g. git-commit.bat). Then when I want to add, commit, and push I just type something like git-commit "Added some new stuff"
and it all goes to the remote repo.
Also, this way anyone on the project can use the same with out having to change anything locally.
I usually also run git config credential.helper store
once so I don't need to give uid/pwd when this is run.
::
:: 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.
I use this git alias:
git config --global alias.cam '!git commit -a -m '
So, instead of call
git add -A && git commit -m "this is a great commit"
I just do:
git cam "this is a great commit"
This answers the question in the title.. not the question in the description, but I think some people who end up here might find this useful.
The following bash script adds and commits files in one command. IT DOES NOT ADD ALL FILES, it just adds files you specify on the command line. This could easily be modified to add all files if no files are specified on the command line. However, that seems a little dangerous to me so I haven't done it.
#!/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
Save it in a file called gac, say, and use it like this
gac file_a file_b file_c "adding three files because.. reasons"
Ripping on the work of @LuisEnMarroquin in this thread.
you can use
git commit -am "[comment]" # best solution
or
git add . && git commit -m "[comment]"
精彩评论