6

I am working on a branch called foo and I want to rebase my branch against origin/foo. In modern versions of git I could run

$ git pull --rebase origin foo

That's a lot of typing for a common operation. Additionally, because I have an old version of git (see this), I also need to run a fetch first so that my local graph doesn't seem confusing:

$ git fetch && git pull --rebase origin foo

How can I write that as a git alias?

Community
  • 1
  • 1
Barry
  • 286,269
  • 29
  • 621
  • 977
  • Does your branch not have an upstream set? I just use `git pull --rebase` and it uses the upstream branch. I don't think I have anything custom in place. – Chris Hayes Mar 23 '15 at 21:58
  • @ChrisHayes How do you check if it does? – Barry Mar 24 '15 at 14:26
  • A quick gander at [this question](http://serverfault.com/questions/175052/how-to-tell-which-local-branch-is-tracking-which-remote-branch-in-git) suggests `git branch -vv` should do the trick. For me, it shows `* master 9aa5d6a [origin/master] `, which correctly shows origin/master as my upstream. You can also use `git remote show origin` for more verbose output. – Chris Hayes Mar 24 '15 at 18:07

1 Answers1

8

The command

git symbolic-ref --short HEAD

outputs the short name of the current branch, if any (or HEAD, if the HEAD is detached). Therefore, you can use it inside a command substitution in your alias definition:

git config --global alias.<name> '!git fetch && git pull --rebase origin $(git symbolic-ref --short HEAD)'

where <name> stands for your alias's name.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • Better option is to use `git fetch && git rebase @{u}`. Or just [`git-up`](https://github.com/aanand/git-up). – Hauleth Mar 23 '15 at 22:10
  • @hauleth Perhaps, but that's under the assumption that the current branch has an upstream, which may not be the case. I guess it's up to you. Also, the OP uses an old version of Git, and I'm not sure when `@{u}` was introduced. – jub0bs Mar 23 '15 at 22:12