How to work on Remote Branches?

Submitted by Rajasekhar on Sat, 11/10/2018 - 13:30
  1. When working with remotes, we need branches that track their equivalent branch on the remote repo. These are called remote tracking branches.
    • Create a new branch on the local repo.
    • Push the branch to the remote repository.
    • See other collaborators fetching your branch and track it.
  2. Fetching from Remote Branches –
    • Remote tracking branches are local branches that reference a branch on a remote server.
    • Remote branches are referenced using a <remote name>/<branch name> syntax, for example, origin/master.
    • To share branches with others, you have to push them to a remote server.
    • To do this, use git push <remote> <branch name>, for example, git push origin my-feature-branch.
    • Collaborators can now fetch a remote reference to this branch. To work on it, they can use git checkout origin my-feature-branch. This will create a tracking branch.
    • A tracking branch is a local branch that tracks a single remote branch.
    • Cloning a repository creates a remote tracking branch for the master.
  3. To create remote tracking branches, use one of:
    • git checkout –b branch-name remote/branch-name.
    • git checkout --track origin/branch-name.
    • git checkout branch-name.
  4. Check tracking branches using git branch –vvt.
  5. To download changes from the remote, we use git fetch.
  6. To incorporate changes into the working copy, we have to merge. Shortcut for this procedure is git pull.
  7. Once a feature has been completed, we should delete the branch. To do this, we use git push --delete <branch-name>