❮ Back to Journal

Git Basics for Beginners: A Practical Guide to Version Control

Introduction

If you’re new to programming, you’ve probably heard that you should be using Git, but might not understand why or how to get started. Git is a version control system that helps you track changes to your code, collaborate with others, and maintain different versions of your project. In this guide, I’ll walk you through the essential Git concepts and commands with practical examples that you can start using right away.

Why Use Git?

Before diving into commands, let’s understand why Git is so important:

  1. Track changes: Git keeps a history of all changes to your code, so you can see what changed, when, and by whom.
  2. Experiment safely: Create branches to try new ideas without affecting your main code.
  3. Collaborate: Work with others on the same project without overwriting each other’s changes.
  4. Backup: Your code is stored both locally and remotely (if using GitHub, GitLab, etc.).
  5. Revert mistakes: Made a mistake? Git makes it easy to go back to a previous working state.

Getting Started with Git

Installing Git

First, you need to install Git on your computer:

For Windows: Download and install from git-scm.com

For macOS:

# Using Homebrew
brew install git

# Or using the installer from git-scm.com

For Linux:

# Ubuntu/Debian
sudo apt-get install git

# Fedora
sudo dnf install git

Configuring Git

After installation, set up your identity:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

This information will be included with every commit you make.

Creating Your First Git Repository

A repository (or “repo”) is a project tracked by Git. Let’s create one:

# Create a new directory for your project
mkdir my-project
cd my-project

# Initialize a new Git repository
git init

You should see a message like “Initialized empty Git repository in /path/to/my-project/.git/“.

The Basic Git Workflow

The typical Git workflow involves these steps:

  1. Make changes to your files
  2. Stage the changes you want to commit
  3. Commit the staged changes with a message
  4. Push the commits to a remote repository (optional)

Let’s go through each step:

1. Making Changes

Create a file in your project:

echo "# My Project" > README.md

2. Checking Status

To see what files have been changed:

git status

You should see README.md listed as an untracked file.

3. Staging Changes

Before committing, you need to stage the files you want to include:

git add README.md

# To add all changed files
git add .

4. Committing Changes

Now commit the staged changes with a descriptive message:

git commit -m "Add README file"

A commit is like a snapshot of your project at a specific point in time.

5. Viewing Commit History

To see your commit history:

git log

This shows all commits with their messages, authors, and timestamps.

Working with Remote Repositories

To collaborate with others or back up your code, you’ll want to use a remote repository like GitHub, GitLab, or Bitbucket.

Creating a Remote Repository

  1. Sign up for a GitHub account at github.com
  2. Click the ”+” icon and select “New repository”
  3. Name your repository and click “Create repository”
  4. Follow the instructions to push your existing repository

Connecting to a Remote Repository

git remote add origin https://github.com/yourusername/your-repo-name.git

Pushing to a Remote Repository

git push -u origin main

This pushes your commits to the remote repository named “origin” on the “main” branch. The -u flag sets up tracking, so in the future you can simply use git push.

Cloning an Existing Repository

To download a copy of an existing repository:

git clone https://github.com/username/repository.git

This creates a new directory with the repository name, downloads all the code, and sets up the remote connection automatically.

Branching and Merging

Branches allow you to work on different features or fixes in isolation.

Creating a Branch

# Create a new branch
git branch feature-login

# Switch to the new branch
git checkout feature-login

# Or do both in one command
git checkout -b feature-login

Working with Branches

Make changes, stage, and commit as usual:

# Make some changes
echo "Login functionality" > login.js

# Stage and commit
git add login.js
git commit -m "Add login functionality"

Switching Between Branches

# Switch back to the main branch
git checkout main

# Switch to the feature branch again
git checkout feature-login

Merging Branches

When your feature is complete, you can merge it back to the main branch:

# First, switch to the branch you want to merge into
git checkout main

# Then merge the feature branch
git merge feature-login

Deleting a Branch

After merging, you can delete the feature branch:

git branch -d feature-login

Handling Merge Conflicts

Sometimes Git can’t automatically merge changes because the same lines were modified in different ways. This is called a merge conflict.

Example of a Merge Conflict

Let’s say you and a teammate both modified the same line in README.md:

<<<<<<< HEAD
# My Awesome Project
=======
# My Cool Project
>>>>>>> feature-branch

The part between <<<<<<< HEAD and ======= is your current branch’s version. The part between ======= and >>>>>>> feature-branch is the incoming branch’s version.

Resolving Merge Conflicts

  1. Open the file with conflicts in your editor
  2. Decide which version to keep, or create a combination
  3. Remove the conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Save the file
  5. Stage and commit the resolved file
# After resolving the conflict
git add README.md
git commit -m "Resolve merge conflict in README"

Common Git Problems and Solutions

Problem 1: Accidentally Committed to the Wrong Branch

# Undo the last commit but keep the changes
git reset HEAD~1

# Switch to the correct branch
git checkout correct-branch

# Stage and commit the changes
git add .
git commit -m "Your commit message"

Problem 2: Need to Undo a Commit

# Undo the last commit but keep the changes staged
git reset --soft HEAD~1

# Undo the last commit and unstage the changes
git reset HEAD~1

# Undo the last commit and discard the changes (be careful!)
git reset --hard HEAD~1

Problem 3: Want to Temporarily Save Changes Without Committing

# Save changes to the stash
git stash

# Do something else, like switch branches
git checkout another-branch

# Apply the stashed changes later
git stash apply

Problem 4: Accidentally Added a File You Don’t Want to Commit

# Unstage a specific file
git reset HEAD filename

# Unstage all files
git reset HEAD

Problem 5: Need to Change the Last Commit Message

git commit --amend -m "New commit message"

Best Practices for Git

  1. Commit often: Make small, focused commits rather than large ones
  2. Write clear commit messages: Describe what and why, not how
  3. Create a .gitignore file: Exclude files like build artifacts and dependencies
  4. Use branches for features/fixes: Keep your main branch stable
  5. Pull before pushing: Always get the latest changes before pushing yours
  6. Review changes before committing: Use git diff to see what you’re about to commit

Creating a .gitignore File

A .gitignore file tells Git which files to ignore:

# Create a .gitignore file
touch .gitignore

Add common patterns to ignore:

# Node.js
node_modules/
npm-debug.log

# Build directories
dist/
build/

# Environment variables
.env
.env.local

# Editor files
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

Git Workflows for Beginners

Solo Developer Workflow

  1. Create a repository
  2. Make changes and commit regularly
  3. Push to a remote repository for backup
  4. Use branches for experimental features

Team Workflow (GitHub Flow)

  1. Create a branch for a feature/fix
  2. Make changes and commit
  3. Push the branch to GitHub
  4. Create a Pull Request (PR)
  5. Get code review and make changes if needed
  6. Merge the PR when approved

Conclusion

Git might seem complex at first, but starting with these basic commands will help you build a solid foundation. As you get more comfortable, you can explore more advanced features.

Remember these key points:

  • git init creates a new repository
  • git add stages changes
  • git commit saves staged changes
  • git push uploads commits to a remote repository
  • git pull downloads changes from a remote repository
  • Branches let you work on features in isolation
  • Commit often with clear messages

With these Git basics, you’re well on your way to better code management and collaboration.

Further Resources