DEV Community

Cover image for Alias for git add, commit and push all together.
Bivor Faruque
Bivor Faruque

Posted on

Alias for git add, commit and push all together.

As we are developers we love to automate stuff. However, I feel that our use of git is quite a manual process and takes our time. In this article, I am going to discuss how I use alias to help me. Hope this is useful to you.


Alias for add, commit and push

So we all remember the tedious commands for pushing our code to a remote git branch whenever we make any changes to our code we have to use,

git add . //To add all your changes 

git commit -m "my commit" //commit the changes that are made with a message

git push //push the commit to your repo 
Enter fullscreen mode Exit fullscreen mode

It seems like small but it is a very tedious process once you do it over and over again. Thus, comes the following alias to do add, commit and push it all in one command

git config --global alias.cmp '!f() { git add -A && git commit -m "$@" && git push; }; f'
Enter fullscreen mode Exit fullscreen mode

After running this command in your cmd. You can just do the following to add, commit and push your changes.

git cmp "my commit"
Enter fullscreen mode Exit fullscreen mode

Alias for add and commit

However, sometimes we just need to do a local commit.

git add . //To add all your changes 

git commit -m "my commit" //commit the changes that are made with a message
Enter fullscreen mode Exit fullscreen mode

No worries, I am lazy too,

git config --global alias.ac "!git add -A && git commit -m "
Enter fullscreen mode Exit fullscreen mode

After running this command in your cmd. You can just do the following to add and commit your changes.

git ac "my commit"
Enter fullscreen mode Exit fullscreen mode

Thanks for reading, and happy coding!

Top comments (5)

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard
Collapse
 
sktoke profile image
Solaiman Khan

Great.
Currently i am using this with gitbash.

---Goto---
C:/ProgramFiles/Git/etc/bash.bashrc
open this file and add your sorthand
save
re-run
---Done---

====few alias, i use frequently====
alias test='./vendor/bin/phpunit --filter='
alias pest='./vendor/bin/pest --filter='
alias dump='composer dump-autoload'
alias serve='php artisan serve'
alias oclear='php artisan optimize:clear'
alias lclear='php artisan optimize:clear && php artisan config:cache && php artisan route:cache && php artisan view:cache'
alias fresh='php artisan migrate:fresh'
alias seed='php artisan db:seed'
alias watch='npm run watch'
alias dev='npm run dev'
alias prod='npm run prod'
alias status='git status'
alias log='git log'
alias branch='git branch -a'
alias gcm='git checkout master'
alias ga='git add .'
alias commit='git commit -m'
alias fetch='git fetch --all'
alias pushmaster='git push origin master'

Collapse
 
bivor profile image
Bivor Faruque

Hope mine helps your awesome arsenal ❀️

Collapse
 
suriyadisha profile image
Suriya Tasmim Disha

This is very helpful. Thanks!!

Collapse
 
apaksoy profile image
Alper Paksoy • Edited

I am using a Mac with Ventura 13.3.1 and I am using Bash as shell.

I tried writing

ac = '!f() { git add -A && git commit -m "$@" && git push; }; f'
Enter fullscreen mode Exit fullscreen mode

into .gitconfig manually using vim and it did not work.

Then, I tried, with vim again, slight variations of it such as using double quotes instead of single and then escaping them with \. None worked. Finally, I have decided to register the alias exactly as suggested in the article using the git config command and it worked.

Interestingly the version of the alias git config placed into the .gitconfig file was slightly different than the one in the command as well as all those I have tried:

ac = "!f() { git add -A && git commit -m \"$@\" && git push; }; f"
Enter fullscreen mode Exit fullscreen mode

Hope this helps someone else!