2.11.6. Work with Git
If you need your repository to be inaccessible to outsiders and all actions with it to be fully controlled by you, you can create your own Git server by following the steps described below.
Configure server
Git is installed by default on the hosting:
[example@hosting]$ git --version
git version 2.16.1
Perform the following actions:
- Navigate to the home directory:
cd ~ - Create a directory for the Git project:
mkdir repo.git - Navigate to the created directory:
cd repo.git - Create an empty Git repository (the
bareparameter creates a repository without a working directory, you will not be able to executegit add,commit, etc. commands from the server):git init --bare - Check if the repository directories have been created by executing the command
ls:[example@hosting]$ ls branches config description HEAD hooks info objects refs
Set up local PC
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 using the command
git clone.
Let's use the first one.
Perform the following actions:
- Create a directory for the Git project in the user's home directory and navigate to it by executing the commands:
cd ~ mkdir repo cd repo - Initialize the repository:
git init - Verify what has been created in the repository directory by executing the command
ls -a:[example@hosting]$ ls -a . .. .git
In this directory, you will work with project files, while the hidden directory .git stores the entire Git history and metadata of your project, including all objects (commits, trees, blobs, tags), all pointers to various branches, and much 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 a remote repository:
git remote add developer login@host:/home/hosting_account_name/repo.git - Check which changes will be sent to the server:
git statusIt is visible 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 - Send files from the local PC from the master branch to the developer server:
git push developer master - If you need to fetch changes from the server, use the command:
git pull developer master