Mastering Git: Essential Commands - Status, Add, and Commit Explained!
This video provides a comprehensive introduction to essential Git commands used for version control in software projects. It explains the basics of initializing a Git repository, tracking changes, staging files, committing updates, and viewing project history. The focus is on making Git understandable and practical for new users by illustrating common workflows and commands.
1. Setting Up a Git Repository
Creating a Git repository is the first step to start tracking a project's history. By default, any folder on your system is just a normal folder and not yet a Git repository.
- Use the command
git initto initialize a folder as a Git repository. - Running
git initcreates a hidden directory named.gitin the project root; this contains all the metadata and tracking information for the project. - Deleting the
.gitfolder will remove all version history information—use caution. - Once initialized, the folder is ready for Git operations.
2. Checking Repository Status with git status
The git status command is vital for understanding the current state of your working directory and staging area.
- It displays:
- The current branch you are on.
- Which files are tracked, modified, or untracked.
- Staged files waiting to be committed.
- Running
git statuson a non-repository folder yields the message "Not a git repository". - This command is used frequently to monitor changes during development cycles.
3. Managing Project Files: Adding and Committing Changes
Tracking and saving changes involves two key steps: staging files and then committing them.
- Untracked files are new files in your directory not yet added to Git.
- Use
git add <filename>to stage individual files, orgit add .to stage all changes in the current directory. - Staging means preparing files to be included in the next commit.
- Use
git commit -m "<message>"to create a commit with a descriptive message.- If you run
git commitwithout-m, a text editor opens for you to enter the commit message manually.
- If you run
- Commit messages summarize what changes were made at each checkpoint, aiding project history clarity.
4. Understanding Git Snapshots and Checkpoints
Git tracks project changes as snapshots (commits), which can be viewed and revisited.
- Each commit generates a unique commit hash acting as an identifier.
- Commits help save various "checkpoints" — for example, when a feature is added or a bug is fixed.
- You can navigate through commits to return to a previous state if necessary.
- This mechanism is analogous to saving progress points in a game.
5. Viewing Project History with git log
The git log command shows the chronological history of commits.
- Displays each commit’s hash, author name/email, timestamp, and commit message.
- Helps track who made which changes and when.
- Useful to understand development progression and debug issues.
Summary of Key Git Commands
Important Git Concepts Emphasized
- The
.gitfolder is core to any Git repository and should never be deleted unless you want to remove Git tracking. - Staging area is a distinct step before committing, allowing controlled inclusion of changes.
- Commit messages must be meaningful to clearly communicate the purpose of changes.
- Frequent use of
git statushelps maintain awareness and avoid confusion. - Git allows easy navigation between commits (snapshots), supporting efficient version control.
Workflow Summary
- Initialize your project folder with
git init. - Add or modify files as needed.
- Use
git statusto see changes. - Stage changes with
git add. - Commit with
git commit -m "message". - Review commit history using
git log. - Use commits as checkpoints to manage project versions.
This foundational knowledge equips new Git users to confidently manage their coding projects using version control, ensuring efficient tracking, collaboration, and progress management.