Git - Perform Changes

Introduction

Hello, aspiring programmers! Today, we're going to dive into the exciting world of Git and learn how to perform changes in our projects. As your friendly neighborhood computer science teacher, I'm here to guide you through this journey with clear explanations, plenty of examples, and maybe even a joke or two along the way. So, grab your virtual hard hats, and let's start building our Git skills!

Git - Perform Changes

Understanding Git Basics

Before we jump into making changes, let's quickly review what Git is all about. Git is like a time machine for your code. It allows you to track changes, collaborate with others, and even travel back in time to previous versions of your project. Pretty cool, right?

The Git Workflow

In Git, we typically follow this workflow:

  1. Make changes to your files
  2. Stage the changes
  3. Commit the changes
  4. Push the changes (if working with a remote repository)

Now, let's break these steps down and see how we can perform each one.

Making Changes to Your Files

The first step in our Git journey is actually making changes to our project files. This could be adding new code, modifying existing code, or even deleting files. Let's start with a simple example.

Imagine we have a file called hello.py with the following content:

print("Hello, World!")

Now, let's say we want to make it a bit more personal. We'll change it to:

name = "Alice"
print(f"Hello, {name}!")

Great! We've made our first change. But how does Git know about this change?

Checking the Status of Your Repository

To see what changes we've made, we can use the git status command. This is like asking Git, "Hey, what's new?"

git status

You might see something like this:

On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

Git is telling us that we've modified hello.py, but we haven't staged these changes yet.

Staging Changes

Now that we've made changes, we need to tell Git which changes we want to include in our next commit. This process is called staging.

To stage our changes to hello.py, we use the git add command:

git add hello.py

If we want to stage all changed files at once, we can use:

git add .

Pro tip: The dot (.) means "everything in the current directory."

After staging, if we run git status again, we'll see:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.py

Great! Our changes are now staged and ready to be committed.

Committing Changes

Committing is like taking a snapshot of your project at a specific point in time. It's a way of saying, "I want to remember this version of my project."

To commit our staged changes, we use the git commit command:

git commit -m "Made greeting more personal"

The -m flag allows us to add a commit message directly in the command line. Always try to write clear, descriptive commit messages. Your future self (and your teammates) will thank you!

Viewing Commit History

Want to see a list of all the commits you've made? Use the git log command:

git log

This will show you a list of commits, starting with the most recent:

commit 1234567890abcdef1234567890abcdef12345678 (HEAD -> main)
Author: Your Name <[email protected]>
Date:   Mon Jun 12 10:00:00 2023 +0000

    Made greeting more personal

commit abcdef1234567890abcdef1234567890abcdef12
Author: Your Name <[email protected]>
Date:   Sun Jun 11 09:00:00 2023 +0000

    Initial commit

Pushing Changes (for Remote Repositories)

If you're working with a remote repository (like on GitHub), you'll want to share your changes with others. This is where git push comes in:

git push origin main

This pushes your commits to the 'main' branch of the 'origin' remote repository.

Common Git Commands

Here's a handy table of the Git commands we've covered:

Command Description
git status Check the status of your repository
git add <file> Stage changes for a specific file
git add . Stage all changes
git commit -m "message" Commit staged changes with a message
git log View commit history
git push origin <branch> Push commits to a remote repository

Conclusion

And there you have it, folks! You've just learned the basics of performing changes with Git. Remember, practice makes perfect. Don't be afraid to experiment and make mistakes – that's how we learn!

In my years of teaching, I've seen students go from Git novices to version control virtuosos. One of my favorite moments was when a student exclaimed, "Git is like a save point in a video game, but for code!" And you know what? They're absolutely right!

So go forth, make changes, commit often, and may your code always compile on the first try (but don't worry if it doesn't – that's what debugging is for!).

Happy coding, and see you in the next lesson where we'll explore more Git magic!

Credits: Image by storyset