Checkout existing remote branch

前端 未结 1 2088
生来不讨喜
生来不讨喜 2021-02-02 18:36

I have seen different ways to checkout existing remote branch:

Suppose my friend has pushed new branch \'bigbug\' and I want to check out and switched my local working c

相关标签:
1条回答
  • 2021-02-02 18:46

    The base for the checkout command is:

    git checkout --[options] <local branch> <remote>/<tracked branch>
    

    When you perform a git checkout -b bigbug origin/bigbug you are saying to Git to execute two commands:

    1. git branch bigbug origin/bigbug (git creates a branch with name bigbug and setup tracking to origin/bigbug)
    2. git checkout bigbug (git changes your workspace to match bigbug branch)

    When you perform git checkout -t origin/bigbug you are saying to Git to execute the same two commands above. The difference is that it will name the local branch with the same name of the remote branch (in the first example you can change the name of the remote branch to whichever you want). The -t options is the same of --track.

    In your last command, when you run: git fetch you tell Git to lookup on the remote repositories for new commits, branches, etc. Then when you run git checkout bigbug you tell it to change the workspace to match the bigbug branch. If you have a local branch with that name, Git will checkout it. If not, it will look the remote branches to one matching name and then create a local branch with the same name.

    So, when you use one or another it depends of what you want. Most of the time they will work as the same (except in the last example, when you already have a local branch with the same name of a remote branch). The most importante thing is to know exactly what the command and options do and combine them accordingly to what you want.

    0 讨论(0)
提交回复
热议问题