How to deal with Git on our Shared servers

In this article, you will learn how to create local and remote Git repositories. Part of this is how to commit, push and pull files between your local computer and server.

Git is a free and open source version control software. It tracks changes made to the files, and gives you access to all versions of all files in the Git repository at any time. It also allows multiple developers to work simultaneously on one project at the same time using their own branches.

A Git repository (repo for short) is a space where Git stores project files and history of changes made to those files.

While there are a number of GUI (graphical user interfaces) Git clients, we will be using the command line (Terminal on Mac) for the purpose of this guide.

Let's imagine you developed a part of your site on your computer and want to move what you have done so far to our server. You also plan to continue developing the website and keep track of all the changes using Git.




Step 2. Create a local Git repository


1. We will use the Windows command line/Terminal on Mac to run Git commands.
  • To open a command line on Windows, press the Win + R keys on your keyboard. Then, type cmd and press Enter or click OK.


  • To open Terminal on Mac, press Command - spacebar to launch Spotlight. Type terminal and hit return/enter.

2. In the command line utility (Terminal), go to the folder that contains your website files using:

cd path\to\the\working\folder

E.g.,
cd C:\Users\User1\Documents\website folder



You can check the content of the folder by running the dir command:



3. To create a new repository, run the git init command:



4. Next, configure the email address and name that will be used for your Git commits. To do that run:

git config --global user.name "Your Name"
git config --global user.email youremail@example.com



where Your name is the name you want to set and youremail@example.com your real email address.


Step 3. Initial commit


Right now, your local Git repo and the website files are located in the same directory. But Git will not keep track of those files unless you tell it to do so.

1. Use the git status command to check what files Git sees as new/modified:



2. Next, tell Git what files you want it to add to the commit by using git add command:

  • To add all files run: git add
  • To add a single file run: git add file_name.ext



3. Once all the files are added, it's time to do the commit. Commit is to record changes to the repository or, in other words, to create a snapshot of the present state of the repository.

To commit, run the following command adding relevant messages to your commits will help you, and other people, find the right commit when required:

git commit -m "Type message about this commit here"



Step 4. Create a remote Git repository


1. Access your hosting account via SSH.

2. Navigate to the website's root folder. If you wish to create a repository for your main domain, navigate to /public_html folder. If you need to make some changes to the addon domain, move to the folder set for the addon domain (usually it's /youraddondomain.com or /public_html/youraddondomain.com folder).

3. Run the following command:

cd /path/to/folder



4. Run git init to initiate a new Git repository:



The git init command creates a non-bare repository. A non-bare repo contains a .git folder and has the checked-out working tree, i.e. actual files. A bare repository is nothing but the .git folder itself. The content of a bare repository is the same as the content of .git folder inside your local working repository. The purpose of a bare repository is to allow multiple contributors to push their work.

By default, Git rejects pushing to the currently checked-out branch of a non-bare repo (pushing to other branches is okay). You can change this by editing a config in the server repository with this command:

git config receive.denyCurrentBranch updateInstead



5. The last thing in this step is to connect your local repository to your remote one. Return to your command line/terminal window on your computer and make sure you are located in your local repository. Run the following command:

git remote add origin ssh://user@server.web-hosting.com:21098/home/user/path_to_folder



where:

  • user is your cPanel username
  • server.web-hosting.com is the servername for your hosting account
  • origin indicates a new place from which files will originate. You can use any word.
  • remote indicates the origin is not on the computer, but on a remote server.


Step 5. Push files to the remote server


To upload all files we previously added to the commit, run the following command on your local machine (Git will ask you to enter the password for the hosting account):

git push origin master



where master is a name of the branch on which the changes will be pushed (master is the default one).

The upload is done. You can go to cPanel >> File Manager >> domain's root folder (or run ls -la in the domains root folder via SSH) to check that the files were uploaded.


Step 6. Future edits


In case you edited a file on the server or added a new one, you will need to update your local repo from the remote one. Git uses the pull command to get updates from other repos.

First, add the edited file to commit by running these commands in the remote Git folder:

git add . (or git add name_of_file.ext)

Then, check what files will be committed:

git status

And commit the file:

git commit -m "Your message (e.g. new file name_of_file)"

In your local Git folder, run:

git pull origin master




Step 7. Restore a version of a file/files from a commit


To retrieve a specific version of a file from a commit use:

git checkout <commit number> <path/to/the/file>

The git log command shows information about all commits. Specifically, commit number, author, date, and the message.

To see just the commit numbers and the messages use: git log --oneline

E.g.: git checkout XXXXXXX test.txt
git checkout XXXXXXX (for all files)



Here, we’ve covered basic Git commands. Git is a powerful tool for collaborative development with many other advanced features. If you are interested in using Git more effectively, here are some resources to get started:

Glossary of commands used in this guide:

  • git init to initiate non-bare Git repository
  • git config --global user.name "Your Name" to configure the name used in Git
  • git config --global user.email youremail@example.com to configure the email address used in Git
  • git status to check what files Git sees as new/modified, what files will be committed
  • git add . to add files to commit
  • git add file_name.ext to add a specific file to commit
  • git commit -m "Type message about this commit here" commit changes to the repository
  • git config receive.denyCurrentBranch updateInstead to allow pushing to the checked out branch of a non-bare repository
  • git remote add origin ssh://user@server.web-hosting.com:21098/home/user/path_to_folder to connect your local repository to your remote one
  • git push origin master to push the local repository to the remote
  • git pull origin master to update the local repository with the data from the remote one
  • git checkout <commit number> <path/to/the/file> to retrieve a specific version of a file from a commit
  • git log --oneline to get information on previous commits

Updated
Viewed
48732 times

Need help? We're always here for you.

notmyip