git add, commit and push commands in one?
GitGit Problem Overview
Is there any way to use these three commands in one?
git add .
git commit -a -m "commit" (do not need commit message either)
git push
Sometimes I'm changing only one letter, CSS padding or something. Still, I have to write all three commands to push the changes. There are many projects where I'm only one pusher, so this command would be awesome!
Git Solutions
Solution 1 - Git
Building off of @Gavin's answer:
Making lazygit a function instead of an alias allows you to pass it an argument. I have added the following to my .bashrc (or .bash_profile if Mac):
function lazygit() {
git add .
git commit -a -m "$1"
git push
}
This allows you to provide a commit message, such as
lazygit "My commit msg"
You could of course beef this up even more by accepting even more arguments, such as which remote place to push to, or which branch.
Solution 2 - Git
I ended up adding an alias to my .gitconfig
file:
[alias]
cmp = "!f() { git add -A && git commit -m \"$@\" && git push; }; f"
Usage: git cmp "Long commit message goes here"
Adds all files, then uses the comment for the commit message and pushes it up to origin.
I think it's a better solution because you have control over what the commit message is.
The alias can be also defined from command line, this adds it to your .gitconfig
:
git config --global alias.cmp '!f() { git add -A && git commit -m "$@" && git push; }; f'
Solution 3 - Git
While I agree with Wayne Werner on his doubts, this is technically an option:
git config alias.acp '! git commit -a -m "commit" && git push'
Which defines an alias that runs commit
and push
. Use it as git acp
. Please be aware that such "shell" aliases are always run from the root of your git repository.
Another option might be to write a post-commit hook that does the push.
Oh, by the way, you indeed can pass arguments to shell aliases. If you want to pass a custom commit message, instead use:
git config alias.acp '! acp() { git commit -a -m "$1" && git push ; } ; acp'
(Of course, now, you will need to give a commit message: git acp "My message goes here!"
)
Solution 4 - Git
I use this in my .bash_profile
gitpush() {
git add .
git commit -m "$*"
git push
}
alias gp=gitpush
It executes like
gp A really long commit message
Don't forget to run source ~/.bash_profile
after saving the alias.
Solution 5 - Git
I think you might misunderstand the workflow that git was designed for. (To clarify/correct what I meant in the comment, you don't need the git add .
, since commit -a
usually serves the same purpose - adding any changes that have not yet been staged, if the files have already been added)
Typically you'll do something like this:
# make some changes
$ git commit -a -m "Changed something"
# make some more changes
$ git commit -a -m "Changed something else"
wash, rinse, repeat, until you've finished feature X, or you're at a stopping point, or you just want other people to see what you've done. Then you do
$ git push
Git is not SVN - but it appears that you're trying to use it as such. You might find some of the resources at the end of the article here to be of some use.
Solution 6 - Git
You can use bash script , set alias to launch any command or group of commands
git commit -am "your message" && git push
Solution 7 - Git
Set as an alias in bash:
$ alias lazygit="git add .; git commit -a -m '...'; git push;";
Call it:
$ lazygit
(To make this alias permanent, you'd have to include it in your .bashrc or .bash_profile)
Solution 8 - Git
Simpliest solution would be to:
git commit -a -m "commit" && git push
git add
is already contained in -a parameter of commit, but if you want you can connect them all:
git add . && git commit -a -m "commit" && git push
Solution 9 - Git
In Linux/Mac, this much practical option should also work
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
If you are working on a new branch created locally, change the git push
piece with git push -u origin branch_name
If you want to edit your commit message in system editor then
git commit -a && git push
will open the editor and once you save the message it will also push it.
Solution 10 - Git
You can try gitu.
For the first time (node js has to be installed):
npm install -g git-upload
After that:
gitu COMMIT_MSG
To issue those three commands at once.
The good thing is that you don't have to worry when you reinstall your system or when you want to do this on different computers and No file modification is needed.
This also work on different platforms (not just Linux and Mac, but also Windows under command prompt like cmd
and powershell
) just that you have to install npm
and nodejs
(git
of course).
Solution 11 - Git
If the file is already being tracked then you do not need to run git add
, you can simply write git commit -am 'your message'
If you do not want to write a commit message you might consider doing something like
git commit --allow-empty-message -am ''
Solution 12 - Git
If you're using a Mac:
-
Start up Terminal and input
cd ~/
to go to your home folder -
Type
touch .bash_profile
to create your new file. -
Edit
.bash_profile
with your favourite editor (or you can just typeopen -e .bash_profile
to open it in TextEdit). -
Copy & Paste the below into the file:
> function lazygit() { > git add . > git commit -a -m "$1" > git push > }
After this, restart your terminal and simply add, commit and push in one easy command, example:
lazygit "This is my commit message"
Solution 13 - Git
- Try this: Simple script one command for git add, git commit and git push
Open your CMD on Windows and paste this answer
git commit -m "your message" . && git push origin master
> This example my picture screenshot : https://i.stack.imgur.com/2IZDe.jpg
Solution 14 - Git
As mentioned in this answer, you can create a git alias and assign a set of commands for it. In this case, it would be:
git config --global alias.add-com-push '!git add . && git commit -a -m "commit" && git push'
and use it with
git add-com-push
Solution 15 - Git
I use a batch file:
@ECHO OFF
SET /p comment=Comment:
git add *
git commit -a -m "%comment%"
git push
Solution 16 - Git
Write a small script named gitpush.sh with below lines and add it your ~ directory.
echo $1
git add .
git commit -m "$1"
git push
Now add an alias in ~/.bashrc like below :
alias gitpush='~/gitpush'
Now from any git repository just write gitpush "message" .
Solution 17 - Git
I like to run the following:
git commit -am "message";git push
Solution 18 - Git
For the macOS users:
-
Open your Terminal or iTerm2 or another terminal that you use.
-
Move to your User profile folder with command
~/
. It's a default folder for.bash_profile
file:
-
Type
nano .bash_profile
This command will open the.bash_profile
document (or create it if it doesn’t already exist) in the easiest to use text editor for terminal –nano
. -
Now you can make a simple change to the file. Paste these lines of code to change your Terminal prompt:
function lazygit() {
git add .
git commit -a -m "$1"
git push
}
-
Now save your changes by typing
ctrl + o
and hit return to save. Then exitnano
by typingctrl + x
. -
Now we need to activate your changes. Type
source .bash_profile
(or. ~/.bash_profile
) and watch your prompt change. -
In iTerm2
Preferences/Profiles/General/Command
set toLogin Shell
andSend text at start
tosource ~/.bash_profile
. So you don't need to make it manually after each macOS restart.
Credentials: https://natelandau.com/my-mac-osx-bash_profile
Solution 19 - Git
For MAC VSC users the best setup is:
-
press 'shift+cmd+P' and type:
Shell Command: install 'code' command in PATH Press ENTER (this will install code command to get to the bash_profile easily)
2 ) you can now run: code ~/.bash_profile
to open the empty bash_profile
-
enter a new function in there:
function lazygit() { git add . git commit -m "$*" git push }
-
now restart VSC
-
make a change, save it and type
lazygit message
to run the three commands concurrently
Solution 20 - Git
There are some issues with the scripts above:
shift "removes" the parameter $1, otherwise, "push" will read it and "misunderstand it".
My tip :
> 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'
Solution 21 - Git
When you want svn-like behavior of git commit, use this in your git aliases in your .gitconfig
commit = "!f() { git commit \"$@\" && git push; };f"
Solution 22 - Git
Building off the lazygit
answer, the following solution adds a user check to verify the changes before pushing. It will revert the commands if cancelled. And all that will happen if and only if there are changes in the local repo.
### 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
}
Solution 23 - Git
If you're using fish shell (building off of btse's answer):
Save this file within '~/.config/fish/functions' as 'quickgit.fish'. Create the directory if it does not exist. '--git-dir=$PWD/.git' Ensures that we run the git commands against the git project where we called the function
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
Restart terminal, navigate to project, make changes, and now you can call 'quickgit "example message"'. Changes will now be added, committed, and push :).
Also can be found as a Gist here: https://gist.github.com/Abushawish/3440a6087c212bd67ce1e93f8d283a69
Solution 24 - Git
There are plenty of good solutions already, but here's a solution that I find more elegant for the way I want to work:
I put a script in my path called "git-put" that contains:
#!/bin/bash
git commit "$@" && git push -u
That allows me to run:
> git put -am"my commit message"
..to add all files, commit them, and push them.
(I also added the "-u" because I like to do this anyway, even though it's not related to this issue. It ensures that the upstream branch is always set up for pulling.)
I like this approach because it also allows to to use "git put" without adding all the files (skip the "-a"), or with any other options I might want to pass to commit. Also, "put" is a short portmanteau of "push" and "commit"
Solution 25 - Git
If you use VSCode, you can download this extension which will let you do it in one simple keyboard shortcut.
Solution 26 - Git
I did this .sh script for command
#!/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
Solution 27 - Git
Since the question doesn't specify which shell, here's the eshell version based on the earlier answers. This goes in the eshell alias file, which might be in ~/.emacs.d/eshell/alias I've added the first part z https://github.com/rupa/z/ which let's you quickly cd to a directory, so that this can be run no matter what your current directory is.
alias census z cens; git add .; git commit -m "fast"; git push
Solution 28 - Git
Add in ~/.bash_profile
for adding, committing and pushing with one command put:
function g() { git commit -a -m "$*"; git push; }
Usage:
g your commit message
g your commit message 'message'
No quotes are needed although you can't use semicolons or parenthesis in your commit messages (single quotes are allowed). If you want to any of these just simply put double quotes in you message, e.g.:
g "your commit message; (message)"
To create a comment in your message do:
g "your commit message:
> your note"
There's also a function for adding and committing in a similar way:
function c() { git add --all; git commit -m "$*"; }
Works exactly the same way that g
function and has the same constraints. Just put c
instead.
E.g.
c your commit message
You can also add an alias for pushing to the remote:
alias p='git push'
Usage:
p
That amounts into 2 letters, c
and p
you use while working with your git repository. Or you can use g
instead to do it all with only one letter.
Full list of aliases and functions: https://gist.github.com/matt360/0c5765d6f0579a5aa74641bc47ae50ac
Solution 29 - Git
This is perfect for command grouping.
> { list; } Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.
legit(){ git add --all; git commit -m "$1"; git push origin master; }
legit 'your commit message here'
Solution 30 - Git
I found this yolo
alias to be amazing to even submit a random comment to the commit while I am being lazy. It works really well out of the box, so I just do git yolo
and all my changes are pushed automatically.
Solution 31 - Git
define function in .bashrc
function gitall() {
file=${1:-.}
comment=${2:-update}
echo $file
echo $comment
git add $file && git commit -m '$comment' && git push origin master
}
in your terminal
gitall
default gitall will add all in current git repo
gitall some-file-to-add 'update file'
will add certain file to change, and use custom commit message
Solution 32 - Git
Please see my answer,I added everything into a single line
alias gitcomm="echo 'Please enter commit message';read MSG ;git add --all;git commit -am=$MSG;git push"
Solution 33 - Git
I created a dedicated bash script for automating these all, as it gives me a lot of trouble every time.
It looks like this
#!/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
You can simply use it by making a script.sh (or whatever you want to name in) BASH Script, it uses GitHub's Personal Acces Token to authenticate, if you don't have one already, you can find how to get on using this official Documentation.
I hope it solves your problem.