Windows custom git commands

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?

  • Why can’t you merge in a bare git repo?
  • Is it possible to find out who created a tag in Git?
  • Retrieve missing files from remote repo?
  • Synchronizing two hosted git repositorities
  • Git: rebase onto development branch from upstream
  • Why does git merge allow me to lose a line
  • 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.

  • Is “git reset --hard” the same as “git reset --hard HEAD”?
  • Piping output from Git Bash to clipboard
  • Unsuccessful git push – cannot push again
  • Git strategy to keep on applying my changes to someone else's updeting branch
  • Is there a way to repeat a filter-branch operation on any new commits?
  • Git add not adding files
  • 2 Solutions collect form web for “Windows custom git commands”

    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
    

    You can use you one git alias which uses git checkout:

    git config --globla 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.

    Git Baby is a git and github fan, let's start git clone.