Say I want a new git command, git new, that makes a new branch that is up to date with origin/master.
Is there a way I can make this script and have it available in all repositories on Windows from powershell?
edit: To clarify I want a git script not a powershell function. The only reason I mentioned powershell is because I don't use git bash.
Writing custom git commands are simple as these three steps: 1 Create a file with a name in the given format git-mycommand. And this file should be given executable access. 2 The created file should be included in $PATH. This can be done in bashrc or in zshrc 3 Then you can run the command git mycommand in any of the git repo you have. More ...
The git-c custom command is the equivalent to typing this: git add -A git commit -m '@mac: message in command line' The command to invoke it:
To maintain your git commands I suggest you create your own git repository where you can place your commands in. This enables you to track changes and share your custom commands. I am using zsh so in my .zshrc file in my home directory, I added the following line to add the path to my repository to the PATH variable.
The first line tells Unix that the file is to be executed by /bin/bash. The following command will print Hello World. Save it and now you can call your git command like the standard git commands you know. This is all you need for your setup.
You can use a git alias which uses git checkout
:
git config --global alias.new 'checkout origin/master -b'
This would then be used as git new new_branch
.
(Which is equivolent to git checkout origin/master -b new_branch
See the git docs for checkout. I tried the command and it worked, but when I looked at the docs, I didn't find a syntax that exactly matched my form. (Closest is git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
)
Note: @axiac, I may have used the !git
because it doesn't hurt, and I may have needed to do multiple commands to solve the problem, and didn't remove it when I was done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With