0

I often use the git command git fetch origin master:master. I want to have an alias so I can just do git ff master. How can I do this? The only piece I am missing is getting the remote branch name. I know I can do git rev-parse --abbrev-ref master@{u} to get origin/master but I just need master from that.

So I imagine it will be something like git fetch $(git config branch.$1.remote) $(???):$1 and I just need to fill in the ???. Then I can add some error handling.

Related:

  1. Git pull without checkout?
  2. Merge, update, and pull Git branches without using checkouts
4
  • 1
    The way Git produces origin/master (for master@{u}) is to run the result of git config --get branch.<branch>.merge through the remote.<remote>.fetch refspec. You want to sidestep that, so just get branch.<branch>.merge directly.
    – torek
    Nov 22, 2019 at 17:39
  • @torek I need the remote branch name. git config --get branch.<branch>.merge gives me the local branch. Nov 23, 2019 at 18:20
  • No, it's the name as seen on the remote. Try it out: git rev-parse --abbrev-ref master@{u} says origin/master, so let's change it: git branch --set-upstream-to=origin/maint Branch 'master' set up to track remote branch 'maint' from 'origin'. git config --get branch.master.merge refs/heads/maint (that's the fully qualifieid name of their maint, not my maint: I don't even have a maint).
    – torek
    Nov 23, 2019 at 20:53
  • @torek Ah you're right! My bad. Why don't you post an answer? Nov 23, 2019 at 22:29

1 Answer 1

0

I got a working alias. Now I can do git ff master. Thanks for the tip @torek.

git-ff (I put it with my zsh functions)

[ -z "$1" ] && { echo 'No branch specified'; return 1 }
local remote src
remote=$(git config branch.$1.remote) || { echo 'No remote'; return 1 }
src=$(git config branch.$1.merge) || { echo 'No merge'; return 1 }
git fetch $remote $src:$1

.gitconfig

ff = !zsh -c \"git-ff $@\"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.