为快进编写一个适用于任何跟踪分支的 git 别名吗?
我想创建一个别名来执行以下操作:
在 master
中工作,我希望 git ff
执行 git merge --ff-only origin/大师。但我想要相同的别名,当在
maint
中运行时执行 git merge --ff-only origin/maint
。对于任何其他跟踪分支也是如此。这可能吗?
I'd like to create an alias to do the following:
Working in master
, I would like git ff
to perform git merge --ff-only origin/master
. But I want the same alias, when run in maint
to perform git merge --ff-only origin/maint
. Likewise for any other tracking branches. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢其他两个答案,但是有一种更简单的方法,假设您的分支正在跟踪远程分支:
奖励:即使您的分支正在跟踪除原始仓库之外的存储库中的分支,或者跟踪不同名称的分支,这也将起作用。而且它真的很短。
Kudos to the other two answers, but there's an easier way, assuming that your branches are tracking the remote branches:
Bonus: this will work even if your branches are tracking branches in a repo other than origin, or tracking differently-named branches. Also it's really short.
我认为这可行:
I think this could work:
可能有更好的方法来做到这一点,但这个命令应该可以工作:
gitbranch
列出所有本地分支;grep "^\*"
选择当前的(以星号为前缀),cut -c 3-
删除星号,只留下分支名称。反引号将该命令的输出(即分支名称)粘贴到合并命令中。There could be better ways of doing this, but this command should work:
git branch
lists all local branches;grep "^\*"
selects the current one (which is prefixed by an asterisk), andcut -c 3-
cuts away the asterisk, leaving only the branchname. The backticks paste the output of this command, namely the branchname, into the merge command.基于@mipadi的回答和@Cascabel的评论:
这意味着
git ff
将按照OP的要求快进(无需提取)到当前分支的远程跟踪分支。但是,您也可以将其与要快进到哪个分支(本地或远程)的可选参数一起使用,例如 git ff master 会将当前分支快进到 master (如果可能) 。
Building on @mipadi's answer and comments by @Cascabel:
This means that
git ff
will fast-forward (without a fetch) to the current branch's remote tracking branch, as OP requested.However, you can also use it with an optional argument of what branch (local or remote) to fast-forward to, for example
git ff master
would fast-forward the current branch to master (if possible).