Up to now, we have learned how to create a local Git repository and add or update files to it. In this recipe, we will learn how to set up a remote repo and synchronize local code with it. We will be using GitHub to host our remote repository; feel free to choose any other code hosting service.
You will need a GitHub account. Sign up for a free account if you do not already have one.
To create a new repository on GitHub, log in to your GitHub account and create a new public repository:
$ git remote add origin https://github.com/sawantuday/mynewproject.git $ git push -u origin master
The first command, git remote
, adds a reference to the remote repository on GitHub and sets it as its origin. The next command, git push
, synchronizes all local content with the remote repository. The git push
command will show the details, as follows:
Now you can use your GitHub repository to share code with others or clone it to some other system. On the GitHub page, check the code tab to take a look at files in the repository.
Local repositories are good for personal work. A single person can work with them easily. A centrally hosted repository is required when you need to share the code base with a group of people. Everyone can make a local copy of the central code base and send their changes back to the central copy. GitHub solves this problem by hosting repositories that are accessible over the Internet. You can simply create a free public repository and share its URL with colleagues. Through access control, you can select who can check in their code. You can also set up your own centrally hosted repository. All you need is a system accessible over your network or Internet.
Here, we have created a central shared repository on GitHub. GitHub provides various options to initialize a repository and add code to it. As we already have our local repository ready, we just need to add a reference to the remote repo and synchronize our changes with git push
. The git remote
command is used to add a reference to the remote repository. We have set the remote repository as origin
, that is, the default remote repository. When using git push
or git pull
commands, if we do not specify any remote name it is assumed to be origin. Also, by default, Git marks the first remote as origin.
Next, we used Git push to push or synchronize our local contents to a remote copy. We have explicitly mentioned the remote name as origin and the remote branch as master. By default, Git always pushes to a remote named origin and branch master.
You can create your own remote copy on a local shared server. All you need is a normal user account on that server.
Log in to the shared server and create a bare repository with the following command:
$ git init --bare shared_repo
This will create an empty bare repository under the shared_repo
directory. If you check its contents, you will find all Git-specific files and directories.
Now you can clone this repo from your workstation or use the git remote add
command to add a remote to your already initialized repository. Use the following command to clone the repo. Replace the username with the user account on a shared server:
$ git clone ssh://user@ server_ip_or_name/full/path/to/repo
This command will ask for the password of the user account you have used in the username. Additionally, you can remove the password prompt by setting key-based SSH authentication with a shared server.
You can host your own simple static website with GitHub for free. All you need is a Git repository hosted on GitHub. Follow these steps to get your own GitHub page:
username.github.io
, where username
should be your GitHub username.index.html
if you do not have one. Add some content to index.html
.GitHub pages works with websites generated using static website generators such as Jeykyll, Hugo, and Octopress. By default, you get a github.io
sub-domain, but you can use your own domain name as well.
Check the manual pages for git remote and git push with man git-remote
and man git-push
respectively: