0

I'm trying to create an alias for my "merge to staging" workflow.

Let's say I've finished a task in branch "dev/layout_fix" and want to deploy it to staging after commiting.

This is my way:

  1. git checkout staging
  2. git merge dev/layout_fix --no-ff
  3. git push
  4. git checkout dev/layout_fix

Now I've tried this approach:

[alias] branch-name = "!git rev-parse --abbrev-ref HEAD" stagify = "!git checkout staging && git merge $(git branch-name) --no-ff && git push && git checkout $(git branch-name)"

but the alias branch-name is "staging" because of the checkout.

Is it possible to write the current branch name in a variable before staging is checked out?

perfectionist
  • 3,876
  • 1
  • 20
  • 33
bambamboole
  • 513
  • 9
  • 24

3 Answers3

1

Is there a possibility to write the current branch name in a variable before staging is checked out?

Sure. Since you're prefixing your git aliases with !, they're just shells scripts. So you can stick the output of git commands into variables the same way you would for any shell script, e.g., something like this:

[alias]
branch-name = "rev-parse --abbrev-ref HEAD"                             
mybranch = "!branch=$(git branch-name); echo my branch is: $branch"     

With that defined, I can run this:

git mybranch

And get:

my branch is: master

That ought to be enough to get you going.

larsks
  • 194,279
  • 34
  • 297
  • 301
0

You can get the current branch name using git describe --exact-match --all.

Instead of an alias, you can write a script and deploy it to /usr/libexec/git. You just have to place it there and call it git-stagify. I'm writing this from memory, but I believe that's the default path. You can find it by searching for where git-pull is located (with the dash), using locate or whatever is available in your distribution. This will give you the full benefits of the bash scripting environment, that may be missing when simply using aliases.

coladict
  • 3,650
  • 1
  • 10
  • 21
0

The full alias command looks like:

stagify = "!branch=$(git rev-parse --abbrev-ref HEAD) && git checkout staging && git merge $branch --no-ff && git push && git checkout $branch"

Thanks to @larsks for the right approach.

bambamboole
  • 513
  • 9
  • 24
  • 1
    One note: `git rev-parse --abbrev-ref HEAD` will print `HEAD` when you're in detached-HEAD mode. This is often the right answer, but not useful for your final goal (which involves changing `HEAD` temporarily). You might want to use `git symbolic-ref --short HEAD` instead, which will make the whole sequence fail (probably what you want—the other option is to merge by commit ID, which is a valid thing to do but perhaps not what you would like). – torek Feb 04 '16 at 16:54