Git : Working with remotes

Remote repositories are versions of your project that are hosted on the Internet or network somewhere. Unless you are starting the project yourself, usually the first step in working on a git base project is to clone a remote repository. There are lot of option for hosting remote repositries in git, and GitHub is certainly the most popular one. It is free of charge if you are ok with your work to be public. It provides some really nice additional functionalities to manage your project work. I have created a TestRepo (master + one additional branch called features1) in my Github account which already has a file in it. We are going to use git clone to mirror the repository:

git_remote1

When you run git clone, the following actions occur:

  • A new folder called repo is made
  • It is initialized as a Git repository
  • A remote named origin is created, pointing to the URL you cloned from
  • All of the repository’s files and commits are downloaded there
  • The default branch (usually called master) is checked out.

For every branch foo in the remote repository, a corresponding remote-tracking branch refs/remotes/origin/foo is created in your local repository. You can usually abbreviate such remote-tracking branch names to origin/foo.

git_remote3

There are other commands too that do the simialar kind of function.

Use git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

If you already have a local repository with a remote URL set up for the desired project, you can grab all the new information by using
$git fetch <remotename>

Merging combines your local changes with changes made by others. Typically, you’d merge a remote-tracking branch (i.e., a branch fetched from a remote repository) with your local branch:

$git merge remotename/branchname

git pull is a convenient shortcut for completing both git fetch and git mergein the same command:

$git pull remotename branchname

Because pull performs a merge on the retrieved changes, you should ensure that your local work is committed before running the pull command. If you run into a merge conflict you cannot resolve, or if you decide to quit the merge, you can use git merge –abort to take the branch back to where it was in before you pulled.

To see which remote servers you have configured, you can run the git remote command. It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin – that is the default name Git gives to the server you cloned from. If you want to see more information about a particular remote, you can use the git remote show [remote-name] command.

git_remote2

If you want to rename a reference you can run git remote rename to change a remote’s shortname. For instance, if you want to rename origin to github, you can do so with git remote rename [remote-name]. This will also change your remote branch names.

git_remote4

If you want to remove a remote for some reason, you can use git remote rm [remote-name].

To add a new remote Git repository as a shortname you can reference easily, run git remote add [shortname] [url].

git_remote5

Lets make some changes in out local repository and then push to the remote. The command for this is simple: git push [remote-name] [branch-name].

git_remote6

This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to pull down their work first and incorporate it into yours before you’ll be allowed to push.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: