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.
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.
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).
You need to install a client on your local to sync local files or code to a remote repository
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
|
The terminal would look like the below.
Run the below command in the terminal to confirm if the installation was done properly
|
The output would display the latest git version installed on your computer (2.37.0 at the time of writing this article).
|
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 new directory and rename it to “sample-project”. Using a terminal, go into the “sample-project” folder by running the below command.
|
Add a local Git repository to the “sample-project” using the below command.
|
The output looks like the below.
|
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.
Use the below command in the terminal for staging a single file.
|
For multiple files, run the below command in the terminal.
|
For staging all files, run the below command in the terminal.
|
Run the following command in the terminal to commit the staged files to the local repository.
|
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.
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.
|
The above command pushes the files from the master branch of the local repository to the master branch of the remote repository.
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.
|
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.
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.
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.