13

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.

RusinaRange
  • 341
  • 1
  • 4
  • 18
  • 2
    You could use an alias that runs a batch file. [This post](http://stackoverflow.com/questions/24914589/how-to-create-permanent-powershell-aliases) will help you out a lot. – byxor Aug 24 '16 at 11:10
  • 1
    That doesn't help at all. I want to run it with `git new` as stated and you can't make an alias like that. I also don't want to make a hacky powershell function, I want to make a git script on windows. – RusinaRange Aug 24 '16 at 11:19
  • 1
    Okay well it was only a suggestion. – byxor Aug 24 '16 at 11:47

2 Answers2

24

Create a batch file that contains the following commands:

git branch %1 origin/master
git checkout %1

Save it, let's say, as C:\Scripts\new-branch.cmd. (I never worked with PowerShell, I don't know its rules. However, it should work as well using the old Windows Command Prompt).

Test the batch file works as expected by running:

C:\Scripts\new-branch.cmd test1

It should output something along these lines:

Branch test1 set up to track remote branch master from origin by rebasing.
Switched to branch 'test1'
Your branch is up-to-date with 'origin/master'.

If you don't need the new branch to track the remote branch then you just add --no-track to the git branch command.

If everything goes well then run:

git config --global alias.new "!C:/Scripts/new-branch.cmd"

This makes the Git alias new available to your Windows profile in all repositories. If you need it only in one repository then remove --global and run the command when the current directory is in the repository where you need it.

Use it as:

git new test2
axiac
  • 68,258
  • 9
  • 99
  • 134
  • It's not exactly what I had in mind but definetly does what I want. Thanks! – RusinaRange Aug 24 '16 at 12:11
  • 1
    btw it's %1 not $1 – RusinaRange Aug 24 '16 at 12:21
  • You're right. I wrote it and tested it on Linux :-) Updated the answer now. – axiac Aug 24 '16 at 12:22
  • Also file paths for aliases have to be given with / not \ even on windows :o – RusinaRange Aug 24 '16 at 12:36
  • Well, while I forgot about `%1`, this thing about the slashes is something I didn't know. I started using Git after I stopped using Windows. It's never too late to learn new things! – axiac Aug 24 '16 at 12:53
  • 1
    You could make an alias `git config --global alias.new '!git checkout origin/master -b'`. You would use this like: `git new new_branch`. – Thomas F Aug 25 '16 at 23:11
  • @ThomasF I was not aware that one can pass the arguments of `git checkout` in this order. A closer investigation of the [documentation](https://git-scm.com/docs/git-checkout) and some tests revealed that your comment is correct. Post it as an answer! It can be improved. Take a closer look at the [`Git aliases`](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases) documentation page. There is no need for `! git` in the alias. – axiac Aug 26 '16 at 08:10
  • 1
    Quick note - I was trying this and it wasn't working. Turns out I was using cmd to set the alias and the file path needed to be in double quotes. I believe double quotes also works for Powershell so this would improve the answer. – Tom Troughton Apr 12 '18 at 10:52
  • @getsetcode Indeed, CMD puts the single quotes into the argument passed to `git` and the alias created this way doesn't work. I changed the answer to use double quotes. It works with both quote types in PowerShell though. Thank you for pointing it out. – axiac Apr 12 '18 at 11:31
7

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.

Tim Skov Jacobsen
  • 3,583
  • 4
  • 26
  • 23
Thomas F
  • 1,869
  • 3
  • 24
  • 25