Multiple git commands in single command executed in order they are encountered by compiler

25k Views Asked by At

I have following list of commands that I run in respective order so that a source project can be committed and pushed to the repository on Bitbucket:

git init
git remote add origin https://[BitBucket Username]@bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git
git config user.name "[BitBucket Username]"
git config user.email "[BitBucket Email ID]"
## if email doesn't work then use below ##
git config --global user.email \<\>
git add *
git commit -m "[Comment]"
git push -u origin master

Now instead of putting each and every line at their respective time and order, I want to know, if there is a possibility that I can chain all these into single git command and maintain the same order, something like below ?

git init remote add origin https://[BitBucket Username]@bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git  config user.name "[Username]" ....

Or atleast combine multiple same category params like below ?

git config user.name "[BitBucket Username]" user.email "[BitBucket Email ID]"

I need to know possibility of both scenarios with examples.

4

There are 4 best solutions below

2
sohan kumawat On BEST ANSWER

We can use list off command in single command for example:

git add . && git commit -m "updated pom file" && git push

or:

git stash pop && git add . && git commit -m "updated pom file" && git push
  • && -> if 1st command successfully executes then execute next command else it won't execute next command.
  • & - it executes all the command
  • || - execute next command if 1st one failed
2
VonC On

I have gitbash on Windows system and I am not as good with Win batch as with Linux shell.

You still can write a bash script (interpreted by the msys2 bash embedded with Git for Windows).
As mentioned in the comments by Lasse V. Karlsen, and as I mentioned before in "Which shell used for git '!' aliases?", you can write a shell script in a file (in your %PATH%) named git-xxx, and call it with git xxx.
That script would begin with:

#!/bin/bash
0
pmiranda On

If you are in a Windows Powershell:

git add . ; git commit -m "Testing one line command" ; git push
0
Johnnybossboy On

I created a file called reset.txt and in that file I have the commands

git reset --hard git clean -d -f [this is a newline - very important to have it]

I just copy and paste this into my terminal and it executes the commands in order.