DEV Community

Cover image for Git for Beginners
Swadesh Chatterjee
Swadesh Chatterjee

Posted on

Git for Beginners

What Is Git?

In simple terms, Git is a time machine for your files. It is a tool that records changes to your code over time, so you can recall specific versions later.

Why Git Is Used

  1. Code History & Rollback: See exactly what changed, when, and why. Made a breaking change? Revert to yesterday’s working version instantly.
  2. Team Collaboration: Multiple people can work on the same project simultaneously without overwriting each other’s work. Git helps merge changes together cleanly.
  3. Experimentation Without Fear: Want to try a risky new feature? Create a separate "branch" to test it. If it doesn’t work, just delete it—your main project stays safe.
  4. Industry Standard: Git is the foundation for platforms like GitHub, GitLab, and Bitbucket. Knowing Git is a non-negotiable skill for modern developers.

Core Git Concepts

To understand Git, you need to know its basic building blocks:

  • Repository: The project’s main folder, where Git stores all history and configuration. It's the .git folder inside your project.
  • Working Directory: The actual files and folders you see and edit on your computer.
  • Staging Area : A middle ground. You add changes here to prepare them for a permanent snapshot.
  • Commit: A permanent snapshot of your project at a specific point in time, with a message describing what you did.
  • Branch: A parallel timeline of commits. The default branch is usually called main or master.
  • HEAD: A pointer that shows what commit or branch you are currently looking at in your working directory. Ima

Essential Git Commands

Start with these fundamental commands:

  • git init: Turns your current folder or directory into a new Git repository.
  • git status: Shows the state of your working directory and staging area (what’s changed, what’s staged).
  • git add <filename>: Moves specific file changes from your working directory to the staging area. Use git add . to stage all changes.
  • git commit -m "Your message": Takes everything in the staging area and saves it as a new commit in the repository. Write clear, concise commit messages!
  • git log: Shows the history of commits.
  • git diff: Shows the exact differences (lines added/removed) in files you haven’t staged yet.
  • git branch: Lists all branches. Creates a new one if you give it a name (e.g., git branch new-feature).
  • git checkout <branch-name> / git switch <branch-name>: Switches your working directory to a different branch.

A Simple Developer Workflow

Let's walk through a typical solo session:

1. Start a new project with Git
mkdir <project-name>
cd <project-name>
git init

2. Create a file, make some changes
echo "# My App" > README.md

3. Check what Git sees
git status # README.md will be shown as "untracked"

4. Stage the new file
git add README.md

5. Commit the change
git commit -m "Add project README file"

6. Make more changes to the README file
echo "This is a simple guide." >> README.md

7. See what changed
git diff

8. Stage and commit the update
git add README.md
git commit -m "Add description to README"

View your commit history
git log
Enter fullscreen mode Exit fullscreen mode

GitImage

Top comments (0)