16

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.

Vicky Dev
  • 1,519
  • 2
  • 21
  • 47
  • What do you want to achieve exactly? What is the problem with the line-by-line solution? – Lajos Veres May 06 '17 at 19:26
  • 1
    Would a shell script in `~/bin` work? Otherwise add a git alias in your user's `~/.gitconfig` file to execute a shell function (IIRC there's samples in the documentation), e.g. http://stackoverflow.com/a/14994923/112968 – knittl May 06 '17 at 19:27
  • For Linux shell script is good, but I have gitbash on Windows system and I am not as good with Win batch as with Linux shell. Can you give me a Windows based suggestion ? – Vicky Dev May 06 '17 at 19:29
  • With stock `git`, no, you can't. You can, however, create a shell script called `git-whatever` somewhere in the PATH, containing all your commands, using parameters if needed, and thus you could execute `git whatever` (note the lack of the space) – Lasse V. Karlsen May 06 '17 at 19:32

3 Answers3

31

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
Stephen Rauch
  • 44,696
  • 30
  • 102
  • 125
sohan kumawat
  • 332
  • 3
  • 4
5

If you are in a Windows shell:

git add . ; git commit -m "Testing one line command" ; git push
pmiranda
  • 6,162
  • 9
  • 51
  • 114
1

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
Community
  • 1
  • 1
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • The following does not work, can I do it? `cmd=('add .' "commit -m $commit"); for elem in ${cmd[@]} ; do; git "$elem" &&; # echo -n "$elem"; done;` it creates a new line for every word in the element array. echo -n can suppress newlines, but does not help because I do not want to echo but run git. – Timo Nov 27 '20 at 20:28
  • @Timo I would use double quotes consistently, as in https://stackoverflow.com/a/7774197/6309 – VonC Nov 27 '20 at 20:30