Git is a powerful version control system that allows developers to efficiently manage their code repositories and collaborate seamlessly with team members. In this step-by-step guide, we will walk you through the process of setting up and using Git in your VPS server in an easy and beginner-friendly way.
Installing and configuring git
Connect to Your VPS Server First, log in to your VPS server using SSH.
ssh your_username@your_server_ip
Install Git: Most VPS servers come with Git pre-installed, but it's always good to verify. To install Git, use the package manager specific to your server's operating system.
# on Ubuntu/Debian sudo apt-get update sudo apt-get install git #On CentOS/RHEL sudo yum install git
Configure Git:
Before you start using Git, you need to configure your identity. Replace the placeholders with your own name and email address:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"
Check your configuration
git config --list
The git config –list command should return the configuration you added
user.name=YourUserName user.email=example@example.com
Using Git Basic Command
Create the Project Directory: First, create a directory for your project. You can do this using the
mkdir
command:mkdir project
Create Files for the Project: Navigate into the
project1
directory and create some files and a sub-directory:$ cd project $ touch file
Initialize Git in the Project Directory: Next, you need to initialize a Git repository in the
project1
directory using thegit init
command:git init
This command initializes an empty Git repository in the project
directory, allowing Git to track changes in the files.
Add Files to the Repository:
git add .
Make Your Initial Commit: After adding the files, it's time to make your initial commit. A commit is a snapshot of the changes you've made to the project. Each commit should have a meaningful commit message describing the changes.
git commit -m "Initial Commit"
The
-m
flag allows you to specify the commit message directly in the command. In this case, we use "Initial Commit" to indicate that this is the first commit for the project.
Adding SSH access to GitHub from your VPS
Check if you already have an SSH key pair by running:
ls -al ~/.ssh
Generate SSH Key Pair on VPS: Log in to your VPS via SSH, open a terminal, and run the following command to generate an SSH key pair:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
View and Copy Public Key: After generating the keys, run the following command to display your public key:
cat ~/.ssh/id_rsa.pub
Copy the entire output of the command, which is your public key.
Add Public Key to GitHub:
Click on your profile picture in the upper right
Select settings
Click on SSH and GPG keys
Click on the “Create SSH Key” button
Give the key a “title” to help you identify it
Paste the public key into the “key” field
Save the key
Test SSH Connection: To test if your SSH connection is working, run the following command in the terminal of your VPS:
ssh -T git@github.com
You should see a message like "Hi username! You've successfully authenticated, but GitHub does not provide shell access."
Update Git Configuration: To ensure Git uses SSH for pushing and pulling from GitHub, configure it to use the SSH URL. Run the following commands in your VPS terminal:
git config --global user.email "your_email@example.com" git config --global user.name "Your Name" git remote remote add origin git@github.com:your-username/your-repo.git
Now, your VPS should be set up to use SSH for interacting with your GitHub repository. Now , you can clone, push, and pull from GitHub using SSH without needing to enter your username and password each time.