Introduction to Version Control and Command Line Basics
Workplace Context
Imagine you have joined a new development team and are preparing to manage project files and collaborate with your teammates. Your first task is to set up your working environment by familiarizing yourself with the command line and version control systems.
- The command line is an essential efficiency tool that developers use to navigate their projects, rather than using graphical interfaces which can often be slower.
- Version control allows multiple developers to work on a single project simultaneously, with many convenient features to enable seamless collaboration.
You will begin by going through multiple trainings that introduce you to these foundational tools and concepts.
Learning Objectives
By the end of this lesson, you will be able to:
- Explain the purpose of version control in software development.
- Perform basic file management tasks using the command line.
- Set up a GitHub account to manage repositories.
- Demonstrate how to initialize a Git repository to track changes.
Introduction to the Command Line Interface (CLI)
The command line interface (CLI) allows you to interact with your computer by typing commands instead of using graphical tools. As a developer, you will often use the CLI to navigate your file system, run scripts, and work with version control systems like Git.
While command line interfaces will differ visually, each has the same basic components:
Source: Edlitera
How to Access the Command Line
On Windows
There are a few ways to access the command line in Windows, including the Command Prompt, PowerShell, or Git Bash (if installed).
-
Command Prompt:
- How to Access:
- Press
Windows + R
to open the Run dialog box. - Type
cmd
and press Enter. - This opens the Command Prompt, where you can type and execute commands.
- Press
- Limitations: The Command Prompt lacks some of the advanced features developers may need, such as Unix-like commands (used in Git workflows).
- How to Access:
-
PowerShell:
- How to Access:
- Search for PowerShell in the Start menu and open it.
- Benefits: PowerShell is a more powerful tool than Command Prompt, with access to more advanced scripting and administrative commands. It supports most basic CLI operations, and many commands are the same as those used in Git Bash.
- How to Access:
-
Git Bash:
- How to Access:
- If you have installed Git from git-scm.com , you should have Git Bash installed as well. Search for Git Bash in the Start menu and open it.
- Benefits: Git Bash provides a Unix-like environment on Windows, supporting many Linux commands (such as
ls
,pwd
, andrm
) and all Git operations. This is the preferred tool for working with Git on Windows as it mimics the terminal environment used on Mac and Linux systems.
- How to Access:
On macOS
Mac computers come with a Terminal application pre-installed, which provides native access to the command line.
-
Terminal:
- How to Access:
- Press
Command + Space
to open Spotlight Search. - Type Terminal and press Enter to open it.
- Press
- Benefits: The Terminal on macOS uses a Unix-based shell, which is similar to Git Bash on Windows. It allows you to execute the same commands that are widely used in software development, making it a powerful tool for working with the file system, running scripts, and managing version control.
- How to Access:
-
Git Bash (optional):
- While the native Terminal is the primary tool for CLI operations on macOS, you can still install Git Bash if you prefer. However, it is unnecessary, as macOS’s Terminal supports all required Git and Unix-based commands.
Command Line Basics
Once you have opened the command line, you can start navigating through your computer’s file system and running commands. The basic commands you will frequently use include pwd
(print working directory), ls
(list files), cd
(change directory), and more.
-
pwd
(Print Working Directory):- Displays the current directory you are working in.
$ pwd
- Displays the current directory you are working in.
-
ls
(List):- Lists all the files and directories in the current directory.
$ ls
- Lists all the files and directories in the current directory.
-
cd
(Change Directory):- Changes to a different directory.
$ cd [directory]
- Changes to a different directory.
-
mkdir
(Make Directory):- Creates a new directory (folder).
$ mkdir [new-directory-name]
- Creates a new directory (folder).
-
rm
(Remove):- Deletes a file or directory (be cautious when using this command).
$ rm [file-name]
- Deletes a file or directory (be cautious when using this command).
These commands form the foundation of how you will navigate through your project files and directories as a developer.
Activity 1: Terminus Game for CLI Practice
To practice navigating the command line, try the Terminus Game. This game will help you get comfortable with essential CLI commands by navigating through different levels in a fun, interactive way.
Introduction to Version Control
Version control is an essential tool for tracking changes to your project files over time. It allows multiple developers to work on the same project simultaneously without overwriting each other’s changes.
Why Use Version Control?
Think of version control as a time machine: users can travel back in time to previous versions of a project, or create parallel timelines (which we call branches) to work on different project features independently, without affecting the “main timeline.”
This development time machine enables:
- Collaboration: Work with other developers on the same project.
- History Tracking: Keep a record of what changes were made, by whom, and when.
- Reverting: Easily revert to a previous version of your project if needed.
Scenario: Version Control in the Workplace
Imagine you are a front-end developer working for a company that is building a new e-commerce website. The project is large, and the team includes multiple developers, each responsible for different sections of the website, such as the homepage, product catalog, shopping cart, and user authentication.
The Problem
You are assigned to develop the homepage, and you start by creating a basic layout in HTML and CSS. While you are working, another team member is adding functionality to the shopping cart. Both of you are making changes to the shared project files on the same server. Without version control, several issues could arise:
- File Overwriting: If you and your teammate both make changes to the same file (for example, the
styles.css
file), one person’s changes might overwrite the other’s without either of you knowing. - No Change History: If a bug is introduced into the project, it will be difficult to pinpoint what caused the issue or who made the change, since there is no history of what has been modified.
- Collaboration Roadblocks: If two developers are working on the same feature at the same time, they might interfere with each other’s progress, leading to confusion and delays.
The Solution: Version Control
By using Git as your version control system, you and your teammates can avoid these problems. Here’s how Git would solve each of these issues:
-
File Overwriting Prevention:
- Git allows each team member to work on their own branch, isolating their changes from the rest of the project. When you are ready to share your work, you can merge your branch back into the main branch, and Git will ensure that no changes are lost. If two people made conflicting changes to the same file, Git will flag the conflict, and you will be able to resolve it by reviewing both sets of changes.
-
History of Changes:
- Every change made to the project is saved in a commit, which acts as a snapshot of the project at a specific point in time. Each commit includes a message explaining what was changed and who made the changes. If a bug is introduced, you can review the history and revert to a previous version before the bug was introduced.
-
Collaboration:
- Multiple developers can work on the project simultaneously without stepping on each other’s toes. Even if you and your teammate are working on the same files, Git will manage those changes, ensuring that each person’s contributions are preserved. Using pull requests, you can review each other’s work before merging it into the main branch, maintaining code quality and avoiding errors.
Outcome
With Git, your team is able to collaborate smoothly, track the history of changes, and quickly resolve conflicts without losing any work. This ensures that the project stays on track, and any issues that arise can be traced and fixed efficiently.
Activity 2: GitHub Account Setup
To begin working with version control, you need to create an account on GitHub, a platform used to host and manage Git repositories. If you have not already created a GitHub account or installed Git, follow the instructions below to do so.
Step-by-Step: Creating a GitHub Account
-
Sign up for a GitHub Account:
- Go to GitHub and click Sign Up.
- Follow the instructions to create an account, ensuring you use a valid email address.
- Remember, your GitHub account will be evidence of your work, so the username and email associated with it should be professional.
-
Install Git:
- After setting up your GitHub account, you need to ensure that Git is installed on your local machine. Visit git-scm.com and download the appropriate version for your operating system.
- You can confirm that Git is installed by running the following command in your terminal:
$ git --version
-
Configure Git with Your Account Information:
- After installation, configure Git with your GitHub account details:
$ git config --global user.name "Your Name" $ git config --global user.email "your-email@example.com"
- After installation, configure Git with your GitHub account details:
Now that your GitHub account is set up, you are ready to start tracking changes in your projects.
Activity 3: Initializing a Git Repository
Now that you are familiar with the command line and have set up Git, you can start using version control in your project.
Step-by-Step: Initializing a Git Repository
-
Navigate to Your Project Folder:
- Use the
cd
command to navigate to the directory where your project files are located.$ cd path/to/your/project
- Use the
-
Initialize a Git Repository:
- Run the following command to initialize Git in your project:
$ git init
This command creates a
.git
folder that allows Git to start tracking changes in your project. - Run the following command to initialize Git in your project:
-
Check Git Status:
- You can use
git status
to see the state of your repository and any untracked files:$ git status
- You can use
Examples from Industry
React Repository
The popular web application library React, developed by Meta, is an open-source project on GitHub with over 1,600 contributors. Well-maintained, professional open source projects like React are wonderful for familiarizing yourself with what a large repository looks like in the real world.
We will use the React repository on GitHub as an exemplar going forward to demonstrate various features of git and GitHub, but feel free to peruse it on your own time as well.
Start by navigating the home page of the repository, linked above, to identify some of its key components. Note the number of people watching the repository for changes, the number of forks (personal copies of the repository), the number of contributors, and some of the files. If you scroll down, you will also see the contents of the repository’s README file, an important component in repository documentation.
Some other items to take note of are the number of open issues, pull requests, and branches, which are all items we will discuss in later sections.
Knowledge Check
What is the primary purpose of version control?
- Select an answer to view feedback.
Which command is used to navigate to a different directory?
- Select an answer to view feedback.
Summary
In this lesson, you were introduced to the command line interface (CLI) and learned how to set up a GitHub account to manage repositories. You also explored the basics of version control and how to initialize a Git repository for tracking changes. These skills are foundational for working on software projects, both individually and in teams.
In the next lesson, you will learn how to stage, commit, and track changes in your project using Git commands.
References
- Git official documentation
- GitHub official documentation
- MIT Terminus Game
- MIT Terminus Game documentation