ZANARDI Bruno

Software Developper

About
Resume

Git Blog

How To: Work with (remote) branches

previous next

related commands:
checkout pull push

This post will explain how to work with remote branches.

Create a remote branch

The basic command to create a local branch remotely tracked is

$ git checkout -b <local_branch> <remote>/<remote_branch>
Branch <local_branch> set up to track remote branch <remote_branch> from <remote>. Switched to a new branch '<local_branch>'

With this command, you can set different names for local and remote branch. Or, if you want to use the same name, you can run the shortcut command:

$ git checkout --track <remote>/<branch>
Branch <branch> set up to track remote branch <branch> from <remote>. Switched to a new branch '<branch>'

Make a local branch tracked

You already have a local branch and you want to track it remotely? It is possible:

$ git push <remote> <branch>

Follow and share in remote branches

To download datas from / upload datas to a remote branch, you can use the same commands than for ‘master’, with specifying the branch name:

$ git push <remote> <branch>
$ git pull --rebase=prevent <branch>

For the pull command, you can avoid the --rebase option if you set the pull.rebase config at prevent with the --no-rebase option.

Delete a remote branch

Be really carefull with this command. Before using it, say your collaborators that this branch is done and be sure it won’t be used again.

$ git push origin --delete <branch>
previous top next

Reminder

checkout

Go to parent commit of a previous commit

$ git checkout <hash>^

Go to parent commit of a tag

$ git checkout <tag>^

Go to “grand parent” of a tag/commit

$ git checkout <object>^^

Create a branch and navigate to it

$ git checkout -b <new_branch>

Create a tracked branch and navigate to it

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

Create a branch, track it with a different name and navigate to it

$ git checkout -b <local_branch> <remote>/<remote_branch>

push

Download and merge datas from remote branch

$ git pull <branch>

Download and securely merge datas from remote

$ git pull --rebase=prevent <branch>

or with config:

$ git config --global pull.rebase prevent
$ git pull <branch>

push

Create a remotely tracked branch over a local branch

$ git push <remote> <branch>

Send datas to a remote branch

$ git push <remote> <branch>

Delete a remote branch

$ git push <remote> --delete <branch>
previous top next