Everything you need as a full stack developer

Common Git Mistakes and How to Recover

- Posted in Junior Developer by

TL;DR Common Git mistakes include committing sensitive data, pushing to the wrong branch, losing uncommitted work, forgetting to add new files, and forced pushing. To recover from these mistakes, use commands like git commit --amend, git reset --soft, git cherry-pick, and git reflog. Following step-by-step guides can help rectify these errors and maintain a healthy codebase.

Common Git Mistakes and How to Recover

As a full-stack developer, you're likely no stranger to using Git for version control in your projects. However, even with experience, mistakes can still happen. In this article, we'll cover some common Git mistakes that can occur and provide step-by-step guides on how to recover from them.

Mistake 1: Committing Sensitive Data

You've just finished implementing a new feature, and in the excitement of the moment, you commit your changes without realizing that you've accidentally included sensitive data such as API keys or database credentials. This can be a major security risk if pushed to a public repository.

Recovery Steps:

  1. Amend the Commit: Run git commit --amend to modify the last commit and remove the sensitive data.
  2. Reset the Local Branch: Run git reset --soft HEAD~1 to undo the last commit and move the changes back to the staging area.
  3. Remove Sensitive Data: Edit the files to remove the sensitive data, then add and commit the changes with a clear message indicating that sensitive data was removed.

Mistake 2: Pushing to the Wrong Branch

You've been working on a feature branch, but in haste, you accidentally push your changes to the master branch instead of the intended feature branch. This can cause unintended changes to be deployed to production.

Recovery Steps:

  1. Create a New Branch: Run git checkout -b new-feature-branch to create a new branch from the current state.
  2. Cherry-Pick Commits: Run git cherry-pick <commit-hash> to apply the commits from the master branch to the new feature branch.
  3. Revert Changes on Master: Run git revert <commit-hash> to undo the changes on the master branch.

Mistake 3: Losing Uncommitted Work

You've made significant changes to your code, but before committing them, you encounter an unexpected issue that requires a system restart or a sudden power outage occurs. When you return to your project, you realize that your uncommitted work is lost.

Recovery Steps:

  1. Check Git Status: Run git status to see if any files are still staged or modified.
  2. Use Git Reflog: Run git reflog to view a log of all commits and actions taken on the repository, which can help you recover your lost work.
  3. Restore Files: Use the reflog output to identify the commit hash before the loss, then run git reset --hard <commit-hash> to restore the files to their previous state.

Mistake 4: Forgetting to Add New Files

You've added new files to your project, but forgot to add them to the Git repository. This can lead to missing files when collaborating with others or deploying to production.

Recovery Steps:

  1. Add New Files: Run git add <file-name> or git add . to stage all new and modified files.
  2. Commit Changes: Run git commit -m "Added new files" to commit the changes with a clear message.

Conclusion

Git mistakes can happen to anyone, but knowing how to recover from them is crucial to maintaining a healthy codebase and avoiding potential security risks. By following these recovery steps, you'll be able to quickly rectify common Git mistakes and get back to developing your project with confidence. Remember to stay vigilant when working with Git, and don't hesitate to seek help if you're unsure about how to recover from a mistake.

Key Use Case

Here is a workflow/use-case example:

Scenario:

As a developer, I'm working on a new feature for an e-commerce website. I've completed the implementation and committed my changes without reviewing them carefully. Upon pushing my changes to the remote repository, I realize that I accidentally included API keys in one of the files.

Step-by-Step Recovery:

  1. I immediately run git commit --amend to modify the last commit and remove the sensitive data.
  2. Next, I run git reset --soft HEAD~1 to undo the last commit and move the changes back to the staging area.
  3. I edit the file to remove the API keys, then add and commit the changes with a clear message indicating that sensitive data was removed.

By following these steps, I'm able to quickly recover from my mistake and ensure the security of our repository.

Finally

Mistake 5: Forced Pushing

You've made changes to your local branch, but someone else has pushed updates to the remote branch in the meantime. In haste, you use git push --force to overwrite the remote branch with your local changes, unaware that this will delete other developers' commits.

Recovery Steps:

  1. Communicate with Team: Inform your team about the mistake and collaborate on recovering the lost commits.
  2. Use Git Reflog: Run git reflog to identify the commit hashes before the forced push.
  3. Cherry-Pick Lost Commits: Run git cherry-pick <commit-hash> to reapply the lost commits to the remote branch.

Recommended Books

Here are some recommended books for developers:

• "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin • "The Pragmatic Programmer: From Journeyman to Master" by Andrew Hunt and David Thomas • "Code Complete: A Practical Handbook of Software Construction" by Steve McConnell

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more