2.11.6. Working with Git
If you need your repository to be inaccessible to outsiders and all actions with it are completely controlled by you, you can create your own Git server by following the steps described below.
Server Tuning
Git on the hosting is installed by default:
[example@hosting]$ git --version git version 2.16.1
Follow these steps:
- Change to your home directory:
cd ~
- Create a directory for your Git project:
mkdir repo.git
- Change to the created directory:
cd repo.git
- Create an empty Git repository (parameter
bare
creates a repository without a working directory, you cannot execute commands from the servergit add
,commit
etc.:git init --bare
- Check if the repository directories have been created by running the command
ls
:[example@hosting]$ ls branches config description HEAD hooks info objects refs
Local PC setup
There are two main approaches to creating a Git repository:
- Importing an existing project or directory into Git.
- Cloning an existing repository from the server with the command
git clone
.
Let's use the first one.
Follow these steps:
- Create a directory for the Git project in the user's home directory and change to it by running the commands:
cd ~ mkdir repo cd repo
- Initialize the repository:
git init
- Check what was created in the repository directory by running the command
ls -a
:[example@hosting]$ ls -a . .. .git
This directory is where you work with the project files, and the hidden .git directory contains all of your project's Git history and meta information, including all objects (commits, trees, blobs, tags), all pointers to various branches, and more.
- Create 3 test files:
touch index.php index1.php index2.php
- Add files to the index:
git add .
- Commit the files:
git commit -m 'First commit'
- Add the remote repository:
git remote add developer login@host:/home/имя_хостинг_account/repo.git
- Check what changes will be sent to the server:
git status
It can be seen that the current branch is master and 3 new empty files will be uploaded to the server:
[example@hosting]$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.php new file: index1.php new file: index2.php
- Upload files from your local PC from the master branch to the developer server:
git push developer master
- If you need to get changes from the server, use the command:
git pull developer master