0

I'm thinking of refreshing a git branch like this:

git checkout master && git pull && git co - && git rebase master

If any changes are pulled from origin then the previous branch gets overwritten and 'git co -' no longer returns to the previous branch.

(Of course I could use the name of the branch, but I'm looking for something generally applicable that can be transformed in an alias.)

Paul Baltescu
  • 2,095
  • 4
  • 25
  • 30
  • See http://stackoverflow.com/a/17722977/456814 to learn how to update your local `master` without having to switch branches. –  Aug 16 '13 at 07:26

3 Answers3

9

Why not just stay on the branch and do

 git fetch origin master && git rebase origin/master

and not worry about all the branch switching?

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Because I want each commit to only have a single parent commit. – Paul Baltescu Aug 15 '13 at 23:59
  • 2
    That reply doesn't make sense to me. My command line would have the same result as yours (rebase the current branch onto the latest master) without all the current-branch fiddling and extra merging. – Mark Reed Aug 16 '13 at 02:47
2

Mark Reed's answer solves the problem.

You can use git checkout @{-1} or git checkout -.

GIT v1.6.2 Release Notes state:

* @{-1} is a way to refer to the last branch you were on. This is accepted not only where an object name is expected, but anywhere a branch name is expected and acts as if you typed the branch name. E.g. "git branch --track mybranch @{-1}", "git merge @{-1}", and "git rev-parse --symbolic-full-name @{-1}" would work as expected.

(...)

* "git checkout -" is a shorthand for "git checkout @{-1}".

Here's an example: enter image description here

tymtam
  • 31,798
  • 8
  • 86
  • 126
1

Since a while Git has branch switching command:

git switch -

ohmyzsh has shortcut for it:

gsw -
Nik Sumeiko
  • 8,263
  • 8
  • 50
  • 53