Git remote branches: how to create, track, remove/delete

1. Here’s how to to create a remote branch in git::
git push origin origin:refs/heads/my_branch
… or even simpler and more straightforward:
git branch my_branch # create branch locally
git push origin my_branch # push it to server

2. To push your changes to the remote branch:
git push origin my_branch

3. To delete the remote git branch:
git push origin :my_branch
or the “formal” version
git push origin :heads/my_branch

NOTE: The difference between create/update and delete is 1 char – the colon (“:”), so be careful what you’re typing or you can easily end up deleting a remote branch instead of pushing changes into it.

NOTE: What the delete command does is tell the remote git machine to push “nothing” (the lack of any name before the “:”) into my_branch, which essentially removes it. A bit convoluted, but hey – that’s git here… 🙂

4. Tracking and changing code in remote branches.
If someone else has already created a branch on the remote machine and you now have to work with it and add changes to it, then you’ll need to create a local “tracking” branch, which will mirror or “track” the remote branch. You do all your changes on the local branch, then push those changes to update the remote server.

Here’s how:

$ git clone me@git-server:/path/to/repo.git
$ git branch -r                 # List all remote branches
$ git checkout --track -b their_branch origin/their_branch
$ ...                           # do your work
$ git fetch origin              # sync to server before pushing.
$ git push origin their_branch  # push your origin

Alternatively, instead of git clone.. you could do git fetch origin if you already have the repository downloaded. This will avoid re-downloading the entire tree, most of which you already have. Similarly, in the step before push you can do git pull origin their_branch to sync just their_branch.

5 thoughts on “Git remote branches: how to create, track, remove/delete

  1. At times you may have local and remote branches which are named similarly or the same and you may be unsure if the local branch is tracking the remote one. In particular, using SmartGit it is very easy to locally create a branch and and push it to the remote and yet would not be a tracking branch.

    To check whether your local branches are tracking or not you may use either of these two commands:

    git config -l
    or
    git remote show origin

  2. … and to create a remote repository:
    ———————————-
    1. Setup the location on the remote server
    ssh user@remote-git-server
    mkdir new_project.git
    cd new_project.git
    git init –bare
    exit

    2. Setup the local git repository:
    cd new_project
    git init
    git add *
    git commit -m “Initial commit”
    git remote add origin user@remote-git-server:my_project.git

    3. Upload your code to the server:
    git push -u origin master

  3. … and if your “remote” repository is really a “local remote” e.g. you are storing the linux kernel git tree on a machine on your network which is not your developer machine but will serve as a git server for your team (IMPORTANT NOTE: in this case the name ‘origin’ is likely lalready taken so use another name for your ‘local remote’ server):
    1. get the project onto your workstation:
    cd my_projects_dir
    git clone user@real-remote:/dir/to/the_project_dir

    2. Ssh to your local-remote server and setup location for the project (same as above, use ‘–bare’ and so on)

    3. on the workstation add a new remote (this will be your local-remote server):
    git add remote our-git-server me@our-organization-git-server:/git/the_project_dir

    4. upload
    git push –all our-git-server

  4. Your note says “The difference between create/update and delete is 1 char – the column, so be careful…”. I think you meant to say “colon”, not “column”. It took me a minute or two to figure out what you meant, but I see it now. Thanks for posting this. It’s just what I needed.

Leave a Reply

Your email address will not be published. Required fields are marked *