Logo

dev-resources.site

for different kinds of informations.

Undo Mistakes in Git: Revert, Reset, and Checkout Simplified

Published at
1/9/2025
Categories
git
github
githubactions
webdev
Author
maulikpaghdal
Categories
4 categories in total
git
open
github
open
githubactions
open
webdev
open
Author
13 person written this
maulikpaghdal
open
Undo Mistakes in Git: Revert, Reset, and Checkout Simplified

1. Git Revert

When to Use:

Use git revert to undo a specific commit while preserving your commit history. It’s ideal for public repositories where rewriting history can cause issues.

How It Works:

git revert creates a new commit that undoes the changes introduced by the targeted commit.

Example:

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Git will prompt you to edit a commit message. Save it, and a new commit reversing the changes will be added.


2. Git Reset

When to Use:

Use git reset to undo changes by moving the HEAD pointer. This command is more destructive than revert as it alters commit history.

Modes of git reset:

  • --soft: Keeps changes in the staging area.
  • --mixed (default): Keeps changes in your working directory.
  • --hard: Discards changes completely.

Example:


# Reset to a specific commit and keep changes in working directory git reset --mixed <commit-hash> # Reset and discard changes git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

⚠️ Be cautious with --hard as it permanently deletes changes.


3. Git Checkout

When to Use:

Use git checkout to switch branches or restore files to a specific state.

Switching Branches:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Restoring Files:

Restore a specific file to the state of the last commit:

git checkout HEAD -- <file-name>
Enter fullscreen mode Exit fullscreen mode

When to Use What?

  • Revert: Undo a specific commit in a shared repository.
  • Reset: Rewrite commit history in local branches.
  • Checkout: Switch branches or restore files without modifying commits.

Best Practices

  1. Understand the Impact:
    • Use revert for safe, non-destructive changes.
    • Use reset only when working locally or with private branches.
  2. Always Backup:
    • Before using reset --hard, stash your changes or back up the branch.
  3. Collaborate:
    • For shared repositories, communicate with your team before altering history.

Conclusion

Knowing when and how to use revert, reset, and checkout can save you from hours of debugging and frustration. Git’s flexibility makes it an indispensable tool for developers.

Learn More

For a deeper dive and detailed examples, visit the original guide on Script Binary.


Let’s Discuss!

Have questions about Git commands? Share your thoughts in the comments below, and follow me for more Git and version control tips!

github Article's
30 articles in total
Favicon
CΓ³mo gestionar tus proyectos de software con Github
Favicon
How to upload Markdown files to Dev.to from GitHub
Favicon
Implantando um aplicativo Angular com DigitalOcean e GitHub de forma gratuita
Favicon
Survival Manual: How to Create and Manage a Project in Git
Favicon
Top 50 Websites a Backend Developer Must Know πŸ–₯οΈπŸ”§πŸš€
Favicon
Unlocking the Power of GitHub Copilot: Your AI Pair Programmer
Favicon
Top 10 Trending GitHub Repositories, Nov 24 2024
Favicon
Improving Port Management Speed: Why I Created `port-client` to Replace `npx kill-port`
Favicon
Unlock Your Coding Potential with the GitHub Copilot Global Bootcamp!
Favicon
Publish Private Nuget Packages on Github
Favicon
Git Merge VS Git Rebase: Which One Should YOU Use?
Favicon
Static sites FTW
Favicon
Hood Ball β€” Multiplayer web-based game
Favicon
GitHub Makeover: Create a Profile README That Stands Out and Connects! πŸ‘¨β€πŸ’»
Favicon
Deploying a Next.js UI App on S3 Using Jenkins🀩
Favicon
The Global South OSC
Favicon
🎁 20 Open Source Projects You Shouldn't Miss in 2025
Favicon
A Conversation with Docker CTO Justin Cormack and Flux CEO Ron Efrani: The Future of Developer Environments
Favicon
Sample Programs Repo Celebrates 1,000 Code Snippets
Favicon
Learn Machine Learning with these amazing GitHub repositories! πŸš€
Favicon
πŸˆβ€β¬› Git and GitHub: A Beginner’s Guide to Version Control πŸš€
Favicon
Undo Mistakes in Git: Revert, Reset, and Checkout Simplified
Favicon
My First npm Package!
Favicon
Top 14 GitHub Data Risks: Data Loss Scenarios and How to Prevent Them
Favicon
Mastering Git and GitHub: A Guide for New Team Members
Favicon
Why I Stopped Using Plain Git Pull (And Why You Should Too)
Favicon
Why I Built commit-ai: A Story About Git Security and Team Safety
Favicon
πŸ”„ Automating GitHub PR Notifications with Slack Integration: A Journey
Favicon
How to Link git to GitHub via SSH on Windows
Favicon
Mastering Git Workflows: Beyond Basic Commands

Featured ones: