简体   繁体   English

在 git 别名中使用变量

[英]Using variables in git alias

I'm trying to create an alias for checking out the master branch and merging the branch I was on before switching.我正在尝试创建一个别名来检查主分支并合并我在切换之前所在的分支。 Right now, my alias looks like this: med = ";f() { git checkout master; git merge ${1}; git branch -d ${1}; }; f"现在,我的别名如下所示: med = ";f() { git checkout master; git merge ${1}; git branch -d ${1}; }; f"

It means that every time I want to use it, I have to call it using the current branch's name: git med topic .这意味着每次我想使用它时,我都必须使用当前分支的名称来调用它: git med topic What I want is avoid it, calling git med without arguments.我想要的是避免它,在没有 arguments 的情况下调用git med

I know that I can get the current branch's name like this: git rev-parse --abbrev-ref HEAD , but after I have switched to master, I can no longer use it.我知道我可以这样获取当前分支的名称: git rev-parse --abbrev-ref HEAD ,但是在我切换到 master 之后,我不能再使用它了。 So, I need to save it in a variable before switching to master.所以,我需要在切换到 master 之前将它保存在一个变量中。

How do I work with variables in git alias?如何使用 git 别名中的变量?

I'm on Windows, using posh-git.我在 Windows,使用 posh-git。

This is a combination of my comment and @torek 's:这是我的评论和@torek 的组合:

med = "!f() {BRANCH=`git symbolic-ref --short HEAD`; git checkout master; git merge $BRANCH; git branch -d $BRANCH; }; f"

which seems to work.这似乎有效。 (And bails out early, rather than late, if you're in a detached HEAD state) (如果您处于分离的 HEAD 状态,则尽早退出,而不是延迟退出)

I found in bash (debian) that wrapping the alias in double quotes didn't work.我在 bash (debian) 中发现用双引号括起别名不起作用。

I had to use single quotes:我不得不使用单引号:

'!f() { BRANCH=`git symbolic-ref --short HEAD`; git checkout master; git merge $BRANCH; git branch -d $BRANCH; }; f'`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM