- A Git/GitHub beginner tutorial - Fri, Aug 11 2023
- Cockpit: GUI administration for Linux - Thu, May 25 2023
- Install and configure Traefik on Docker - Mon, May 8 2023
Git and GitHub
Version control systems (VCS) are software tools that help you keep a history of the changes made to files over time so that you can access previous versions on demand.
Git is one of many version control systems, along with others like Subversion and Mercurial. Git is a distributed VCS, which means that instead of having a central server to which all clients maintain a live connection, every user has their own copy of the full history of changes. This enables clients to work independently.
A central repository may be set up to collect the changes in each client's local repository and give each collaborator a full view of the current state of the project. This is useful but not mandatory, and allows Git to be more tolerant of server faults while enabling more flexible collaboration than centralized VCSs like Subversion.
On the other hand, GitHub is an online platform where developers can collaborate on software projects, manage their Git repositories, review code, track bugs and features, and secure code. Read this article to learn more about the interplay between Git and GitHub.
Installing Git
On most Linux distributions, you can install Git with the default package manager if it does not come preinstalled. For example, on Ubuntu, Git can be installed via its package manager, apt, as follows:
sudo apt install git
For Windows, visit the Git website and download the installer. On macOS, I recommend first installing the popular packet manager Homebrew and then installing Git with this command:
brew install git
Create a new repository
To create a new Git repository for your project, run git init at a command prompt, as shown in the snippet below. A Git repository is a tree of files and folders, usually named .git, for storing the metadata and history of tracked files. The name of the project in this tutorial is myproj. You can use any other name.
git init myproj
This is the default way to initialize a repository and results in the .git directory being created as a subdirectory of your project folder. If the project folder doesn't exist, a new directory will be created and initialized as a Git project.
On Linux and Windows, you can see some of the contents of a new Git project with the tree command, shown below. The switch -a allows you to see hidden folders like the .git directory.
tree -a myproj
Track changes with Git
After setting up a repository with git init, you need to select files in the project directory for tracking. In this section, you will create an example project file and select it for versioning.
Create a new Python script named os-version.py in your project directory, and save the following snippet in it.
import platform print(platform.system())
Your file should resemble the file in the screenshot below.
Tell Git to track all the files in your project directory, including the file you just created, with the git add command, as follows. This is also known as staging files.
git add .
Instead of a period (.) for the current working directory, you can also track a specific file by typing in its filename.
Run git status to get a report on the state of the files Git is tracking.
git status
You should receive feedback, as in the screenshot below, confirming that the new file has been staged.
Commit changes to a Git repository
Great. You now have changes to be committed. However, before you commit any changes, you need to configure the repository with a username and an email address. This helps to identify the source of commits and is useful in a project with many collaborators to show the authors of certain changes.
Run the git config command to set your username and email, as shown below. This tutorial uses weebo and weebo@weebo.com for the username and email address, respectively.
Git config user.name "weebo" git config user.email "weebo@weebo.com"
Type git commit, as below, to commit all tracked changes to the repository with a message (-m). The message should be short but descriptive, allowing you or your team members to know what changes are covered in a particular commit. Commits also serve as reference points that allow collaborators to navigate the history of a project.
Git commit -m “"dded script to determine platform"
As shown below, a successful commit operation confirms how many files were changed and the nature of the changes made.
Viewing the change history of a repository
It is often useful for a collaborator to walk through the history of a project to see the changes that have been made. Since Git is a distributed VCS, it has built-in support for independently investigating the timeline of changes in a repository.
If you are a second developer, you can execute the git log command with the oneline switch, as below, to see a list of recent changes. (A few more optional commits have been added for this demonstration.)
git log –oneline
You can go further to see the details of a particular commit with the git show command. For example, you can see the changes in the earliest commit using its commit hash, as follows:
git show
You can see what files were added along with the lines that were modified, as shown in the screenshot below.
You can also use git log with the -p switch to view the log with corresponding change details. This is often so verbose that a pager is needed to help you navigate the log in the terminal. Tap the up and down keys to move by line or the space bar to move by page. Quit the Git log with the q key.
This tutorial covers a newly created repository with a single branch, main. However, it is also possible to use the Git log to view the history of another branch, as shown in the command snippet below:
git log <branch name>
Connecting your repository to GitHub
To make your project more accessible to other collaborators, you can create a more central remote repository for your project on an online platform, such as Github. This is a common follow-up step, as remote repositories allow all collaborators to keep abreast of ongoing development.
First, add a remote repository named origin to the configuration of your project. If you don't already have a GitHub account, you should create one now here. The remote address should be the web URL of an empty GitHub repository. You can create a new repository and copy its web URL to follow along for this section.
Supply the web URL to the git remote command as follows to add the new remote:
git remote add origin <Your GitHub URL>
Now that your repository is associated with a remote origin, you can push your commits upstream. Turn on the set-upstream-to (-u) switch to set the main branch of the remote origin as the default remote branch for your local main branch. After this step, a simple git push or git pull will be enough to keep your repository current.
Git push -u origin main
After entering your credentials, you should receive confirmation of a successful push, as follows:
Now, you and your collaborators can push and pull changes to and from GitHub.
Uninitializing a Git repository
The easiest way to remove versioning from a project is to remove the .git subdirectory. On Linux, you can use the rm command within the project folder to remove .git and its constituents (-r) forcibly (-f), as follows:
rm -rf .git
Please use this with caution, as all the history of the project will be lost.
Subscribe to 4sysops newsletter!
Conclusion
In this beginner tutorial for Git and GitHub, you learned how to create a Git repository with git init, view the changes made to the repository, and then share it with other developers via GitHub. If you have questions, please leave a comment below.
Nice and informative article