Core Git Commands

🧱 1. Repository Setup

Purpose: Start tracking a project with Git.

CommandDescriptionExample
git initInitialize a new Git repository in your current folder.git init
git clone <repo_url>Copy an existing remote repository to your local machine.git clone https://github.com/user/project.git

Tip:
After cloning, you already have a .git folder — no need to run git init again.


🧩 2. Checking Status and Changes

Purpose: See what’s happening in your working directory.

CommandDescriptionExample
git statusShows untracked, modified, and staged files.git status
git diffView unstaged changes (differences between working directory and index).git diff
git diff --stagedView differences between staged files and last commit.git diff --staged

📦 3. Staging and Committing

Purpose: Record file changes into history.

CommandDescriptionExample
git add <file>Stage a file for commit.git add index.html
git add .Stage all changes in the current directory.git add .
git commit -m "message"Save staged changes with a descriptive message.git commit -m "Add homepage layout"
git commit -am "message"Add and commit all tracked files in one step.git commit -am "Fix typo"

🧭 4. Viewing History

Purpose: Review what happened over time.

CommandDescriptionExample
git logShow full commit history.git log
git log --onelineShow compact commit history.git log --oneline
git show <commit_id>View details of a specific commit.git show a1b2c3d

Tip:
Press q to exit the log viewer.


🔁 5. Branching and Merging (Basics)

Purpose: Work on features independently.

CommandDescriptionExample
git branchList all branches.git branch
git branch <name>Create a new branch.git branch feature/login
git switch <name>Switch to another branch.git switch feature/login
git merge <branch>Merge another branch into the current one.git merge feature/login

🌍 6. Working with Remotes

Purpose: Connect your local repo to a remote (e.g., GitHub).

CommandDescriptionExample
git remote add origin <url>Link your local repo to a remote.git remote add origin https://github.com/user/project.git
git push -u origin mainPush commits to the remote repository.git push -u origin main
git pullFetch and merge changes from the remote.git pull
git fetchDownload changes without merging.git fetch

🔄 7. Undoing Changes

Purpose: Fix mistakes safely.

CommandDescriptionExample
git restore <file>Undo unstaged changes in a file.git restore index.html
git restore --staged <file>Unstage a file (keep changes).git restore --staged index.html
git reset --hard <commit>Reset everything to a previous commit (⚠ destructive).git reset --hard HEAD~1
git revert <commit>Create a new commit that undoes the changes from another commit.git revert a1b2c3d

🧰 8. Ignoring Files

Purpose: Tell Git which files/folders to skip.

File: .gitignore

# Example .gitignore
node_modules/
.env
*.log

🕵️ 9. Viewing Information

CommandDescriptionExample
git statusSee which files changed.git status
git diffCompare working changes.git diff
git remote -vView remote connections.git remote -v
git branch -vvView branches and their tracking info.git branch -vv

🧠 10. Common Git Command Workflow

Here’s the everyday Git cycle:

# 1. See what’s changed
git status

# 2. Stage your changes
git add .

# 3. Commit them
git commit -m "Add feature X"

# 4. Push to remote
git push

# 5. Update from others
git pull

Leave a Reply