A BEGINNER'S GUIDE TO GIT
Source: Dev.to
Introduction
Git is a free, open‑source version control system that developers and analysts use for tracking changes, collaborating, and managing project history efficiently. It allows you to work offline and merge your work seamlessly.
Before getting started with Git, configure your identity:
git config --global user.name "nashipae"
git config --global user.email "nashipaentungani@gmail.com"
Git Workflow
A file goes through three stages in Git:
- Working directory – where you edit files.
- Staging area – a loading dock for changes you want to save (almost final).
- Repository – the permanent storage area for your project.
Essential Commands
Starting a Project
-
Initialize a new repository
git init -
Clone an existing repository
git clone
Checking Status and Tracking Changes
-
Show the current status
git status -
View detailed changes
git diff -
Stage specific files
git add -
Stage all changes
git add . -
Unstage a file
git restore --staged -
Commit staged changes
git commit -m "Your commit message" -
View commit history
git log
Working with Remotes
-
Add a remote repository
git remote add origin -
Push to the remote (first time)
git push -u origin main -
Push subsequent changes
git push origin main -
Pull changes from the remote
git pull origin main
History, Comparison, and Resetting
-
Compact log view
git log --oneline -
Show who last modified each line of a file
git blame -
Compare two commits
git diff -
Reset the current HEAD
# Soft reset (keeps changes staged) git reset --soft HEAD~1 # Mixed reset (unstages changes but keeps them in working directory) git reset --mixed HEAD~1 # Hard reset (discards all changes) git reset --hard HEAD~1