Git for Beginners: A Complete Guide to Version Control

<h2>What is Git?</h2>

<p>Git is a distributed version control system created by Linus Torvalds in 2005. It tracks changes in source code and enables multiple developers to collaborate efficiently. Today, Git is used by virtually every software development team in the world.</p>

<h2>Installing Git</h2>

<p>Download Git from <strong>git-scm.com</strong> and follow the installer for your OS. After installation, configure your identity:</p>

<pre><code>git config --global user.name "Your Name"

git config --global user.email "you@example.com"</code></pre>

<h2>Core Git Concepts</h2>

<h3>Repository (Repo)</h3>

<p>A repository is a directory tracked by Git. It contains all project files and the complete history of changes.</p>

<h3>Commit</h3>

<p>A commit is a snapshot of your project at a specific point in time. Each commit has a unique ID (hash) and a message describing the change.</p>

<h3>Branch</h3>

<p>A branch is an independent line of development. The default branch is called <code>main</code> (or <code>master</code> in older repos).</p>

<h3>Remote</h3>

<p>A remote is a version of the repository hosted on a server (like GitHub, GitLab, or Bitbucket).</p>

<h2>Essential Git Commands</h2>

<h3>Starting a Project</h3>

<pre><code># Initialize a new repo

git init

# Clone an existing repo

git clone https://github.com/user/repo.git</code></pre>

<h3>Making Changes</h3>

<pre><code># Check status

git status

# Stage a specific file

git add filename.js

# Stage all changes

git add .

# Commit with a message

git commit -m "Add user authentication feature"</code></pre>

<h3>Working with Branches</h3>

<pre><code># List all branches

git branch

# Create a new branch

git branch feature/login

# Switch to a branch

git checkout feature/login

# Create and switch in one command

git checkout -b feature/login

# Merge a branch into current branch

git merge feature/login

# Delete a branch

git branch -d feature/login</code></pre>

<h3>Syncing with Remote</h3>

<pre><code># Add a remote

git remote add origin https://github.com/user/repo.git

# Push to remote

git push origin main

# Pull latest changes

git pull origin main

# Fetch without merging

git fetch origin</code></pre>

<h2>Understanding the Git Workflow</h2>

<ol>

<li><strong>Create a branch</strong> for your feature or fix</li>

<li><strong>Make changes</strong> and stage them with <code>git add</code></li>

<li><strong>Commit</strong> with a descriptive message</li>

<li><strong>Push</strong> the branch to the remote</li>

<li><strong>Open a Pull Request</strong> for code review</li>

<li><strong>Merge</strong> after approval</li>

</ol>

<h2>Writing Good Commit Messages</h2>

<p>Good commit messages make your history readable. Follow these conventions:</p>

<ul>

<li>Use the imperative mood: "Add feature" not "Added feature"</li>

<li>Keep the first line under 72 characters</li>

<li>Reference issue numbers when applicable: "Fix login bug (#123)"</li>

<li>Use conventional commits: <code>feat:</code>, <code>fix:</code>, <code>docs:</code>, <code>refactor:</code></li>

</ul>

<h2>Common Mistakes and How to Fix Them</h2>

<pre><code># Undo last commit (keep changes)

git reset --soft HEAD~1

# Discard changes to a file

git checkout -- filename.js

# View commit history

git log --oneline

# Temporarily save work

git stash

git stash pop</code></pre>

<h2>Next Steps</h2>

<p>Once you're comfortable with basics, explore: Git rebase, cherry-pick, interactive staging (<code>git add -p</code>), and Git hooks for automating workflows.</p>

<h2>Conclusion</h2>

<p>Git is a non-negotiable skill for every developer. Practice these commands daily, and version control will become second nature. Start by using Git on a personal project, then collaborate with others on GitHub or GitLab to experience the full power of distributed version control.</p>