DSS Blog

Git Your Way Up Collaboration

Written by Data Science Salon | Oct 25, 2022 5:45:11 PM

Software development is a learning process in itself. Not only do developers learn in retrospect, but they also need to frequently update a piece of code while working on a project. This iterative development process oftentimes calls for updating the code in the light of new requirements or may come with a better understanding of the existing ones. The code modification is a one-way road implying that it becomes difficult to roll back the code changes to previous versions unless every code version is saved. It can be done by saving multiple files with similar names and keeping track of those, but that sounds quite cumbersome. Ugh! 

So how will you maintain different versions of code to support active and rapid software development? 

Git comes to the rescue that provides a simple and intuitive way to version your code. This makes it easier to roll back changes and apply tags to a version of your code. The post will explain what Git is, how it works, and how you can quickly get started with it. Also, stay tuned till the end of the post to learn some of the excellent resources to get hands-on with Git.

What is Git?

Git is defined as an Open Source Distributed Version Control System primarily used to store files and keep track of different versions of these files. Git provides features like branching and merging to facilitate collaboration across teams. It also gives the ability to store these files remotely and locally on the developers' computers.

Why is it Needed?

Software development requires developers to work simultaneously on different project modules. Therefore, Git is a must to ensure that the latest code is synced among the developers with an option to roll back to an older version of the code in case of a code bug. Git also provides features like branching and merging enabling shared codebases among several projects.

Another reason why you should choose Git is the need for collaboration across different teams. Yes, you alone are not enough to spin the wheels and write entire software code, take it to production, debug and fix it (if God forbid the code breaks which is inevitable). 

Getting Started with Git

You need to install a client on your local to sync local files or code to a remote repository

Installing Git

On macOS, you can install Git using homebrew by running the below command in the terminal.

brew install git

 

An output like the below would appear.

 

If you prefer a GUI over a terminal for Git operations, it can be installed by running the below command in the terminal

brew install git-gui

 

The terminal would look like the below.

 

Run the below command in the terminal to confirm if the installation was done properly 

git --version

 

The output would display the latest git version installed on your computer (2.37.0 at the time of writing this article).

git version 2.37.0 (Apple Git-136)

 

Other ways of installing Git on macOS are listed here. You may refer to the official installation guide for installation on other operating systems such as Windows, Linux, or Debian.

Create a Local Git Repository

Create a new directory and rename it to “sample-project”. Using a terminal, go into the “sample-project” folder by running the below command.

cd sample-project0

 

Add a local Git repository to the “sample-project” using the below command.

git init

 

The output looks like the below.

Initialized empty Git repository in ~/sample-project/.git/

 

Staging and Committing Files

Committing means adding the code to the local repository. But before that, it has to be staged. Staging keeps track of the files that are about to be committed. This enables the developer to decide which files to commit.

Step 1: Staging

Use the below command in the terminal for staging a single file.

git add sample_code.py

 

For multiple files, run the below command in the terminal.

git add file1.py file2.txt file3.docx00

 

For staging all files, run the below command in the terminal.

git add .

 

Step 2: Committing

Run the following command in the terminal to commit the staged files to the local repository.

git commit -m "Hello World of Git Commit"

 

The text following “-m” is the commit message and acts as a label for the particular code change in the commit. It is useful in code reviews and code rollbacks if needed.

The terminal shows the below output message.

Git Push

After committing the new files or existing files to the local repository, these changes are pushed to the remote repository by running the below command in the terminal.

git push -u origin master

 

The above command pushes the files from the master branch of the local repository to the master branch of the remote repository.

Git Pull

To pull the latest changes from the master branch of the remote repository into the master branch of the local repository, you need to run the below command in the terminal.

git pull origin master

 

Pull request is required because the remote repository is revised constantly by other developers in your team and those changes need to reflect in your local repository for you to build over those modifications.

An Excellent Compilation of Resources to Learn Git

The post intended to set the premise for the need for code version control and explained how Git serves the purpose. The section below shares a compilation of an excellent list of free resources to learn different terms and concepts in Git. You will also get to practice hands-on towards the end of these resources and will be very comfortable working with Git workflows.

  1. Git and GitHub for Beginners - Crash Course 
  2. Git Cheatsheet
  3. An Intro to Git and GitHub for Beginners (Tutorial)
  4. Version Control with Git
  5. Introduction to Git and GitHub 

Summary

In this post you learned the need for collaborative, and distributed version control systems and how Git is a good choice for an open-source option. You also learned to get started with Git including its installation and configuration. The post also demonstrated how to push local changes to the remote repository as well as pull the latest changes from the remote repository. In the end, the readers are presented with a varied list of resources to further their knowledge of Git concepts and workflows.