1

I am using Windows 11 and git and trying to create a alias which will checkout and pull a branch before checking out the original branch, merging the just pulled branch and then push. I am having trouble setting a local variable for the original branch:

git config --global alias.sync "!f() { \
        CURRENT_BRANCH=($`git rev-parse --abbrev-ref HEAD`) && echo $CURRENT_BRANCH && \
        git checkout main && git pull && \
        git checkout $CURRENT_BRANCH && git merge main && \
        git push; \
      }; f"

In the above the echo $CURRENT_BRANCH statement (there for testing) always prints blank. What's going wrong? I've tried the following in case it's the syntax which is wrong but again just a blank line is printed.

git config --global alias.sync "!f() { \
        CURRENT_BRANCH=TEST && echo $CURRENT_BRANCH; \
      }; f"
SBFrancies
  • 3,987
  • 2
  • 14
  • 37
  • Side note, you could avoid checking out `main` completely by just fetching and merging `origin/main` instead. Then you don't even need a variable for the current branch. Also, `git switch -` could be used here, to avoid needing the variable. – TTT Feb 09 '23 at 16:40
  • That's a good point but for the next step I may be working with downstream forks, want to parameterise the branch to be merged, etc. so knowing how to use variables would be very useful. – SBFrancies Feb 09 '23 at 16:41
  • 1
    Your subcommand syntax is odd (`($\`git rev-parse --abbrev-ref HEAD\`)`). It's either `\`sub-command here\`` or `$(sub command here)`, but why both? – Romain Valeri Feb 09 '23 at 16:44
  • @RomainValeri - thank you, that may be an issue but as the second example shows my fundamental problem is being able to set and retrieve a variable. Even a hardcoded one doesn't work. – SBFrancies Feb 09 '23 at 16:46
  • I tried your second variant and it failed because `bash` expanded `"!f"` to `firefox`. I retried using apostrophes `'` instead of double quotes: `git config --global alias.sync '!f() { \ CURRENT_BRANCH=TEST && echo $CURRENT_BRANCH; \ }; f'` — then `git sync` printed `TEST` – phd Feb 09 '23 at 17:10
  • BTW `echo $CURRENT_BRANCH` inside double quotes is expanded when you declare the alias, not when you call it so apostrophes should help in that case too. Or use backslash screening: `git config --global alias.sync "\!f() { \ CURRENT_BRANCH=TEST && echo \$CURRENT_BRANCH; \ }; f"` – phd Feb 09 '23 at 17:13
  • @phd - thank you so much, single quotes fixed it. Would accept an answer based on your comment. – SBFrancies Feb 09 '23 at 17:20

1 Answers1

1

There's a problem with your aliases: bash expands "!" and "$CURRENT_BRANCH" in double quotes when you declare the alias, not when you call it. To protect the code either use apostrophes (single quotes ') instead of double quotes:

git config --global alias.sync '!f() { \
        CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) && echo $CURRENT_BRANCH && \
        git checkout main && git pull && \
        git checkout $CURRENT_BRANCH && git merge main && \
        git push; \
      }; f'

or escape shell metacharcters with backslashes:

git config --global alias.sync "\!f() { \
        CURRENT_BRANCH=\$(git rev-parse --abbrev-ref HEAD) && echo \$CURRENT_BRANCH && \
        git checkout main && git pull && \
        git checkout \$CURRENT_BRANCH && git merge main && \
        git push; \
      }; f"

PS. I also fixed $() syntax.

phd
  • 82,685
  • 13
  • 120
  • 165