·2 min read

Getting Started with Git: A Practical Guide

Understand the basics of Git, common commands, and how to use Git effectively in a development workflow.

Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to work on the same project simultaneously without overwriting each other’s work. Git is fast, efficient, and integral to modern software engineering.

Why Use Git?

  • Track Changes: Keep a history of all code changes.
  • Collaboration: Work with others without conflict.
  • Branching: Experiment in isolated branches before merging.
  • Backup: Store code on platforms like GitHub, GitLab, or Bitbucket.

Key Concepts

  • Repository: A project’s directory tracked by Git.
  • Commit: A snapshot of your files at a point in time.
  • Branch: A parallel version of the repository.
  • Merge: Combine branches together.
  • Clone: Create a local copy of a remote repository.
  • Push/Pull: Send/receive changes between local and remote repositories.

Basic Workflow

  1. Initialize a Git Repository
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> init</span></span>
  1. Clone a Remote Repository
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> clone</span><span style="color:#9ECBFF"> https://github.com/user/repo.git</span></span>
  1. Check the Status of Your Files
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> status</span></span>
  1. Stage Files for Commit
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> add</span><span style="color:#9ECBFF"> filename</span><span style="color:#6A737D">     # Add one file</span></span>
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> add</span><span style="color:#9ECBFF"> .</span><span style="color:#6A737D">            # Add all changes</span></span>
  1. Commit Changes
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> commit</span><span style="color:#79B8FF"> -m</span><span style="color:#9ECBFF"> "Describe what you changed"</span></span>
  1. View Commit History
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> log</span></span>
  1. Push Changes to Remote
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> push</span><span style="color:#9ECBFF"> origin</span><span style="color:#9ECBFF"> main</span></span>
  1. Pull Changes from Remote
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> pull</span><span style="color:#9ECBFF"> origin</span><span style="color:#9ECBFF"> main</span></span>

Working with Branches

  1. Create a New Branch
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> checkout</span><span style="color:#79B8FF"> -b</span><span style="color:#9ECBFF"> feature-branch</span></span>
  1. Switch to a Branch
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> checkout</span><span style="color:#9ECBFF"> main</span></span>
  1. Merge a Branch into Another
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> merge</span><span style="color:#9ECBFF"> feature-branch</span></span>
  1. Delete a Branch
<span class="line"><span style="color:#B392F0">git</span><span style="color:#9ECBFF"> branch</span><span style="color:#79B8FF"> -d</span><span style="color:#9ECBFF"> feature-branch</span></span>

Helpful Tips

  • Commit often with clear messages.
  • Use .gitignore to exclude files like logs or secrets.
  • Avoid committing directly to main – use feature branches.
  • Resolve merge conflicts with care.

Summary

Git is an essential tool for any software developer. By mastering a handful of commands and following best practices, you can collaborate more efficiently and maintain a clean, organized project history.