[英]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.假设我想要一个新的 git 命令 git new,它创建一个与 origin/master 保持同步的新分支。
Is there a way I can make this script and have it available in all repositories on Windows from powershell?有什么方法可以制作这个脚本并让它在 Windows 上的所有存储库中从 powershell 中可用?
edit: To clarify I want a git script not a powershell function.编辑:为了澄清我想要一个 git 脚本而不是一个 powershell 函数。 The only reason I mentioned powershell is because I don't use git bash.我提到 powershell 的唯一原因是因为我不使用 git bash。
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
.将其保存为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). (我从未使用过 PowerShell,我不知道它的规则。但是,使用旧的 Windows 命令提示符应该也能正常工作)。
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.如果您不需要新分支来跟踪远程分支,那么您只需将--no-track
添加到git branch
命令。
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.这使得Git
别名new
可用于所有存储库中的 Windows 配置文件。 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.如果您只在一个存储库中需要它,则删除--global
并在当前目录位于您需要它的存储库中时运行该命令。
Use it as:将其用作:
git new test2
You can use a git alias which uses git checkout
:您可以使用使用git checkout
的 git别名:
git config --global alias.new 'checkout origin/master -b'
This would then be used as git new new_branch
.然后将其用作git new new_branch
。
(Which is equivolent to git checkout origin/master -b new_branch
(这相当于git checkout origin/master -b new_branch
See the git docs for checkout .请参阅git 文档以进行结帐。 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>]
) (最近的是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.注意:@axiac,我可能使用了!git
是因为它没有伤害,而且我可能需要执行多个命令来解决问题,并且在我完成后没有将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.