Coding on a computer screen

Git tutorial: Version control from the ground up

Git is an essential tool. In this guide, we’ll look at what Git is, why it matters, and how to get started using it. By the end, you’ll have a solid understanding of Git basics and an established workflow you can apply to any project.

Introduction

Whether you’re a software developer, a technical writer, or just collaborating on projects that involve text files, Git is an essential tool. In this guide, we’ll look at what Git is, why it matters, and how to get started using it. By the end, you’ll have a solid understanding of Git basics and an established workflow you can apply to any project.

The guide starts from the very beginning, what’s Git, how to install and configure it, and then it goes through all the Git core concepts, commands and usage.

What is Git?

Git is a distributed version control system (VCS) that tracks changes in files (usually source code) and helps coordinate work on those files among multiple people. It lets you:

  • Keep a full history of every change made to your project.
  • Collaborate seamlessly on code or text files.
  • Experiment with different approaches (in branches) without interfering with the main version of your project.

Why Use Git?

  • Collaboration: Multiple people can work simultaneously on the same project, merging their contributions as needed.
  • Revisions & History: You can roll back to any previous state in your project if something goes wrong.
  • Branching & Experimentation: You can try out ideas in “branches” without risking the stability of your main project.
  • Distributed Nature: Every developer has a full local copy of the repository, including its entire history.

Installing and configuring Git

Installation

Windows

  1. Download the Git for Windows installer from the Git download site.
  2. Run the installer and follow the prompts (the defaults are fine for most users).
  3. Open “Git Bash,” which is installed by default and provides a Linux-style terminal.

macOS

  1. Download the official macOS Git installer and follow the instructions.
  2. Otherwise, if you use Homebrew, do:
brew install git

Verify installation by running:

git --version

Linux

Use your distro’s fave package manager.

# Debian/Ubuntu:
sudo apt-get update
sudo apt-get install git

# Fedora:
sudo dnf install git

# Arch:
sudo pacman -S git


Initial Configuration

Once installed, you’ll want to configure your identity and a few helpful settings. Note: it’s very important that you do this, otherwise you’ll not be able to commit your code and perform a lot of other equally important operations with Git.

Set User Details

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

These commands tell Git who you are. The --global flag makes them universal across your system.

Enable Helpful Settings

git config --global color.ui auto

Enables color-coded Git output in the terminal.


Core Git Concepts

Repository (Repo)

A Git repository is a database that tracks file changes. It lives in a hidden folder named .git at your project’s root.

Working Directory / Working Tree

This is the actual folder you see with your files. When you edit a file, you’re modifying the working directory.

Staging Area (Index)

The staging area (index) is where you place changes you want to include in your next commit.

Commit

A commit is a snapshot of changes in your repository. It includes the changes you staged plus a message describing them.

Remote Repository

A remote is a copy of your repository hosted on a server (GitHub, GitLab, or any self-hosted solution). You push commits to and pull commits from the remote.


Setting Up a New Project

Create a Local Repository

mkdir my_project
cd my_project
git init
  • git init creates a .git folder and makes this directory a repo.

Add a Remote Repository

  1. Create a new empty repository on GitHub/GitLab.
  2. Add it as a remote:
git remote add origin https://github.com/username/my_project.git

Push your local commits to the remote:

git push -u origin master

The -u sets origin master as the default remote tracking branch.


Essential Git Commands

git status

git status

Shows which files are modified, untracked, or staged.

git add

git add file1.txt
git add .

Use git add . to stage all changes.

git commit

git commit -m "Add initial project setup"

Commits everything in the staging area with a message.

git push

git push origin master

Sends local commits to the remote repository named “origin,” on the branch “master.”

git pull

git pull origin master

Brings down remote changes and merges them into your local branch.

git clone

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

Makes a local copy of an existing remote repository.


Branching and Merging

Git’s branching system lets you develop features independently of the main codebase.

git branch

git branch new-feature

Creates a new branch called new-feature.

git checkout / git switch

Either command updates your working directory to reflect the new branch.

Older Git versions (before 2.23) use git checkout to switch branches:

git checkout new-feature

Newer Git versions introduce git switch:

git switch new-feature

git merge

  1. Switch back to your main branch:
git checkout master

Merge the new-feature branch into master:

git merge new-feature

Optionally delete the feature branch:

git branch -d new-feature

Merge Conflicts

Sometimes Git can’t automatically merge changes to the same parts of a file. You’ll see conflict markers in the file(s) that need to be manually resolved:

<<<<<<< HEAD
Your changes here
=======
Changes from new-feature
>>>>>>> new-feature

After editing the file to resolve conflicts, do:

git add .
git commit

The merge is then complete.


Viewing History and Changes

git log

git log

Shows the commit history. Press q to quit. For a concise view:

git log --oneline --graph --decorate

git diff

Displays differences between your working directory and the latest commit:

# Compare working directory changes to last commit
git diff

# Compare staging area changes to last commit
git diff --staged

Example project

Now that you have a basic grasp of Git, you can put your knowledge to the test by following an example project: From the very start of the repo → Make changes → Stage → Commit → (Optionally) Branch → Merge → Push.

You can access the example following this link: Learning Git from the start with an example project.


Advanced Topics

You can check our guide of Advanced Git Topics, these include: Stashing changes with git stash, Tagging releases with git tag, Cherry-picking with git cherry-pick, Undoing commits with git revert and git reset, following this link: Advanced Git topics and commands.


Best Practices & Tips

  1. Commit often with clear messages.
  2. Use branches for each feature or bug fix.
  3. Pull frequently to stay updated with remote changes.
  4. Resolve conflicts as soon as they arise.
  5. Don’t commit secrets (API keys, passwords). Use environment variables or a vault.
  6. Review git diff before committing to confirm your changes.

In conclusion…

Git can seem intimidating, but with practice it becomes second nature. Its ability to track changes, foster collaboration, and manage multiple developmetn paths is invaluable. By following this guide, you now know how to:

  1. Initialize a repository and clone a remote one.
  2. Track and commit file changes.
  3. Push and pull from a remote.
  4. Branch, merge, and handle conflicts.
  5. Use advanced commands like stash, tags, cherry-pick, and revert.

As you grow more comfortable, explore advanced concepts like submodules and continuous integration. Until then, keep practicing the essentials and enjoy the benefits of version control.

Happy Coding!

Feel free to use this tutorial as a springboard to help someone get started with Git. If you get stuck, refer back to these notes or check Git’s excellent official documentation at git-scm.com. Good luck, and have fun version-controlling your projects!

Leave a Reply

Your email address will not be published. Required fields are marked *