Git
Git is a distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency. It was initially developed by Linus Torvalds in 2005 for the development of the Linux kernel but has since become the standard tool for version control in software development.
Linus Torvalds invented both the Linux Kernel and Git. The Linux kernel is the heart of Linux. It’s how your operating system connects to your hardware and all of the applications running on your desktop.
Git is the tool Linus invented to record the changes you make to the software you are working on and to make collaborating with other programmers practical. You install git on your computer globally.
Project Development
When you start a new project, you make a new directory for that project, in your Projects directory, and then make a container for the project using LXD, venv or some other containerization tool. Then, you run the [git init] command in that container to start a new git repository for that project.
Websites must be built on a Virtual Private Server (VPS), to be able to use Git and SSH to securely log into your live website backend, from your local development environment. Otherwise, you use cPanel and WordPress’ Gutenburg editor to edit your website. Which is what I’m dong now.
The commercial server your website is hosted on is your remote server. Build your local development environment on your personal computers and then, use secure shell, SSH, to log into your remote server and edit the files on the remote server.
You’ll have to do all the maintenance and repair. Your hosting company does not have root access to your VPS. This is part of the learn how to use your tools to build your own, do it yourself, full stack web development strategy.
Github and other remote repositories are good for building all kinds of applications. You can build a remote repository holding all your config files and be able to share them with other people and use them on any computer.
It may be a good idea to use sFTP to start with. Secure File Transport Protocol is quite a bit easier to learn and get started with. It is a graphical version of SSH. SSH is a command line interface tool. Definitely learn how to use Git, sFTP and SSH to connect your local development environment to your remote repositories and your live websites. Start with sFTP because it is easier to learn and a valuable tool that you may want to use at some point.
Start by initializing a git repository in your project directory with git init. This creates a .git directory, which contains all the necessary Git metadata for tracking changes in your project. When you make changes, add them to your staging area using git add. This doesn’t commit the changes but marks them for the next commit. Once files are staged, you commit these changes with git commit. This creates a snapshot of your files at that point in time, along with a message describing the changes.
Git uses branches to manage different lines of development. The default branch is usually named main or master. You can create new branches for features, bug fixes, or experiments with git branch new-branch-name or switch to another branch with git checkout branch-name. Branches allow parallel development without affecting the main codebase until you choose to merge them.
You can connect your local repository to a remote server (like GitHub, GitLab or Bitbucket) where others can access your code. This is done with git remote add origin. git push sends your commits to the remote repository. git pull fetches and merges changes from the remote into your local repository.
When merging branches, conflicts can occur if the same part of a file has been changed in both branches. Git will notify you of these conflicts, and you’ll need to manually resolve them before committing the merge.
Git keeps a history of your commits, which you can view with git log. This history includes who made changes, when and what was changed, providing a detailed trail of project evolution. If you need to undo changes, Git offers commands like git reset, git revert or git checkout to move back to previous states or to undo specific commits.
Basic Workflow Example:
markdown
# Initialize a repository
git init
# Add files to be tracked
git add file.txt
# Commit changes
git commit -m "Initial commit"
# Create a new branch
git branch feature-branch
# Switch to the new branch
git checkout feature-branch
# Make some changes, add, and commit in the new branch
git add changed_file.txt
git commit -m "Added new feature"
# Switch back to main branch, merge feature-branch into main
git checkout main
git merge feature-branch
# Push changes to remote repository
git push origin main
Git’s distributed nature allows for robust collaboration, with each developer having a full copy of the repository. This model supports offline work and provides multiple backups of the project’s history.
Secure Shell
SSH, or Secure Shell, is a network protocol that provides administrators with a secure way to access a remote computer. It’s primarily used for executing commands on a remote machine as if you were sitting right in front of it. Using protocols like SCP (Secure Copy) or sFTP (SSH File Transfer Protocol). Forwarding other network connections through an encrypted channel.
SSH ensures that all data transferred between the client and server is encrypted, providing confidentiality and integrity of the data in transit.
SSH supports several authentication methods, the simplest, where you provide a username and password. More secure, where you have a pair of keys (public and private). The public key is placed on the server and the client uses the private key for authentication. Host-Based Authentication and Keyboard-Interactive Authentication are also available but less commonly used. Before accepting a connection, SSH clients verify the server’s identity. This is done by checking the server’s public key against a known list or by prompting the user to accept a new key.
The client initiates a connection to the server on port 22 (default SSH port). The server sends its public key to the client for verification.
Using algorithms like Diffie-Hellman, both client and server agree on a session key, which is used to encrypt further communication. This key exchange is secured by the server’s public key. All data exchanged after this point is encrypted with symmetric encryption using the session key.
Once authenticated, you can execute commands directly on the remote machine. Open a shell session for interactive use. Use file transfer protocols like SFTP or SCP for secure file operations.
SSH can also set up tunnels where other network traffic (like HTTP or database connections) is forwarded through the SSH connection. This can be used for secure browsing or accessing services behind firewalls.
Basic SSH Usage
bash
# Connect to a remote server
ssh username@hostname
# Copy a file from local to remote securely
scp localfile.txt username@hostname:/remote/path/
# Open an SFTP session
sftp username@hostname
Managing public/private keys securely is crucial. Keys should be protected with strong passphrases, and access to private keys should be restricted. SSH servers should be configured to disable root login, use strong ciphers and only allow necessary authentication methods. Both SSH clients and servers should be kept updated to patch any vulnerabilities.
SSH provides a robust, secure means of managing remote systems, ensuring that your interactions with remote services are kept confidential and secure against eavesdropping or man-in-the-middle attacks.
Sources:
Scott Chacon, Ben Straub, Pro Git, Apress, 2014
Grok