You can use Git locally to manage file versions, but more power comes when you distribute your work and allows other to collaborate. You can accomplish this by “pushing” and “pulling” content to and from a remote server locations. In this example, of course, we will be using an InMotion Hosting server to demonstrate setting up your remote repository with Git.
As stated before in the Introduction to Git, there are free Git platforms available on the web, but you are forced to use their resources and follow their rules. For some users, managing a private server location provides more attractive options as far as customization goes.
Be sure to check out our full guide on how to install Git, if you have not completed that step yet.
Using Git to Publish Files
Using Git as a publishing vehicle can allow for easy file transfer and also allow others to use your project files. This can be very helpful if you want to host files that others will need to have access to, and thus require the latest version with most recent changes applied. With Git, you can manage the files locally, commit changes, and upload the changes without ever leaving a single command line.
In this case, we will be using the Git “Push” command. This exact use case is detailed completely in our full guide on how to publish files with Git.
How to Add a Remote Repository to your Server Using Git
The most important step for distributing your Git project is to establish a remote server location. We can refer to this as the “remote” repository as opposed to your “local” repository, which we have been using thus far.
To use a remote repository, you must only make sure that you have SSH access to your server. Once there, you can follow these steps to create your remote repository.
- Log into your server via SSH
- In a convenient location, create a new directory ending with the
production.git, for example)
- Enter the new directory
- Run this command inside the directory:
git init --bare
- Go back to your local repository
- Enter your working directory and run this command, submitting the “name-of-repo” with something relevant for your project and “userna5” with your cPanel or SSH username and “destination” with the host domain or IP:
git remote add name-of-repo userna5@desination:/home/userna5/production.git
- Having successfully added a remote repository, this command will push the contents of your local repository up to the remote repository:
git push name-of-repo master
“master” being the name of the branch (we will discuss branches later, but, for now, using the master branch exclusively is fine)
- Unless you are using SSH keys, you will be prompted for a password
How to Pull Recent Files from your Remote Repository
Our full guide on how to publish files with Git uses a “checkout” command to copy all of the files associated with a project to a convenient location.
Let’s suppose that you eventually have a collaborator working on a project with you. They will want to always make sure they have the most recent iteration of the project files.
To accomplish this, we will use a Git “clone” command to grab the project from the remote repository and, once cloned, use a “pull” command to make sure we have incorporated all recent changes.
- Open up a Terminal app
- Navigate to a convenient location and run the “clone” command:
git clone name-of-repo userna5@desination:/home/userna5/production.git
- Unless the user has SSH keys, they will be prompted for the SSH password
Using the example above, there will now be a directory called “production” in this location. This is effectively a local Git repository.
Supposing that changes are made to this project in the future, in order for the user to make sure they have the most recent updates, they will need to run a “pull” command:
git pull name-of-repo master
We now know how to set up a Git repository at a remote server location, how to “push” information to the server, how to clone a repository, and how to “pull” information from the server. We covered a lot here, but you should be getting a solid foundation in Git by now. Next, we will cover “tagging” various commits for record-keeping purposes.
Consider deploying files with GitHub actions.