In this section, we will cover the essential concepts and commands for staging and committing changes in Git. These operations are fundamental to version control and will form the basis of your daily workflow with Git.

Key Concepts

  1. Working Directory: The directory where your project files are located.
  2. Staging Area (Index): A temporary area where changes are gathered before committing.
  3. Commit: A snapshot of the staged changes, saved in the repository's history.

Staging Changes

What is Staging?

Staging is the process of adding changes to the staging area. This allows you to prepare a set of changes before committing them. You can stage changes selectively, which is useful for organizing your commits logically.

Commands for Staging

  • git add <file>: Stages a specific file.
  • git add .: Stages all changes in the current directory and subdirectories.
  • git add -p: Interactively stages changes, allowing you to review each change before staging.

Example

Let's say you have modified two files, file1.txt and file2.txt.

$ git status
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:   file1.txt
	modified:   file2.txt

To stage file1.txt:

$ git add file1.txt
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file1.txt

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:   file2.txt

Committing Changes

What is a Commit?

A commit is a snapshot of the changes in the staging area. Each commit has a unique identifier (SHA-1 hash) and includes a commit message that describes the changes.

Commands for Committing

  • git commit -m "commit message": Commits the staged changes with a message.
  • git commit: Opens the default text editor to write a commit message.

Example

After staging file1.txt, you can commit the changes:

$ git commit -m "Update file1.txt with new content"
[main 1a2b3c4] Update file1.txt with new content
 1 file changed, 1 insertion(+), 1 deletion(-)

Practical Exercise

Exercise 1: Staging and Committing

  1. Create a new file named example.txt and add some content to it.
  2. Stage the file using git add.
  3. Commit the staged changes with a meaningful commit message.

Solution

  1. Create and edit the file:

    $ echo "Hello, Git!" > example.txt
    
  2. Stage the file:

    $ git add example.txt
    
  3. Commit the changes:

    $ git commit -m "Add example.txt with initial content"
    

Exercise 2: Selective Staging

  1. Modify two files in your repository.
  2. Stage only one of the files.
  3. Commit the staged changes.
  4. Verify that the other file is not included in the commit.

Solution

  1. Modify file1.txt and file2.txt.

    $ echo "Change in file1" >> file1.txt
    $ echo "Change in file2" >> file2.txt
    
  2. Stage only file1.txt:

    $ git add file1.txt
    
  3. Commit the changes:

    $ git commit -m "Update file1.txt"
    
  4. Verify the status:

    $ git status
    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:   file2.txt
    

Common Mistakes and Tips

  • Forgetting to Stage Changes: Ensure you stage all the changes you want to include in the commit.
  • Unclear Commit Messages: Write clear and descriptive commit messages to make the history easier to understand.
  • Staging Unintended Changes: Use git status to review what is staged before committing.

Conclusion

Staging and committing changes are fundamental operations in Git. By mastering these commands, you can effectively manage your project's history and collaborate with others. In the next section, we will explore how to view the commit history to understand the changes made over time.

© Copyright 2024. All rights reserved