Skip to main content

Command Palette

Search for a command to run...

Git and Github - 2

Published
•5 min read•View as Markdown
U

Transformative Tech Leader | Serial Entrepreneur & Machine Learning Engineer Leveraging 3+ years of expertise in Machine Learning and a background in Web Development, I drive innovation through building, mentoring, and educating. Passionate about harnessing AI to solve real-world problems."

Git Adding New Files

After initializing your Git repository, it’s currently empty, so let's add some files. In this lesson, we’ll use Python files as examples, and we’ll explore Git's staging environment and how to prepare your code for your first commit.

Step 1: Create New Files

Start by creating a new Python file, hello.py, in your project folder. You can use your preferred text editor or create it directly in the terminal:

hello.py

# hello.py
print("Hello, Git!")

After saving this file in the project folder, let’s take a look at Git’s staging environment.


Understanding Git’s Staging Environment

The staging environment in Git allows you to select specific files for tracking and saving in your repository. When you reach a milestone, or finish a part of your work, you’ll add files to this staging area. Files in the staging area are “staged” and ready to be committed (saved permanently) to your repository.


Step 2: Stage the hello.py File

To add hello.py to the staging area, use:

git add hello.py

Check Git Status

After adding the file, you can check the status to see if it’s successfully staged:

git status

Expected Output:

On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   hello.py

The output shows that hello.py is in the staging environment and ready for your first commit.


Step 3: Adding Multiple Files to the Staging Environment

If you’re working on multiple files, Git allows you to stage more than one file at a time. Let’s create two additional files: a README.md for project documentation and a Python script for data operations.

README.md

# My Git Project
This is a sample project to learn Git using Python examples.

data_operations.py

# data_operations.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

After saving these files, you can add all the files in your directory to the staging environment by using:

git add --all

Alternatively, you can use the shorthand -A:

git add -A

Check Git Status Again

git status

Expected Output:

On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   README.md
        new file:   data_operations.py
        new file:   hello.py

Now all files are in the staging environment, ready for your first commit.


Git Commit: Saving Changes to Your Repository

Now that you’ve staged files in Git’s staging environment, it’s time to commit them. A commit in Git is like a “save point” or snapshot of your current project state, allowing you to save your changes and come back to them later if needed.


Step 1: Committing Your Staged Files

To make a commit, use the git commit command along with the -m flag to add a short message describing what changes you’re saving.

git commit -m "Initial commit: added hello.py, README.md, and data_operations.py"

What the Command Does:

  • git commit tells Git to commit the staged changes.

  • -m stands for “message” and allows you to add a commit message directly from the command line.

  • "Initial commit: added hello.py, README.md, and data_operations.py" is the message describing the changes. This is helpful when you or others need to understand what each commit accomplishes.

Why Write Descriptive Commit Messages?

Descriptive commit messages are crucial in Git, especially when collaborating with others or revisiting your own work later on. A good commit message gives context to the changes you’ve made.


Step 2: Checking the Commit History

To view a log of all commits you’ve made, use the git log command:

git log

Example Output:

commit 1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p
Author: Your Name <your.email@example.com>
Date:   Mon Oct 28 10:32:40 2024

    Initial commit: added hello.py, README.md, and data_operations.py

Each commit in Git has a unique ID (known as a SHA-1 hash), allowing you to reference specific versions of your project. This log provides a detailed history of your work.


Making Additional Commits

If you make changes to any files, you can stage and commit them again to save a new snapshot of your project’s state. Let’s modify our hello.py file and add a new feature to data_operations.py to see how this works.

hello.py

# hello.py
print("Hello, Git!")
print("Welcome to version control with Git!")

data_operations.py

# data_operations.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b
  1. Stage the Changes:

     git add --all
    
  2. Commit the Changes:

     git commit -m "Updated hello.py with a welcome message, added multiply function to data_operations.py"
    

Step 3: Undoing Commits

Git makes it easy to roll back changes if necessary. Here are some useful commands for undoing commits:

  1. Undo Last Commit (Keep Changes in Staging):

     git reset --soft HEAD~1
    
  2. Undo Last Commit and Unstage Changes:

     git reset HEAD~1
    
  3. Discard All Changes:

     git reset --hard HEAD~1
    

Warning: --hard will delete any uncommitted changes permanently.


Summary of Key Commands

CommandDescription
git commit -m "message"Commits staged changes with a message
git logShows a log of previous commits
git diffShows changes made since the last commit
git reset --soft HEAD~1Undo the last commit but keep changes staged
git reset HEAD~1Undo the last commit and unstage changes
git reset --hard HEAD~1Undo the last commit and discard changes completely

By committing your changes, you’re effectively building a history of your project that you can navigate, inspect, and return to whenever needed. In the next lesson, we’ll dive into Git branching, a powerful feature that enables you to work on new features independently without affecting the main project.

More from this blog

Coder Connect

23 posts

Join the coding revolution: learn, innovate, transform. The only limit is your mind."