BCPeng
BCPeng

Reputation: 163

How to checkout a local branch from a remote's one with the same name?

Is there anyone knows how to
checkout a local branch from a remote's one with the same name with more convenient command?

I usually do it by typing remote branch's name in param manually

Ex: git checkout -b remotebranchName origin/remotebranchName

Or do it in Android Studio by
Check out as a new remote branch enter image description here

Is there any short equivalent command in Git ? Thanks a lot

Upvotes: 1

Views: 1230

Answers (1)

VonC
VonC

Reputation: 1324218

Yes the short version is:

git checkout remotebranchName 

As mentioned in the manual:

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

git checkout -b <branch> --track <remote>/<branch>

With Git 2.23+, the new command git switch would also create the branch and track its upstream branch in one line:

git switch remotebranchName 

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

$ git switch -c <branch> --track <remote>/<branch>

Upvotes: 2

Related Questions