What is Git?
Git is a distributed version‑control system that records changes to files and coordinates work among multiple developers.
Why Use Git?
- Enables concurrent development without overwriting each other’s work.
- Provides a complete history of every change for audit and rollback.
- Facilitates branching, merging, and collaboration across teams.
How to Create a Local Repository
Follow these steps on your workstation.
- Open a terminal (Git Bash on Windows).
- Navigate to the project folder:
cd /path/to/project. - Initialize Git:
git init. - Stage files:
git add .. - Verify staged files:
git status. - Commit with a descriptive message:
git commit -m "Add initial project files".
How to Connect to a Remote Repository (GitHub)
After creating a repository on GitHub, link it to your local repo.
- Copy the HTTPS URL of the remote repository.
- Add it as a remote named
origin:git remote add origin.
How to Push Changes to GitHub
Transfer local commits to the remote.
- Set the upstream branch (first push only):
git push -u origin main. - Subsequent pushes:
git push.
How to Pull Updates from GitHub
Synchronize your local branch with the remote.
- Fetch and merge in one step:
git pull. - Or fetch first then merge:
git fetch originfollowed bygit merge origin/main.
How to Track File Changes
Use Git commands to view history and specific modifications.
- Check repository status:
git status. - Show commit history for a file:
git log -- path/to/file. - Inspect a particular commit:
git show commit-hash.
Best Practices for Commit Messages
Clear messages improve collaboration.
- Use the imperative mood (e.g., “Add login feature”).
- Limit the subject line to ~50 characters.
- Provide a longer description only when necessary.