_____                   _             ___     _________  __
 |_   _|__ _ __ _ __ ___ (_)_ __   __ _| \ \   / / ____\ \/ /
   | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | |\ \ / /|  _|  \  /
   | |  __/ |  | | | | | | | | | | (_| | | \ V / | |___ /  \
   |_|\___|_|  |_| |_| |_|_|_| |_|\__,_|_|  \_/  |_____/_/\_\
advanced14 min

Git Command Line Guide

What is Git?

Git is a distributed version control system created in 2005 by Linus Torvalds for developing the Linux kernel. Today it is the most widely used version control tool in the world and a cornerstone of software development processes. Git stores a snapshot of every change in your files, allowing you to revert to any point in time, work in parallel on different branches, and collaborate with teammates without conflicts.

Although GUI-based Git tools exist, using Git from the command line gives you full control. Command line knowledge is indispensable in complex scenarios, automation processes, and server environments. In this guide, we will cover Git's most important commands from beginner to advanced level.

Git Installation and Configuration

# Check if Git is installed
$ git --version

# Configure user information (required on first setup)
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"

# Set default editor
$ git config --global core.editor "nano"

# View all configuration
$ git config --list

# Enable colored output
$ git config --global color.ui auto

Creating a Repository

git init — Initialize a New Repository

The git init command converts an existing directory into a Git repository. When this command is executed, a hidden .git folder is created in the directory. This folder contains all the version control history and configuration.

# Create a new project directory and initialize a Git repo
$ mkdir new-project
$ cd new-project
$ git init
# Output: Initialized empty Git repository in /home/user/new-project/.git/

# Initialize Git in an existing project
$ cd existing-project
$ git init

git clone — Copy an Existing Repository

git clone downloads all files, branches, and history from a remote repository to your local machine.

# Clone via HTTPS
$ git clone https://github.com/user/project.git

# Clone via SSH
$ git clone git@github.com:user/project.git

# Clone with a different directory name
$ git clone https://github.com/user/project.git my-project

# Clone a specific branch
$ git clone -b development https://github.com/user/project.git

Basic Workflow Commands

git status — View Repository Status

git status shows the state of changes in your working directory. You can see which files have been modified, which are staged, and which are untracked.

# Check repository status
$ git status

# Short format status
$ git status -s
# M  file.txt      (modified)
# A  new.txt       (added)
# ?? untracked.txt (untracked)

git add — Stage Changes

git add adds changes to the staging area. It lets you select which changes will be included before committing.

# Stage a specific file
$ git add file.txt

# Stage multiple files
$ git add file1.txt file2.txt file3.txt

# Stage all changes
$ git add .

# Stage files matching a pattern
$ git add *.js

# Unstage a file
$ git reset HEAD file.txt

git commit — Save Changes

git commit permanently saves staged changes to the repository. Each commit is a snapshot of your project's current state and has a unique SHA-1 hash value.

# Commit with a message
$ git commit -m "Add user login form"

# Auto-stage all tracked files and commit
$ git commit -am "Fix: prevent empty form submission"

# Detailed commit message (editor opens)
$ git commit

# Amend the last commit (use carefully!)
$ git commit --amend -m "Corrected commit message"

git log — Examining History

git log displays the commit history. It is used to examine the project's history, find specific changes, and see who did what and when.

# View commit history
$ git log

# One-line summary view
$ git log --oneline

# Show last 5 commits
$ git log -5

# Graphical branch view
$ git log --oneline --graph --all

# History of a specific file
$ git log -- file.txt

# Commits by a specific author
$ git log --author="Ali"

# Filter by date range
$ git log --since="2026-01-01" --until="2026-02-28"

# With change details
$ git log -p

Branch Management

Branching is one of Git's most powerful features. It allows you to develop new features, fix bugs, and experiment without breaking the main codebase.

git branch — Branch Operations

# List existing branches
$ git branch

# List all branches including remote
$ git branch -a

# Create a new branch
$ git branch new-feature

# Delete a branch
$ git branch -d old-branch

# Force delete a branch (if unmerged changes exist)
$ git branch -D old-branch

# Rename a branch
$ git branch -m old-name new-name

git checkout / git switch — Switching Between Branches

# Switch to an existing branch
$ git checkout new-feature
# or (Git 2.23+)
$ git switch new-feature

# Create and switch to a new branch
$ git checkout -b bugfix
# or
$ git switch -c bugfix

# Return to the main branch
$ git checkout main

git merge — Merging Branches

Used to merge changes from one branch into another.

# First switch to the target branch
$ git checkout main

# Merge the source branch
$ git merge new-feature

# Merge with a message
$ git merge new-feature -m "Merge new feature branch"

# In case of conflicts
# 1. Edit the conflicting files
# 2. Stage the resolved files
$ git add conflicted-file.txt
# 3. Complete the merge commit
$ git commit

Remote Repository Operations

git remote — Remote Repository Management

# List remote repositories
$ git remote -v

# Add a remote repository
$ git remote add origin https://github.com/user/project.git

# Change remote URL
$ git remote set-url origin https://github.com/user/new-project.git

git push — Sending Changes

# Push changes to remote repository
$ git push origin main

# First push (set upstream)
$ git push -u origin main

# Subsequent pushes (if upstream is set)
$ git push

# Push a specific branch
$ git push origin new-feature

git pull — Pulling Changes

# Pull and merge remote changes
$ git pull origin main

# Pull with rebase (cleaner history)
$ git pull --rebase origin main

# Short form (if upstream is set)
$ git pull

.gitignore File

The .gitignore file specifies files and directories that Git should not track. Build outputs, dependency folders, and files containing sensitive information should be added to this list.

# .gitignore example

# Dependency directories
node_modules/
vendor/
__pycache__/

# Build outputs
dist/
build/
*.o
*.class

# Environment files (sensitive data)
.env
.env.local
*.key
*.pem

# IDE files
.vscode/
.idea/
*.swp
*.swo

# Operating system files
.DS_Store
Thumbs.db

# Log files
*.log
logs/

Useful git diff Usage

# Show changes in the working directory
$ git diff

# Show staged changes
$ git diff --staged

# Differences between two commits
$ git diff abc123 def456

# Differences between two branches
$ git diff main..new-feature

# Changes in a specific file
$ git diff -- file.txt

Common Workflow Example

# 1. Make sure the main branch is up to date
$ git checkout main
$ git pull origin main

# 2. Create a new feature branch
$ git checkout -b feature/user-profile

# 3. Make your changes and commit
$ git add src/components/Profile.tsx
$ git commit -m "Add user profile page component"

$ git add src/api/user.ts
$ git commit -m "Complete user API integration"

# 4. Pull main into your branch (to stay current)
$ git pull origin main

# 5. Push your branch to remote
$ git push -u origin feature/user-profile

# 6. Create a pull request (via GitHub/GitLab)
# 7. After code review and merge, delete the branch
$ git checkout main
$ git pull origin main
$ git branch -d feature/user-profile

Common Recovery Commands

# Undo the last commit (changes are preserved)
$ git reset --soft HEAD~1

# Unstage a file
$ git reset HEAD file.txt

# Discard changes in a file (CAUTION: irreversible!)
$ git checkout -- file.txt

# Revert a specific commit
$ git revert abc123

# Temporarily stash changes
$ git stash
$ git stash pop

Summary

Git is an essential tool for modern software development. Creating repositories with init and clone, saving changes with add and commit, branching with branch and merge, and collaborating with push and pull — once you learn these fundamental commands, you can begin leveraging the endless possibilities Git offers. Using Git from the command line may seem challenging at first, but with practice it will become your most reliable and efficient tool.