How to call multiple git commands using one .gitconfig alias - bash

Here are my current aliases in .gitconfig, and they work great. However, I'd like to have a single alias that can do all three.
Example of what I'd like at terminal:
git x my_commit_message
Psuedo-code of .gitconfig:
[alias]
x = add -A <then do> commit -m <use variable from command line> push
I have push set to default = current, so push alone works.
[push]
default = current
Any help is appreciated, thanks!

If you want to combine add, commit and push, you'll need a bash function. Git add and commit can be combined with git -am "msg", but the push can only be done as an additional command. So, just define a bash function like this:
gacp() {
git add -A &&
git commit -m "${1?'Missing commit message'}" &&
git push
}
This works by doing the git add -A first, and if it succeeds, then the command git commit -m is executed, with the required message, and if that succeeds, then the git push is executed.
It's important to make the latter commands depend on successful execution of the previous commands in order to avoid downstream messes. In other words, you don't really want to commit changes unless the add succeeded, and you don't really want to push your most recent commits unless the commit succeeded.
You use it like this:
gacp "Latest changes"

You need to use shell function to be able to execute multiple commands inside a git alias.
First, start with ! so Git will run it as a shell command.
Write your function, like: f() { git add -A && git commit -m "$1" && git push }.
Execute your function just after its declaration.
You should write something like:
[alias]
x = "!f() { git add -A && git commit -m \"$1\" && git push } f"
Note that:
$1 will be replaced by your variable from command line,
&& will execute the next command only if the previous one has succeed.

Related

Git short command for checking out, pulling, prune fetching and deleting

Is there a shortcut for:
Checking out on master
Pulling from master
Prune Fetch (check which remote branches are removed)
Delete those local branches
Scenario:
Let's say I'm on master and I checkout on the branch foo, I do some commits and publish foo to remote and push to it as well. Next I merge that branch to master on GitHub and delete the online version of foo since it's complete. Now in the offline environment, I have to do the following:
$ git checkout master
$ git pull
$ git fetch -p
$ git branch -d foo
or shorthand:
git checkout master && git pull && git fetch -p && git branch -D foo
Is there a command I can execute to make this much shorter? Like
$ git complete foo
or something along those lines..?
aliases can be used for solving this problem.
An alias can be created by running:
$ alias cpfb="git checkout master && git pull && git fetch -p && git branch -D"
Now, you can execute
$ cpfb foo
which will execute those commands specified in the alias.
Setting alias through terminal lasts for only that particular terminal instance.
Hence, save them in ~/.bashrc to make the alias permanent.

Is there a way to add multi steps to a git alias?

I'm going to create an alias that does this:
I've added it to my git-bash .bash_profile but I'd like to see if there is a way to add it as an alias so I don't have to use git bash
Git finish will push to current branch # Eg. gf "commit message"
gf() {
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
git add . && git commit -m "$1" && git push origin "$CURRENT_BRANCH"
} # Git merge # Eg. gm branch-name
gm() {
git merge "$1"
} # Git checkout # Eg. gc branch-name
gc(){
git checkout "$1" && gp
}
You could create a "Git-Subcommand" the name of this executable must be in this Format: git-{{ Name }} eg: git-acommit. This File must be located in one the Directories listed in $PATH, you can now execute git acommit and git will search for it in you $PATH.
To use this than in an alias simply run git config --global alias.gf acommit and you are finished. Please note: the alias step is unnecessary since you can name your file how you want, so you could also name it git-gf instead of git-acommit.
But without creating a separated file it's impossible to stack commands in a git alias.
Or you could use GitAlias to create an alias that executes a function or multiple Commands.
For an example check out the Recovery examples Section.

How to use git alias with shell function to build a commit message

I'm trying to create a git alias to automate my PR merge and approval.
The complete git command is this:
git checkout master && git sync && git merge --no-ff -m 'Merged in hotfix/X-3144 (pull request #1112)' remotes/origin/hotfix/X-3144
Where X is the BitBucket prefix for the issue. Reading other stack overflow questions, I came up with this:
mergeto = "!f() { git checkout $1 && git sync && git merge --no-ff -m 'Merged in {$2}/B2B-{$3} (pull request #{$4})' remotes/origin/$2/X-$3;}; f "
So, naturally I'm using this to merge a PR that has conflicts. After I resolve these conflicts, I issue a git commit and git brings up nano with my previously typed commit message. My goal here is that the commit message is constructed using my parameters. So, for an example, if I use:
git mergeto master hotfix 123 1120
My final commit message was supposed to be "Merged in hotfix/X-123 (pull request #1120)" but instead I get a "Merged in {$2}/X-{$3} (pull request #{$4})"
How can I specify the parameters in such a way that this can work?
I Read a few stack over flow posts that show how to use a shell function inside a git alias, but couldn't find an example of escaping the text with parameters.
Using John Szakmeister and eftshift0 suggestions, I got it working by escaping double quotes with backlash:
mergeto = "!f() { git checkout $1 && git sync && git merge --no-ff -m \"Merged in $2/B2B-$3 (pull request #$4)\" remotes/origin/$2/X-$3;}; f "
Thanks for your help!

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

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.
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
If you are in a Windows Powershell:
git add . ; git commit -m "Testing one line command" ; git push
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
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.

Add new command on Windows Git Bash

I would like to add new command on my Git Bash (just now i am under Windows OS). I tryed to look on Web different solutions but I did not find anything. The command that i like to add is:
commitall -> git add --all && git commit -m "$*"
There is a way to add this command on Windows Git Bash?
Thanks
Use Git aliases, like so:
git config --global alias.commitall '!git add --all && git commit -m'
There's no need to use $* because all the arguments you specify will simply be appended to the line above, i.e. if you run:
git comitall "a message"
…the following will be executed:
git add --all && git commit -m "a message"

Resources