fetch and checkout a remote git branch in just one command

ε祈祈猫儿з 提交于 2019-12-10 13:05:33

问题


If

  • I have local repo with a remote $REMOTE already set up
  • and a new branch $BRANCH exists on the remote repo that I haven't fetched, yet

can I fetch that branch and check it out into a tracking local branch of the same name in a single command?

I can achieve the desired result in two commands either with

git fetch $REMOTE $BRANCH
git checkout $BRANCH # or more explicitly git checkout -b $BRANCH $REMOTE/$BRANCH

or (inspired by this answer to Question How do I check out a remote Git branch?) with

git fetch $REMOTE $BRANCH:$BRANCH
git branch --set-upstream-to=$BRANCH $BRANCH

回答1:


There is no builtin command, but you could define an alias in your ~/.gitconfig:

[alias]
  fetch-checkout = !sh -c 'git fetch $1 $2 && git checkout $2' -


来源:https://stackoverflow.com/questions/30392969/fetch-and-checkout-a-remote-git-branch-in-just-one-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!