KandZ – Tuts

We like to help…!

Git 🧩 1d The Git Workflow Local Repository 🧩

1d The Git Workflow: Local Repository

Git’s power lies in its ability to track every change, giving you fine-grained control over your project’s history. This local workflow is the foundation of that control.

Understanding the Three States (The Git Lifecycle):

Before we start, remember these three crucial states that files in a Git repository can be in:

  • Working Directory: This is where you actually create, view, and modify your project files. Any file here that Git is tracking but has uncommitted changes is considered “modified.” Any new file that Git isn’t aware of yet is “untracked.”
  • Staging Area (Index): This is a temporary “holding area” where you meticulously prepare a snapshot of your changes before committing them. Think of it as a draft of your next save. Only changes explicitly git added are in the Staging Area, making them “staged.”
  • Local Repository: This is the .git directory—the brain of your Git project. It’s where all the permanent snapshots (commits) of your project’s history are stored. Once changes are git committed, they are saved here, making them “committed.”

Basic Git Commands

*Command: git init
Usage: Initialize a new repository in the current directory.

  • git add
    Command: git add [file-name] or git add .
    Usage: Stage specific files or all changes for commit.
    Example: git add README.md
  • git commit
    Command: git commit -m “Your commit message” or git commit
    Usage: Save the staged changes to the local repository.
    Example: git commit -m “Initial commit”
  • git status
    Command: git status
    Usage: Check the current state of your repository, including modified files, staged changes, and untracked files.
    Example: git status
  • git log
    Command: git log
    Usage: Display commit history with various options for detailed view or summary.
    Example: git log
  • git restore
    Command: git restore [file-name]
    Usage: Revert changes in a specific file to the last committed version.
    Example: git restore README.md
  • git restore –staged
    Command: git restore –staged [file-name]
    Usage: Remove changes of a specific file from the staging area without affecting the working directory.
    Example: git restore –staged README.md
  • git commit –amend
    Command: git commit –amend
    Usage: Edit the last commit message or add more changes to it.
    Example: git commit –amend -m “Updated initial commit”

Example: Simple Git Workflow

  • Step 1: Initialize a New Repository
    First, create a new directory for your project and initialize it as a Git repository.
mkdir my-simple-project
cd my-simple-project
git init
  • Step 2: Create and Modify Files
    Create a simple file, e.g., README.md, and add some initial content to it.
echo "# My Simple Project" > README.md

Check the status of your repository

git status
  • Step 3: Stage and Commit Changes
    Stage the README.md file
git add README.md

Check the status again

git status

Commit the changes with a message

git commit -m "Initial commit"

Check the status again

git status

View the commit history:

git log --oneline
  • Step 4: Modify and Amend the Last Commit
    Modify README.md again:
echo "This is a simple project." >> README.md

Check the status again

git status

Stage the README.md file

git add README.md

Amend the last commit message:

git commit --amend -m "Initial commit with description"

View the updated commit history:

git log --oneline
  • Step 5: Undoing Changes
    Discard changes in README.md:
git restore README.md

Check the status

git status

Unstage README.md if it was previously staged:

git restore --staged README.md

Do you remember?

  • How do you initialize a new Git repository?
  • How do you stage all changes in your working directory?
  • How do you stage specific files?
  • How do you commit staged changes with a message?
  • How do you open an editor to write a longer commit message?
  • How do you check the current state of your repository?
  • How do you view a concise list of commits?
  • How do you discard changes in the working directory for a specific file?
  • How do you modify the last commit message or add more changes to it?

Leave a Reply