Skip to content

Latest commit

 

History

History
160 lines (119 loc) · 3.78 KB

3. Staging and Committing.md

File metadata and controls

160 lines (119 loc) · 3.78 KB

Staging and Committing

Staging and committing are essential steps in Git's version control process. These actions allow you to prepare changes and save snapshots of your project's state.


1. What is Staging?

The staging area is an intermediate space where you prepare changes before committing them to the repository. It allows you to:

  • Review changes.
  • Group related modifications into a single commit.

Staging Process

  1. Modify files in your working directory.
  2. Use git add to stage the changes.
  3. Commit the staged changes to save them in the repository.

2. What is Committing?

A commit represents a snapshot of your project at a specific point in time. It includes:

  • All staged changes.
  • A commit message describing the changes.

3. Staging Changes

Stage a Specific File

To stage a single file:

git add filename

Stage All Changes

To stage all modified and new files:

git add .

Stage Files by Pattern

To stage files matching a pattern:

git add *.txt

Unstage Files

To remove a file from the staging area without discarding changes:

git reset filename

4. Committing Changes

Basic Commit

To commit all staged changes:

git commit -m "Your commit message"

Write Detailed Commit Messages

For multiline messages, use:

git commit

This opens your default text editor for writing a detailed description.

Amend the Last Commit

To modify the most recent commit (e.g., update its message or add more changes):

git commit --amend

5. Viewing Commit History

To view the history of commits:

git log

Additional Options

  • View a summary of commits:
    git log --oneline
  • View changes made in each commit:
    git log -p

6. Practical Example

Step 1: Modify a File

Create or modify a file:

echo "Git is awesome!" > example.txt

Step 2: Check Status

Check the current state of your repository:

git status

Step 3: Stage the File

Add the file to the staging area:

git add example.txt

Step 4: Commit the Changes

Commit the file with a message:

git commit -m "Add example.txt with a message"

Step 5: Verify the Commit

View the commit history:

git log --oneline

7. Best Practices for Committing

  • Write Clear Commit Messages: Describe what and why changes were made.
  • Commit Often: Make small, incremental commits to simplify debugging and history tracking.
  • Group Related Changes: Stage and commit logically related changes together.

Common Commands

Command Description
git add filename Stage a specific file.
git add . Stage all changes.
git commit -m "message" Commit staged changes with a message.
git reset filename Unstage a file while keeping changes.
git commit --amend Modify the last commit.
git log View commit history.
git log --oneline View a concise summary of commit history.

Conclusion

Staging and committing are at the heart of Git workflows. By mastering these steps, you can effectively organize your project changes and maintain a clean, descriptive history.


Next Steps: Branch Basics