Git Introduction
What is Git?
Git is a distributed version control system that lets developers track changes in their codebase, collaborate efficiently, and roll back if necessary.
Core Git Concepts
- Repository: A dedicated folder where Git manages your project's files along with its complete history.
- Clone: Creating a local copy of a remote repository on your machine to work with.
- Stage: Selecting specific changes you want Git to prepare for committing, acting as a preparation step.
- Commit: Recording a snapshot of your staged changes into the repository for historical tracking.
- Branch: Diverging from the main codebase to work on different features or versions independently.
- Merge: Combining changes from separate branches back into a single, unified branch.
- Pull: Fetching and integrating updates from a remote repository to keep your local version current.
- Push: Sending your committed changes from your local machine to a remote repository for sharing and backup.
Working with Git
- Start by initializing Git in any folder, which turns it into a repository. Git creates a hidden folder to manage all change history.
- When you modify, add, or delete files, they are labeled as "modified" in Git.
- Next, you need to select these changed files for the next snapshot—this is called "staging."
- Staged files are then committed by Git, capturing a permanent version of the current state.
- Git keeps a detailed history of every commit, allowing you to review what changes were made and when.
- You can also reverse to any previous commit if needed, restoring your project to an earlier state.
- Instead of copying entire files with each commit, Git tracks incremental changes, making version control efficient.
Basic Git Workflow
- Initialize a repository:
git init
- Track files:
git add filename
orgit add .
- Commit changes:
git commit -m "message"
- View history:
git log
Common Git Commands
git status
– Show current changes and statusgit diff
– Show file differencesgit clone <repo-url>
– Copy a repositorygit push
– Upload local commits to remotegit pull
– Fetch and merge from remote