Git and GitHub – Workflow – How Git work (Part-5)

Posted by

Understanding the Git Workflow:

The Git workflow typically involves three main areas: Workspace, Staging Area (Stage), and Repository (Repo). The diagram you provided illustrates these areas and their roles in the Git process.

1. Workspace

  • Description: This is your working directory where you make changes to your files. This is where you write and edit your code.
  • Location: In the diagram, it is shown as C:\sat\repo1.

Example:

  • You have a file main.py in C:\sat\repo1 that you are editing.
C:\sat\repo1
└── main.py

2. Staging Area (Stage)

  • Description: This is where you place files you want to include in your next commit. Files in the staging area are marked to be committed.
  • Location: In Git terminology, this area is sometimes referred to as the “index”.

Command:

  • To add a file to the staging area, you use the git add command.

Example:

git add main.py

3. Repository (Repo)

  • Description: This is where Git permanently stores your changes as different versions of the project. Each commit you make is stored here.
  • Location: The repository is represented by the .git directory inside your project folder (e.g., C:\sat\repo1\.git).

Command:

  • To commit changes from the staging area to the repository, you use the git commit command.

Example:

git commit -m "Initial commit"

Workflow Steps:

Step 1: Edit Files in Workspace

  • You start by creating or editing files in your workspace.
  • Example: You create or modify main.py in C:\sat\repo1.

Step 2: Add Files to Staging Area

  • Once you’re happy with your changes, you add the files to the staging area.
  • Example Command: git add main.pyThis tells Git to include main.py in the next commit.

Step 3: Commit Files to Repository

  • After staging your files, you commit them to the repository, creating a snapshot of your project at that point in time.
  • Example Command: git commit -m "Add main.py with initial code"This command records the changes in the repository along with a message describing the changes.

Summary:

  1. Workspace (C:\sat\repo1): Where you make changes to files.
  2. Staging Area (Stage): Where you place files to be included in the next commit using git add.
  3. Repository (Repo) (C:\sat\repo1\.git): Where Git stores committed snapshots of your project.

Live Practical:

Step-by-Step Git Workflow

Initial Setup

Create a Repository:

    mkdir my_project
    cd my_project
    git init
    

    Create Files:

    echo "Initial content of file1" > file1.java
    echo "Initial content of file2" > file2.java
    

    Check the Status:

    git status
    
    Output:
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            file1.java
            file2.java
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    • Explanation: Both file1.java and file2.java are untracked and not yet added to Git.

    Add file1.java and Commit

    Add file1.java to Staging Area:

    git add file1.java
    

      Check the Status Again:

      git status
      
      Output:
      On branch master
      
      No commits yet
      
      Changes to be committed:
        (use "git rm --cached <file>..." to unstage)
              new file:   file1.java
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              file2.java
      
      • Explanation: file1.java is now in the staging area, ready to be committed. file2.java is still untracked.

      Commit file1.java:

      git commit -m "Add file1.java"
      

      Check the Status After Commit:

      git status
      
      Output:
      On branch master
      nothing to commit, working tree clean
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              file2.java
      • Explanation: file1.java is committed to the repository. file2.java is still untracked.

      Add file2.java to Staging Area

      Add file2.java to Staging Area:

      git add file2.java
      
      git status
      
      On branch master
      
      Changes to be committed:
        (use "git rm --cached <file>..." to unstage)
              new file:   file2.java
      
        • Explanation: file2.java is now in the staging area, ready to be committed.

        Modify file1.java and Stage Changes

        Modify file1.java:

        echo "Modified content of file1" >> file1.java
        

          Check the Status:

          git status
          
          Output:
          On branch master
          
          Changes to be committed:
           (use "git rm --cached <file>..." to unstage)
                 new file:   file2.java
          
          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:   file1.java

          Add file1.java to Staging Area:

          Commit Only file1.java

          Commit Only file1.java:

          git commit -m "Update file1.java" file1.java
          

          Check the Status Again:

          git status
          
          On branch master
          
          Changes to be committed:
            (use "git rm --cached <file>..." to unstage)
                  new file:   file2.java
          
            • Explanation: file1.java is committed, and file2.java remains in the staging area.

            Commit All Changes

            Commit All Changes:

            git commit -m "Add file2.java"
            

            Check the Status Again:

            On branch master
            nothing to commit, working tree clean
            
              • Explanation: Both file1.java and file2.java are now committed, and the working directory is clean.

              Summary

              • git status: Checks the current state of the working directory and staging area.
              • git add <file>: Stages changes for a specific file.
              • git commit -m "<message>" <file>: Commits only the specified file.
              • git commit -m "<message>": Commits all staged changes.
              guest
              0 Comments
              Inline Feedbacks
              View all comments
              0
              Would love your thoughts, please comment.x
              ()
              x