Skip to Content

Some useful git tips

Here will be useful git tips and just repeating commands I often use but can’t remember.

Setting up a remote git repository

Firstly we create a bare git repository on the remote server:

ssh user@remotehost
cd /repositories
mkdir project.git
cd project.git
git --bare init

Since the remote one is ready, let’s change to our local repository and configure it to work with remote origin:

cd /projects/project
git remote add origin ssh://user@remotehost/repositories/project.git
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git push --all

If you do not have a local git repository, for instance if you setup a new empty project, you can simply clone remote repository:

git clone ssh://user@remotehost/repositories/project.git

It will warn you that you’ve cloned an empty repository, but it’s okay.

Now we have ready to use local repository, synced with remote origin.

Checking out branches from the remote git repository

To list all available local and remote branches type:

git branch -a

To check out a needed remote branch:

git checkout -t origin/branch_name

To pull/push changes from/to remote branch:

git pull origin branch_name
git push origin branch_name

Removing a remote branch

This will delete some-branch within the remote repository:

git push origin :heads/some-branch