Git Tutorial: A Step-by-Step Guide for Beginners
Git is the backbone of modern version control, essential for collaborative and remote software development workflows. This guide is designed as a crash course to help beginners master the basics of Git, including repository initialization, adding and committing changes, pushing to GitHub, and using token-based authentication.
Prerequisites
Before diving into Git, ensure you have the following:
prerequisites
Steps to Initialize a Repository
Follow these steps to start a version-controlled project using Git:
steps
- Open a terminal and navigate to your project directory:bash
cd /path/to/project - Initialize a Git repository:bash
git init - Verify the initialization by checking for Git’s hidden
.gitfolder:bashls -a # Lists hidden files
How to Stage Changes
After editing or creating files in your project, stage them for tracking:
steps
- Use the following to stage all changes:bash
git add . - Alternatively, stage specific files:bash
git add filename - Verify staged changes:bash
git status
Committing Changes
Save staged changes to your repository with descriptive messages:
steps
- Commit staged changes with a message:bash
git commit -m "Initial commit with basic setup" - Confirm the commit history:bash
git log --oneline
Token-Based Authentication with GitHub
GitHub now requires Personal Access Tokens (PATs) for HTTPS authentication instead of passwords.
Common Mistakes in Git Workflow
Avoid these pitfalls for smoother Git usage:
Verification
Ensure Git actions are successful:
FAQ
FAQ
How do I delete a local Git repository?
Run rm -rf .git inside the repository folder to remove the Git tracking entirely.
How do I resolve merge conflicts?
Open the conflicted file, review conflicting sections marked by Git, edit to resolve, then stage and commit the changes.
Why is Git asking for my username and password during git push?
If HTTP authentication is enabled, Git will prompt for credentials. Generate and replace your password with a Personal Access Token.
What does git clone do?
git clone copies a remote repository (e.g., from GitHub) to your local machine, allowing you to work offline.