Add new command on Windows Git Bash - windows

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"

Related

How to alias "git push" into itself + running a script?

I need to run a script which notifies my CI server after I push. Therefore I need to alias "git push" into "git push; powershell script.ps1". Here is what I am trying:
$ alias git push='git push; powershell script.ps1'
bash: alias: git: not found
Alternatively, denoting whitespace characters returns this:
$ alias git\ push='git push; powershell script.ps1'
bash: alias: `git push': invalid alias name
$ alias "git push"='git push; powershell script.ps1'
bash: alias: `git push': invalid alias name
How can I alias my script to run every time I push?
This worked:
git config --global alias.push "push; powershell ./script.ps1"
The command runs, but using "git push" still doesn't run my script.
Yes, I know that I can use webhooks for this, but it is imperative that the CI server remains fully local. Yes, I know that I can use NGROK for that, but my company does not allow it.
Git doesn't allow alias names to override proper command names. Use another label for your alias.
And if the alias content mixes a git command and shell commands, you'll have to prepend a ! to tell git to pass the command for the shell to interpret.
Try
git config --global alias.p '!git push; powershell ./script.ps1'
# then to invoke it, just
git p
Another slight improvement would be to use && instead of ; to chain your commands, in case the push fails. It would then stop from executing the unnecessary following.
git config --global alias.p '!git push && powershell ./script.ps1'

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.

Creating git aliases

I am trying to add the following aliases in ubuntu
alias l=log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
$ source ~/.aliases
bash: alias: --decorate: not found
bash: alias: --decorate: not found
bash: alias: --numstat: not found
I could use this command outside with git
I am not so sure why? Can someone help me? I tried googling but I did not go far with it. I do not know bash so much.
This is bit older question but it is very important to understand and create git alias as this will save lot of time of yours.
In your question you are close to answer just a silly mistake done is you are trying to create alias using script.
Alias needs to be defined in .gitconfig file. Not just alias but all config part like
[core], [color], [pack], [help], [alias] etc
I would like to share some basic and useful alias with you to have things handy and you can change it further per your need and daily usage
[alias]
lg = log -p
lol = log --graph --decorate --pretty=oneline --abbrev-commit
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
st = status
co = checkout
ci = commit -a -m
br = branch
ls = ls-files
po = push origin
f = fetch
p = pull
delete = branch -d master
com = checkout master
cob = checkout -b
unstage = reset HEAD
url = remote set-url origin
ign = ls-files -o -i --exclude-standard
cp = cherry-pick
You can also create an alias for a combination of multiple git commands in a single one as, for instance:
rdev = !git checkout dev && git pull && git checkout - && git rebase dev
Let me know if any other understanding needed.
You are almost there. You just need to put the alias in the right file. Because Git doesn’t automatically infer your command if you type it in partially, you can easily set up an alias for each command using git config like so:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
And then you use it the aliases like: git ci, git co, git br, git st in any repo.
You can also run an external command through an alias. In that case, you start the command with a ! character. This is useful if you write your own tools that work with a Git repository:
git config --global alias.visual '!gitk'
You might have also noticed that the config command takes in several parameters (like the --global one). If we look at the docs man git config:
For writing options: write to global ~/.gitconfig file rather than the repository .git/config, write to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn’t.
For reading options: read only from global ~/.gitconfig and from $XDG_CONFIG_HOME/git/config rather than from all available files.
See also the section called “FILES”.
There is also --system, which writes to /etc/gitconfig, --local, for the local repo .git/gitconfig, and --worktree, which is similar to --local.
But you can just directly edit the files themselves. It will look similar to this:
# in ~/.gitconfig
[alias]
lg = log --all --stat --pretty=oneline --graph --format='%h %d %an %cr %s' --oneline
l = log --all --stat --graph --format='%h %d %an %cr %s'
up = pull --rebase
br = branch --verbose -a
sfp = push --force-with-lease
You should set the alias in your git aliases and use it from the command line
You can directly edit the configuration file or do it from CLI:
Git Alias
Use the git config --global alias.<name> in order to add git alias
git config --global alias.l 'log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate'
now you should be able to use it with: git l
Ubuntu Alias
If you wish to add an alias to your shell in Ubuntu:
alias gitl='git l'

How to call multiple git commands using one .gitconfig alias

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.

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.

Resources