Eslam HelmyEslam Helmy
4 min readEslam

Mastering Git: The Power of Git Reset — A Developer’s Journey 🚀


Mastering Git: The Power of Git Reset — A Developer’s Journey 🚀

We’re all familiar with creating feature branches to add new code into the targeted branch. We know how to create feature branches, add commits, and push them remotely. Then we create PRs to merge our code from the feature branch to the target branch. Although this handles most of our work, we encounter situations during development that we struggle to know how to handle.

Consider these situations:

  • Realizing you forgot to add something to your commit. Oops! 🤦‍♂️
  • Accidentally committing sensitive information 😱
  • Committing code that is actually a hot mess 🔥
  • Having multiple commits that should be combined into one 📦

Before discovering git reset, my solutions were less than ideal:

  1. Adding a new commit for forgotten changes
  2. Creating another commit to remove mistaken additions
  3. Discarding the entire branch and starting over
  4. Pushing them anyway without consolidation

Trust me, if a Git guru saw my commit history back then, they’d know I was fumbling in the dark. But fear not! Our superhero git reset is here to save the day!

Let’s walk through each scenario and see how git reset turns these Git nightmares into sweet dreams. 😴💭

Scenario 1: Adding Forgotten Changes 🕰️

Imagine you’re working on a bookstore app and have just added a BookReview class:

public class BookReview
{
    public int Id { get; set; }
    public int BookId { get; set; }
    public string ReviewerName { get; set; }
    public int Rating { get; set; }
    public string Comment { get; set; }
}

You committed your change:

git add .
git commit -m 'Added_Book_Review'

Then it hits you — you forgot the DateAdded property! 😱 Don't sweat it. Let's fire up our Git time machine:

git reset --soft HEAD~1

Boom! We’re back in time, changes still intact. Now let’s add that missing piece:

public DateTime DateAdded { get; set; }

And commit again, this time with everything we need:

git commit -m 'Added_Book_Review_with_DateAdded'

Scenario 2: Removing Mistakenly Added Files 🧹

You’ve just added a new endpoint to list all books but you accidentally committed an HTTP file along with it:

[ApiController]
[Route("api/[controller]")]
public class BooksController : ControllerBase
{
    // Controller code...
}
### Get all books
GET https://localhost:5001/api/books
Accept: application/json

Don’t panic! Here’s how we’ll clean up this mess:

git reset --mixed HEAD~1

This unstages everything, giving us a clean slate. Now we can carefully remove what we don’t want and commit again:

git add .
git commit -m 'Added_Get_Books_Endpoint'

Scenario 3: The “What Was I Thinking?” Do-Over 🔄

Ever written code so bad you wish you could pretend it never existed? Been there! Let’s say you committed this problematic code:

[HttpPut("{id}")]
public IActionResult UpdateBook(int id, [FromBody] Book updatedBook)
{
    // Get the list of all books (terribly inefficient)
    var allBooks = _bookService.GetAllBooks();
 
    // Call the service method to update the book in a very bad way
    var result = _bookService.UpdateBook(id, updatedBook);
 
    // If no book was found (though this is very poorly handled)
    if (result == null)
    {
        return NotFound();
    }
 
    return Ok(result);
}

Yikes! 😬 Let’s make this bad dream disappear:

git reset --hard HEAD~1

It’s gone. Now you can start fresh and write code you’re actually proud of.

Scenario 4: Combining Multiple Commits 📦

You’ve made a series of commits, but now you want to roll them up into one commit as they seem related to each other.

To consolidate several related commits into one:

git reset --soft HEAD~3

This command retains all changes from the last three commits but removes the commit history. You can then create a single, comprehensive commit:

git commit -m 'Added_Book_Endpoints_and_Reviews'

Just like that, your messy commit history is now a single, professional-looking commit. 🌟

This is all about how I use git reset in my day to day work but remember guys:

Use git reset only on local, unshared branches. For shared branches, use git revert to maintain a proper history. ⚠️

By mastering git reset, you can maintain a clean, professional commit history and work more efficiently with Git. This knowledge empowers you to manage your version control with confidence and precision. 🎓💪

Thanks for reading!

Share this post