Skip to Content
GitEssential Git Commands

Essential Git Commands

Git provides powerful commands to inspect, reset, and manage your project. Hereโ€™s a handy reference for common tasks.


๐Ÿ” View Commit History

git log

Shows a list of commits, their IDs (hashes), authors, and messages.


โช Go Back to a Specific Commit

1. Check history:

git log

Copy the commit hash (e.g., 1a2b3c4d)

2. Reset to that commit:

git reset --hard <commit_hash>

Example:

git reset --hard 1a2b3c4d

โš ๏ธ --hard resets your working directory and discards uncommitted changes.


๐Ÿงน Soft Reset (keep changes)

git reset --soft <commit_hash>

Moves HEAD to an earlier commit but keeps your code and staging.


โŒ Remove a File from Repo

Keep the file locally, remove from Git:

git rm --cached filename.txt

Delete file from Git and local folder:

git rm filename.txt

๐Ÿ›  Check Current Status

git status

Shows:

  • Staged files
  • Unstaged changes
  • Untracked files

๐ŸŒฟ View All Branches

git branch

To view remote branches too:

git branch -a

๐ŸŒฑ Create a New Branch

git branch new-feature

Switch to it:

git checkout new-feature

Or combine both:

git checkout -b new-feature

๐Ÿ”€ Merge Branches

Switch to the branch you want to merge into (usually main), then:

git merge new-feature

๐Ÿงญ See Where You Are (HEAD, Branch)

git status

To see which commit youโ€™re on:

git log --oneline

๐Ÿ”„ Discard Local Changes

Discard changes to a file:

git restore filename.txt

Discard all unstaged changes:

git restore .

๐Ÿงฝ Unstage a File

git restore --staged filename.txt

๐Ÿ” Revert a Commit (undo but keep history)

git revert <commit_hash>

This creates a new commit that undoes the changes from the given commit โ€” safer than reset.


๐Ÿ” See Differences

Unstaged vs last commit:

git diff

Staged vs last commit:

git diff --cached

๐Ÿงช Try It Yourself

Problem: Revert a Mistaken Commit

Show Code

git log git revert <commit_hash>
Last updated on