Git checkout remote branch
In this tutorial let us look out how to use git checkout remote branch and related concepts. The
git checkout command lets you navigate between the branches created by
git branch. The repository you maintain on Github/VSTS/Bitbucket/Gitlab is the remote repository which you always clone on your local machine for collaboration. By default the cloned version refers to the remote origin of a particular branch which you have checkout.
Developers use git to manage each feature in a different branches which is a dramatic shift from the SVN workflow. It makes the developers easy to experiment lot of things on a local branch and also no need to fear on destroying the remote branch code. Once the functionality is completed the developers can sync the feature branch to the remote branch.
Managing Git Remotes
$ git remote
$ git remote show origin
* remote origin
Fetch URL: https://askmein.visualstudio.com/_git/gitDemo
Push URL: https://askmein.visualstudio.com/_git/gitDemo
HEAD branch: master
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
You could always use git remote and git remote show origin to find out the certain information about your git remote repository. As you can see it returns the output such as
- Remote repository URL
- Remote Branch name
- Remote tracking branch status
List all remote branches in Git
If you want to know the list of all the branches available in Git remote before doing a checkout then you can run a command git branch -a to list all the remote branches
git branch -a
Git checkout remote branch
Git checkout let us to switch from the current branch into another branch. Below are few reason on switching branches in Git –
- to switch from a one local branch to another local branch.
- to create a new local branch from a git remote branch
Checking out the remote branch updates the files in the working directory to the latest code stored in the remote branch.
First fetch the remote branches using command git fetch
Second checkout the remote branch by using git checkout command.
Replace branchname with your desired remote branch name.
git checkout branchname
Additionally you can checkout a new local branch and reset it to the remote branches last commit. Below command will checkout the branch master and reset it to remote branch last commit.
git checkout -b master
git reset --hard origin/master
If you are using the older git version(< 1.6.6 ) then you need to slightly change the command for fetch and checkout the remote branches.
$ git fetch origin
//Fetches a branch
$ git branch -v -a
//Fetches all the remote branch with git
$ git checkout -b develop origin/develop
//checkout the remote branch and also creates the local branch with git the name provided.
How do you track a remote branch during git checkout?
If multiple people want to work on the same branch in that scenario you need to use –track option with the git checkout command.
The below command ensure to create a new local branch with the same name as the remote one – and directly establishes a tracking connection between the two.
Set up to track remote branch
$ git checkout --track origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'<br/>
Git Checkout Remote Branch Best Practices
Use git pull before pushing code – Ensure to pull the latest code from the origin as the other team mates might have pushed their changes into remote repository. Merging these changes into your local branch is necessary before you push anything to remote.
Have smaller Commit often – Ensure you split your task into smaller features and keep the commits small and do commit frequently so that you can avoid merge conflicts.
Commit only working code – Once you have finished with developing the feature and unit tested commit the code to remote. Do not merge or commit the non-working code. This might break the entire solution on remote.
Use branches – Create as many branches as you need . Follow a proper naming conventions for the branch you create.
Clear messages on Commit – Include a meaningful message while commit, this will ensure what functionality is been added or modified to other users.