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
cambunctious
  • 8,391
  • 5
  • 34
  • 53
  • 1
    The way Git produces `origin/master` (for `master@{u}`) is to run the result of `git config --get branch..merge` through the `remote..fetch` refspec. You want to sidestep that, so just get `branch..merge` directly. – torek Nov 22 '19 at 17:39
  • @torek I need the remote branch name. `git config --get branch..merge` gives me the local branch. – cambunctious Nov 23 '19 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 '19 at 20:53
  • @torek Ah you're right! My bad. Why don't you post an answer? – cambunctious Nov 23 '19 at 22:29

1 Answers1

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 $@\"
cambunctious
  • 8,391
  • 5
  • 34
  • 53