DEV Community

Cover image for What Is Git? Explained Simply for Beginners
Lavkush Kumar
Lavkush Kumar

Posted on Originally published at codingcush.com

What Is Git? Explained Simply for Beginners

Originally published on CodingCush.

Git is a free tool that saves every version of your code. It is called a version control system, and it runs on your own computer. In this guide you will learn what Git is, how repositories, commits, branches and remotes work, and how to make your first commit, branch and clone. Every command was tested on Git 2.43.0.

What Is Git in One Minute?

Think of a video game save: you save before a boss fight, and if it goes badly, you load the save and try again. Git gives your code the same safety net: save a snapshot at each good point, and return to the last good one when a change breaks everything. The official Git website calls Git "a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency". Your computer holds the full history, so you can undo mistakes offline.

Why Developers Bother with Git

Without Git, version control means a folder of files named final, final2, final_FINAL and final_really_this_one. Nobody knows which copy is real, and two people editing at once means lost work. Git records who changed what and when, so a bug can be traced to the exact change that caused it. It lets you try a risky idea on a branch and throw it away if it fails, and it merges several people's work without anyone overwriting anyone else. That is why almost every professional team uses Git.

Four Ideas That Explain Almost Everything

Repository. A repository, or repo, is a project folder that Git watches. Turning a folder into a repo adds a hidden .git folder holding the whole history.

Commit. A commit is one saved snapshot, with a short message saying what changed and why, like "Add login form". A project is a chain of commits you can jump back through.

Branch. A branch is a separate line of work starting from a commit. The main line is usually called main. Build a feature on a branch, then merge it back.

Remote. A remote is a copy of your repo somewhere else, usually on a server like GitHub. Push commits to it to share or back them up, and pull to get other people's work. Git works without a remote; it only adds sharing and backup.

Your First Git Project, Step by Step

Make a folder called my-first-project, open a terminal inside it, and check that Git is installed:

git --version
Enter fullscreen mode Exit fullscreen mode

Output:

git version 2.43.0
Enter fullscreen mode Exit fullscreen mode

A version number means you are ready. Now turn your folder into a repo:

git init -b main
Enter fullscreen mode Exit fullscreen mode

Output:

Initialized empty Git repository in /tmp/my-first-project/.git/
Enter fullscreen mode Exit fullscreen mode

This creates the hidden .git folder and names your main branch main. Your path will look different. Now tell Git who you are:

git config user.name "Lavkush"
git config user.email "lavkush@example.com"
Enter fullscreen mode Exit fullscreen mode

These apply to this project only. Create a file and check what Git sees:

echo "Hello, Git!" > hello.txt
git status
Enter fullscreen mode Exit fullscreen mode

Output:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    hello.txt

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

Git calls hello.txt untracked: visible, but not watched yet. Start watching it with git add:

git add hello.txt
git status
Enter fullscreen mode Exit fullscreen mode

Output:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   hello.txt
Enter fullscreen mode Exit fullscreen mode

Staging picks which changes go in the next snapshot. Now save it with a commit:

git commit -m "Add hello.txt"
Enter fullscreen mode Exit fullscreen mode

Output:

[main (root-commit) a53f9d4] Add hello.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
Enter fullscreen mode Exit fullscreen mode

The -m flag supplies the message, and a53f9d4 is your first commit's short ID. Now check the state of things, and your history:

git status
git log --oneline
Enter fullscreen mode Exit fullscreen mode

Output:

On branch main
nothing to commit, working tree clean
a53f9d4 Add hello.txt
Enter fullscreen mode Exit fullscreen mode

"Working tree clean" means every change is saved. Keep our Git commands cheat sheet for beginners beside you while you practise.

Screenshot of the git-scm.com homepage, the official Git website, showing the definition of Git and links to downloads and the Pro Git book

The git-scm.com homepage, the official Git website.

Branches: Try Things Without Breaking Anything

Want to add a greeting to hello.txt without risking your main code? Make a branch and run the experiment there. Create a branch called feature-greeting:

git branch feature-greeting
git branch
Enter fullscreen mode Exit fullscreen mode

Output:

  feature-greeting
* main
Enter fullscreen mode Exit fullscreen mode

The star marks where you stand, still main. Move onto the new branch and commit a change there:

git switch feature-greeting
echo "Hello from the feature branch!" >> hello.txt
git add hello.txt
git commit -m "Add greeting from the feature branch"
Enter fullscreen mode Exit fullscreen mode

Output:

Switched to branch 'feature-greeting'
[feature-greeting f4816b2] Add greeting from the feature branch
 1 file changed, 1 insertion(+)
Enter fullscreen mode Exit fullscreen mode

The double >> appends a line instead of replacing the file. Now look at the history:

git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

Output:

* f4816b2 Add greeting from the feature branch
* a53f9d4 Add hello.txt
Enter fullscreen mode Exit fullscreen mode

Switch back to main and check the file:

git switch main
cat hello.txt
Enter fullscreen mode Exit fullscreen mode

Output:

Switched to branch 'main'
Hello, Git!
Enter fullscreen mode Exit fullscreen mode

On main, the file still has only its original line: the branch kept the experiment separate. Our guide to Git branches explained with simple examples covers creating, switching and merging in more detail.

Seeing What Changed with git diff

Before you commit, check what is different. Add a line on main without committing it, then ask Git:

echo "Hello, Git! This line is new." >> hello.txt
git diff
Enter fullscreen mode Exit fullscreen mode

Output:

diff --git a/hello.txt b/hello.txt
index 670a245..dbc72cc 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 Hello, Git!
+Hello, Git! This line is new.
Enter fullscreen mode Exit fullscreen mode

Lines starting with a space are unchanged, lines starting with + were added, and lines starting with - would be removed. Check git diff before every commit, and you will catch small mistakes before they are saved.

Remotes and Cloning: Git Meets the Internet

So far everything happened on your own computer. To get a project that already exists, you clone it. Try it with a tiny public project:

git clone --depth 1 https://github.com/octocat/Hello-World.git
Enter fullscreen mode Exit fullscreen mode

Output:

Cloning into 'Hello-World'...
Enter fullscreen mode Exit fullscreen mode

Listing the new folder shows a README file inside it. The --depth 1 flag downloads only the latest snapshot, which keeps a first look fast. Pushing your own project to GitHub is a separate setup step, and the GitHub website walks you through it. The free Pro Git book explains every command in plain language. Some projects also ship with Docker, which packs the code and its environment together, and our Docker commands cheat sheet for beginners explains the commands you will meet first.

Git vs GitHub

Beginners mix these two up more than anything else:

Git GitHub
What it is A tool that tracks code versions A website that hosts Git repos online
Where it runs On your own computer On GitHub servers, in your browser
Needs internet? No, for commits, branches and history Yes, it is a website
Main job Save snapshots and manage branches Share, back up and collaborate on repos

Common Mistakes and Troubleshooting

Committing without staging first. Edit a file and run git commit straight away, and Git refuses with "no changes added to commit". Run git add first, then commit.

Running Git commands in the wrong folder. Outside a repo, Git stops with "fatal: not a git repository": there is no .git folder here. Move into your project folder with cd, or run git init.

Committing on the wrong branch. Run git branch before you start work, and git switch to move first. If the commit already happened, do not panic: the work is saved, and you can move it later.

Deleting the .git folder. That hidden folder is the entire history. Delete it and every commit, branch and message is gone for good. Never delete it.

Frequently Asked Questions

Is Git the same as GitHub?

No. Git is the free tool on your computer that tracks versions. GitHub hosts Git repos online so people can share and collaborate.

Do I need to use the command line to use Git?

No, but it helps. GitHub Desktop and the Git tools in VS Code cover everyday commands with buttons. Still learn the commands, because tutorials and error messages use them.

Do I need the internet to use Git?

No, not for local work. Commits, branches, history and diffs all work offline. You only need the internet to clone, push or pull.

Can Git track any type of file?

Yes, but it works best with text files like code. Large binary files work too, but each version is stored in full, so the repo grows fast.

Conclusion

Git is a save system for your code: a repo holds the project, a commit saves a snapshot, a branch keeps experiments separate, a remote shares the work, and you have now used all four. Now use Git on your next project, commit little and often, and write messages your future self will understand.

Top comments (0)