Version Control / Git

Key Areas Covered

  • Version Control

  • What is git? Why use it?

  • Github

  • What are the alternatives to git and what are their use cases

Version Control

  • Version control, also known as source control, is the practice of tracking and managing changes to software code.
  • Version control systems are software tools that help software teams manage changes to source code over time.
  • It helps make changes to your files while maintaining the flow.-It is a way of making changes without having to worry that something is going to get lost or fall out of flow.
  • It offers backup and history for any changes in case you need to backtrack.

Categories of Version Control

Version control systems allow source code management(SCM). SCM is used to track modifications to a source code repository

There are 2 main categories of version control

  1. Centralised Version Control System: Here, the entire project is stored on a central system. Some of the most common centralized version control systems are CVS, Subversion (or SVN) and Perforce
  2. Decentralised Version Control System: All of its history is mirrored on each developer's system. The three most popular of these are Mercurial, Git, and Bazaar. These systems do not necessarily rely on a central server to store all the versions of a project's files. Instead, every developer “clones” a copy of a repository and has the full history of the project on their own hard drive or local computers.

We will be focusing on Git as a version control system.

What-is-Git-1.jpeg

Git

What is git? Why use it??

Basically, Git is a system that records changes to our files over time. It also allows many people collaborate on a particular project, enabling them to have their own version of the project files on their local system/computers -Allows syncing your code on GitHub (an online service that hosts projects)

Git installation

you will need to have git installed on your personal computer to use it

  • On Windows To install Git, navigate to your command prompt shell and run the following command:

sudo dnf install git-all

Once the command output has completed, you can verify the installation by typing:

git version

  • On Mac If you have installed Homebrew to manage packages on OS X, you can follow these instructions to install Git:

Open your terminal and install Git using Homebrew:

brew install git

Verify the installation was successful by typing which git --version:

git --version

Creating a Repository in Git

To create a repository, you first need to -Navigate to the folder you need git to track -Initialise an empty git repository by running the command

git init

How Git works

Git repo is a folder for a project you want to track with git (e.g, a web app project) It tracks changes to every single file or folder under the main project folder.

During the process of making changes to files in the folder, there are different stages -Modified: These are files that have been changed but have not been committed -Staged: When we decide to commit the modified changes, we can add them to staging.

git add

-Committed: When we then make a commit, any files in the staging are are being added to the commit. And the snapshot of these files are then added to the commit history.

git commit

Staging Files

To be able to commit a file, you need to stage the changes first. To know files that have been changed and waiting to be committed or sent to the staging area, Git status To add files to the staging area Git add name of file e.g git add index.html The git add command stages the changes files. Therefore, running the git status command after staging shows that the file is ready to be committed. To remove a file from the staging area, run Git rm —cached index.html.

Staging files basically allows us review our work before we commit them

Undoing changes / actions in git

At times, we want to go back to the previous version our project was on This can be done via 3 different ;commands Checkout commit: this enables you to see how your folder was at a previous commit. It is readonly and hence you won’t be able to destroy or alter the commit history. Hence, it is really safe to run.

git checkout

Revert commit: this basically lets us undo a particular commit as though the changes never existed.

git revert

Reset commit: this command would totally delete any commits after the particular commit you want to go back to. And your project will be just as it was when you ran that previous commit. Running this command could potentially remove all the files in the deleted commits.

git reset

git push

Pushing is how you transfer commits from your local repository to a remote repository. The git push command is used to upload local repository content to a remote repository.

git log --online

Generally, the git log is a record of commits. The above commit is used to display the output as one commit per line.

Github

Github is a service that allows the setting up of hosting(remote) repositories. It enables developers to create a central online repository that multiple team members. Can have access to.

Version Control Best Practices

  1. Commit changes Atomically: Commit all files that belong to a task in a single operation to keep the project consistent at all times.

  2. Commit files with a single purpose: Keeping the scope narrow also makes it easier to back out a bad commit.

  3. Write good commit messages: good commit message makes it easier for a reviewer to understand the purpose of the commit later
  4. Do reviews before committing to a shared repository.
  5. Ensure every commit is traceable: For security and auditing, you must store the author of the change. You also need to store additional information, such as reviewer comments.

Git-Alternatives.jpeg

What are the alternatives to git and what are their use cases

Mercurial

mercurial.png Mercurial is a free, distributed source control management tool. It efficiently handles projects of any size and offers an easy and intuitive interface.

GNU Bazaar

bazaar.jpeg Bazaar is a distributed and client-server revision control system sponsored by Canonical. Bazaar can be used by a single developer working on multiple branches of local content, or by teams collaborating across a network.

Thank you for coming by. Do well to reach out on my social media for collaborations 😃

References

https://www.educba.com/what-is-git/

https://www.wired.com/2010/03/a-subversion-users-guide-to-mercurial-version-control/

https://www.youtube.com/watch?v=sgma0JIASe8

https://www.flynerd.pl/2017/09/git-dla-poczatkujacych-ucz-sie-interaktywnie.html

1
Subscribe to my newsletter

Read articles from Omolade Akinwumi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Omolade Akinwumi
Omolade Akinwumi