- Introduction to version control systems
- Git and GitHub
- Branching and merging
- How to contribute to open source
- How to sync your Repo
Git and GitHub are must-learn tools for every software developer. These tools not only make it easy to track changes in your personal projects, but they also make it seamless to contribute and coordinate team projects. This guide is to help every beginner to harness their skills and have an easy time learning and using these tools. Let’s get into it.
Version control systems are tools that are used by software developers to track and manage changes in a project. These tools keep track of the change made, who made and the time it was made. Thus, making it easy to turn back to the previous versions of a document and identify errors if they occur.
There are three main types of VCS, namely;
• Local version control system
• Centralized version control system
• Distributed version control system
A local version control system is a type of VCs that uses the local database in your computer to store the changes made. It stores them as a patch where every patch contains the changes made to the file since the previous versions and not the entire file. Consequently, to see how the file appeared at any instance, you have to add all the relevant patches to come up with the document.
A centralized version control system uses a central server to store the repository. Thus every developer must be connected to the central server to access the repository and make the changes. Even though it is quite easy to maintain a single repository in the server, in case the central server crashes, you may risk losing the data which is a major drawback. An example of centralized VCS is Apache Subversion which is abbreviated SVN.
Unlike the centralized VCS, every contributor has their own local copy of the main repository. Thus they can change, update and commit to their local repository without interfering with the main repository. The contributors copy the main repository by cloning it. Also, they have to stage, commit and push the changes to make them available for other contributors to see.
After knowing what a version control system let is us dive into GIT
Git is an open-source distributed version control software that was created in 2005 by Linus Torvalds for the Linux kernel. It allows a team of developers to work on a project where each of them has a copy of their main repository which is located in the central server.
Features of Git
- Free and open-source- you don’t have to pay anything to use Git. Also, its source code is available openly, hence you can modify it to your preference.
- Distributed development- Git supports cloning where the contributors can store a local copy of the main repository
- Supports non-linear development – git supports independent lines of code known as branches, which can be staged, committed, and updated independently of the main codebase. Hence, branching gives a developer a safe space to implement and test something new without interfering with the codebase. The branches can later be merged into the codebase.
- Scalable- Git is quite scalable making it easy to handle an increase in the number of collaborators.
- Secure – this tool uses the secure hash function (SHA1) for naming and identifying objects in a repository. Hence every change is well monitored making it impossible to make changes without being noticed. ##What is GitHub? GitHub is a hosting company that was founded in 2008 that provides a platform for developers to host and share their projects. it provides access control and management features for every hosted project. Basically, it offers source code management among other features of git.
There are many alternatives to GitHub such as GitLab and bitbucket. Consequently, you do not necessarily need GitHub to use git, but you require git to use GitHub.
You need to install git on your operating system. This guide here will help you.
Also, create a GitHub account, and let’s move on.
Let's discuss the diagram briefly;
After the user makes changes on the working file, git takes notice of the recently modified files. the user can check the modified files using git status command.
The modified files are added to a staging area which acts as the temporary location for the files.
All the staged files are ready to be committed/moved to a local repository. Up to this point the changes are only visible on the developer’s local machine and cannot be accessed by other contributors online.
To make the file available online, you need to host on an online hosting service such as GitHub. So, you need to push the local repository to sync it with the remote repository.
Take, an instance that the other contributors have pushed changes to the remote repository. You must sync your local repository with the remote repository to be up-to-date with the changes done. To do that you use Git Pull command which will sync your local repository.
Let’s now look at the git commands
The git status command lists all the files that have been modified recently and has not been added to the local repository. In the below case the Two Sum.md file has not yet been added to the local repository
PS C:\Users\ERICA WANJA\Desktop\coding problems> git status On branch main Your branch is up to date with 'origin/main'. Untracked files: (use "git add <file>..." to include in what will be committed) leetcode/Two Sum.md nothing added to commit but untracked files present (use "git add" to track) PS C:\Users\ERICA WANJA\Desktop\coding problems>
git add command is used to move the files to the staging area for committing. To stage your files use the commands below;
git add filename- to add a specific file
git add -all which is shortened to
git add -A stages all the files, that is the new, modified and deleted files. On the other hand the
git add . command stages only the new and modified files excluding the deleted files.
git commit moves the repository from the staging area to the local repository. In other words, it stores the snapshots of the changes made to the local repository instead of blindly copying the entire repository one more time.
Every time you are making a commit you must give a brief message explaining the changes made.
git commit –m “solved the two sum problem”
The pull command helps to keep your local repo up-to-date with the remote repository. It up streams any change made by another contributor in the remote repository to your local repository.
To pull the changes, you need to set the origin or parent remote repository
git remote add origin LinkToTheRemoteRepo
After setting the origin remote repository, you can now pull the changes using the below command;
git pull origin master
*Note: * it is a good practice that you pull the changes before the push command when working on a team’s project.
git push command transfers the commits changes from the local repository to the remote repository(GitHub). Thus, the changes you have made will be published on the central repository and made available online.
git push origin main pushes the commits to the main or the master branch
git push origin branchName pushes the commits to the named branch.(you will learn about branches shortly)
Branching simply means creating a different line of development (branch) where you can test and experiment new things before implementing them into the main codebase. Thus practicing branching will save you from messing up with production codebase.
After testing the new changes on the branch, you can later on integrate them with the main line of development. This act of integrating the branches to the main line of development is known as merging.
git branch branchname command is used to create a new branch
git checkout branchname command is used to move to the specified branch. After checking out to the branch you can now commit and push the changes.
git checkout-b branchname command is the short form of the above two commands. It creates a new branch and moves (checks out) to it at the same time.
A locally created branch is only available on your device and not available to the other team members. You can push this local branch to make it available remotely using the below command.
git push –u origin <name>
git branch –d branchname command is used to delete branches after merging the changes.
In some cases git may refuse to delete the branch if it has changes which have not been merged with the main branch which is a safety mechanism to prevent accidental loss of data.
However, if you are sure you want top delete the branch with uncommitted changes, you can use the command below;
git branch –D branchname
Also, you use the following command to delete a remote repository;
git push origin --delete branchname
I greatly appreciate that you stopped by. I hope the article has been of help. You can also this article on how to contribute on GitHub and how to sync your repository.