Today, we're learning about Git, a system that lets you track changes to your code over time.

This guide will equip you with the essential Git skills to start using version control locally straight away. Here's what we'll cover: 

  • What is Git and why is it awesome? 
  • Installing Git on your machine 
  • Basic Git commands to navigate version control 
  • Putting it all together with a practical example 
  • Untrack and/or remove files 

Git: Your Code's Best Friend 

With Git you can: 

  • Travel back in time: If you accidentally deleted some code, with Git you can revert to previous versions seamlessly. 

  • Work Together Smoothly: While working on a project with a team, Git lets you see each other's changes and merge them effortlessly. 

  • Track your progress: Git keeps a detailed history of all your code changes, making it easy to keep track of your project. 

Getting Started: Git on Your Machine 

Git usually comes installed by default on most Mac and Linux machines. To check if this is true to you open your terminal application and type: 

git --version 

  • From Red Hat Linux, Git installed by default: 

 

  • From Windows 11, Git not installed: 

 

If you get no version number back, the first step is to install Git on your computer. Head over to the official Git website (https://git-scm.com/downloads) and download the installer for your operating system. The installation process is straightforward, just follow the on-screen instructions. 

Git Commands 101 

Now that Git is set up, let's learn some basic commands to get you started: 

  • git init: This command initializes a new Git repository in your current directory. It basically marks a folder as a project for Git to track. 

  • git add <filename>: This command adds a specific file to the staging area, which is like a temporary holding zone before you commit changes. It adds the file to be tracked by Git. You can also use – a period after ‘add 

  • git add .  which will add all files in the current directory to be tracked. 

  • git commit -m "<message>": This command captures the changes you've added to the staging area and creates a permanent snapshot of your project, along with a descriptive message. It’s like saving your code changes. 

  • git status: Use this command to see the status of your project. It will show you which files are modified, staged (ready to commit), or committed. 

  • git log: This command displays the history of your commits, like a time machine for your code! 

  • git ignore <filename>: This command will tell Git to not track that file. Usually files with credentials, passwords etc. 

Putting it into Practice: The Course Catalogue Example  

Let's use a practical example to understand better these concepts. Imagine you're building a course catalogue application. Here's how Git can help: 

  1. Initialize the Git repository: Open your terminal and navigate to your project directory. Then, type git init to create a new Git repository. 

  1. Create some files: Let's say you create three files: index.html, styles.css, and app.js. These files contain the code for your application. 

  1. Stage your changes: Once you've written some code, use git add index.html styles.css app.js to add all three files to the staging area. 

  1. Commit your changes: Finally, run git commit -m "Initial commit - Course Catalogue App" to create your first commit with a descriptive message. 

 

Removing Files 

Let's say you decide you don't need the "index.html" file anymore. Here's how to remove it from Git's tracking: 

  • Removing from Tracking (Keeping the File): 

Use git rm --cached index.html. This removes "index.html" from Git's tracking, but the file itself remains in your directory. 

  • Complete Removal: 

Use git rm -f code. This removes "index.html" from Git's tracking and also deletes it from your directory. Be careful! This option is permanent. 

 

Bonus Tip: Collaboration Magic 

Git is really helpful when collaborating with others. By hosting your Git repository on a platform like GitHub, you and your team can share code, track changes, and merge updates effortlessly. But that's a story for another day!