Skip to content

Git Basics: Init, Commit, Push & Pull — Getting Started Guide

DodaTech Updated 2026-06-22 5 min read

In this tutorial, you'll learn about Git Basics: Init, Commit, Push & Pull. We cover key concepts, practical examples, and best practices.

Git tracks changes to files over time, enabling teams to collaborate without overwriting work and providing a complete history you can browse or revert.

In this tutorial, you'll learn Git fundamentals from initializing your first repository to pushing code to GitHub. Version control is the backbone of modern software development, letting teams collaborate without overwriting each other's work — and Git is the most widely used system in the industry. By the end, you'll be comfortable staging, committing, branching, and syncing with remote repositories.

flowchart LR
  Init[git init] --> Stage[git add] --> Commit[git commit] --> Remote[git push]
  Remote --> Pull[git pull] --> Stage
  Init --> Branch[git branch] --> Merge[git merge]

What Is Git and Why Do You Need It?

Imagine writing a document without an "undo" button — every mistake is permanent. Git gives you unlimited undo, branching for experiments, and a complete history of who changed what and when. If you work with Python, JavaScript, or any code, Git lets you ship features confidently knowing you can always roll back.

Unlike older systems like SVN, Git is distributed — every developer has the full repository on their machine. You can commit, branch, and browse history offline. Only when you're ready do you push changes to a remote like GitHub.

Installing and Configuring Git

Download Git from git-scm.com or install via your package manager:

# Linux (Debian/Ubuntu)
sudo apt install git

# macOS
brew install git

# Verify installation
git --version

Expected output:

git version 2.40.0

Configure your identity — every commit uses this info:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global init.defaultBranch main

Initializing a Repository

Navigate to your project folder and run:

mkdir my-project && cd my-project
git init

Expected output:

Initialized empty Git repository in /home/user/my-project/.git/

This creates a hidden .git folder containing all version history. Now Git tracks every file change.

Staging and Committing

Create a file and track it:

echo "# My Project" > README.md
git status
git add README.md
git commit -m "Initial commit: add README"

Expected output:

On branch main
nothing to commit, working tree clean

The git add command moves changes to the staging area. Think of it as a preparation zone — you decide exactly which changes go into the next commit. git commit takes the snapshot and records it permanently.

Branching Workflow

Branches let you work on features without affecting the main codebase:

git branch feature-login
git checkout feature-login
# Or combine: git checkout -b feature-login

echo "login code" > login.py
git add login.py
git commit -m "Add login functionality"

git checkout main
git merge feature-login

Expected output:

Updating a1b2c3d..e5f6g7h
Fast-forward
 login.py | 1 +
 1 file changed, 1 insertion(+)

Working with Remotes

Connect your local repo to GitHub and push:

git remote add origin https://github.com/yourusername/my-project.git
git push -u origin main

Later, pull the latest changes from teammates:

git pull origin main

Common Errors

Error Cause Fix
fatal: not a git repository Running Git outside a repo Run git init first
Please tell me who you are Missing user config Set git config user.name and user.email
hint: Waiting for your editor to close No commit message provided Use git commit -m "message"
rejected: failed to push Remote has newer commits Run git pull before git push
fatal: remote origin already exists Remote already configured Use git remote set-url origin URL
Your branch is ahead of origin Local commits not pushed Run git push
merge conflict Concurrent edits to same file Resolve manually then git commit
detached HEAD Checked out a commit not a branch Create a branch with git switch -c
fatal: pathspec did not match File doesn't exist in index Check git status first
LF will be replaced by CRLF Line ending mismatch Configure git config core.autocrlf

Practice Questions

What is the difference between git add and git commit?

git add moves changes to the staging area, preparing them for recording. git commit takes the staged snapshot and saves it permanently to the repository history. You must add before you commit.

How do I undo a commit I just made?

Use git reset --soft HEAD~1 to undo the commit while keeping your changes staged. Use git reset --hard HEAD~1 to discard both the commit and the changes.

What does git push -u do?

The -u flag sets the upstream tracking reference. After running git push -u origin main, future pushes and pulls can use just git push or git pull without specifying the remote and branch.

Can I commit without adding first?

No. Git requires you to explicitly stage changes with git add (or git rm) before committing. The -a flag on git commit -a -m "msg" stages all tracked files automatically, but untracked files still need git add.

What is the .git folder?

The .git folder is Git's internal database. It stores all commits, branches, tags, and configuration. Deleting it removes all version history but leaves your working files untouched

Challenge

Create a repository with three branches: main, develop, and feature/payment. On feature/payment, add a file called payment.py with a comment. Merge it into develop, then merge develop into main. Delete feature/payment locally. Write the full command sequence without looking at notes.

Real-World Task

Set up a Git repository for a small bash project that scans log files. Initialize the repo, create a .gitignore excluding *.log files, make three commits adding incremental features (file listing, keyword search, output formatting), and push it to a GitHub remote. This mirrors the workflow used at DodaTech for tools like Durga Antivirus Pro.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro