🐙
Git Developer Guide
About
  • Overview
  • Scope of this book
  • Table of Content
  • 🐢Introduction to Version Control
    • What is Version Control?
    • Overview of git and it's benefits
    • Setting up Git on Different Platforms
  • 🍼Git Fundamentals
    • Initialising a new Git repository
    • Understanding the Git Workflow
    • Committing Changes and Writing Good Commit Messages
    • Viewing and Navigating Commit History
    • Git Basics - Practice Scenarios
      • Initialising a Git Repository
      • Committing Changes
      • Exploring Commit History
      • Amending and Undoing Commits
  • 🦕Working With Git
    • What is Git Branch?
    • Creating and Switching Between Branches
    • Merging Branches and Resolving Conflicts
    • Best Practices for Branch Management
    • Git Workflows
    • Git Log
    • Git Stash
    • Working with Git - Practice Scenarios
      • Creating and Switching Between Branches
      • Merging Branches and Resolving Conflicts
      • Branching Strategies in a Team Project
      • Rolling Back to a Previous Version
      • Experimenting with Feature Branches
      • Working with Stash
  • 🤝Working with Remote Repositories
    • Cloning a Repository from Remote
    • Pushing and Pulling Changes to and from Remote Repositories
    • Collaborative Workflows - Forking, Branching, and Pull Requests
    • Resolving Conflicts in a Collaborative Environment
    • Collaborating with Git - Practice Scenarios
      • Cloning a Remote Repository
      • Pushing and Pulling Changes
      • Collaborative Workflow with Forking and Pull Requests
      • Resolving Conflicts in a Pull Request
  • 🏆Advanced Git Features
    • Aliases and Custom Configurations
    • Working with Tags and Releases
    • Rewriting Commit History with Interactive Rebase
    • Utilising Git Hooks for Automation
    • Advanced Git Features - Practice Scenarios
      • Creating Custom Git Aliases
      • Working with Tags and Releases
      • Rewriting Commit History with Interactive Rebase
      • Using Git Hooks for Automated Testing
  • 😎Git in Real-World
    • Managing a Project with Multiple Contributors
    • Integrating Git with Continuous Integration, Continuous Deployment (CI, CD)
    • Versioning Assets with Git LFS (Large File Storage)
    • Deploying a Web Application using Git
    • Git In Real World - Practice Scenarios
      • Managing a Project with Multiple Contributors
      • Integrating Git with CICD Pipelines
      • Versioning Assets with Git LFS
      • Deploying a Web Application using Git
  • Git Troubleshooting
    • Common Mistakes and Pitfalls When Using Git
    • Undoing Changes with Git - Reverting and Resetting
    • Recovering Lost Commits or Branches
    • Dealing with Repository Corruption or Other Issues
  • Git Best Practices and Tips
    • Creating efficient git workflows: writing clean code for faster reviews
    • The importance of clean code in collaborative development
    • Significance of consistent naming conventions & coding Standards
    • Good code documentation for better git workflows
    • Writing meaningful git commit messages
    • Atomic commits in git & it's benefits for software teams
    • Structuring code & managing dependencies for better git workflows
    • Git branching strategies for software teams
  • Conclusion & Next Steps
    • Recap of Key Concepts and Commands
    • Further Resources for Expanding Git Knowledge
    • Encouragement and Tips for Continued Learning and Practice
  • License Considerations
Powered by GitBook
On this page

Was this helpful?

  1. Working With Git
  2. Working with Git - Practice Scenarios

Rolling Back to a Previous Version

In software development, bugs are an inevitable part of the process. Sometimes, a bug may be introduced unintentionally, causing unexpected behaviour or errors in the codebase.

To resolve such issues, it is crucial to have proper version control in place. In this scenario, we will highlight a situation where a bug is introduced, demonstrate how to revert to a previous commit to resolve the issue, and discuss the importance of version control for bug tracking and resolution.

Identifying the Bug

Before proceeding with the rollback, it's important to identify the bug and understand its impact on the codebase. This can be done through manual testing, observing error messages, or examining the code itself. Understanding the issue thoroughly will help us choose the appropriate commit to which we want to roll back.

Using Git Log to Find the Target Commit

To find the commit that introduced the bug, we can utilise the git log command. Open a terminal or command prompt and navigate to the project's root directory. Run the following command:

git log

This command will display a list of commits, starting from the most recent. Each commit will have a unique commit hash, commit message, and other relevant information. Identify the commit where the bug was introduced, and note down its commit hash.

Creating a New Branch

To safely roll back to a previous version, it is recommended to create a new branch. This allows us to preserve the current state of the codebase while working on the fix. Run the following command:

git branch bug-fix

This command creates a new branch named "bug-fix" based on the current commit.

Checking Out the Target Commit

Now, let's check out the target commit using its commit hash. Run the following command:

git checkout <commit-hash>

Replace <commit-hash> with the actual commit hash of the target commit. This command switches the codebase to the state of that specific commit.

Verifying the Rollback

Once we have checked out the target commit, it's important to verify whether the bug has been resolved. You can test the code manually or use automated tests to ensure the expected behaviour has been restored. If the bug persists or additional issues arise, further investigation may be necessary.

Committing the Fix

If the rollback successfully resolves the bug, we can commit the fix to the new branch we created earlier. Run the following commands:

git add .
git commit -m "Rollback to fix bug"

These commands stage the changes made during the rollback and create a new commit with an appropriate commit message.

Merging the Fix

After committing the fix, we can merge the bug-fix branch back into the main branch (e.g., master or main). This ensures that the fix becomes a part of the main codebase. Run the following command:

git checkout main
git merge bug-fix

These commands switch back to the main branch and merge the changes from the bug-fix branch into it.

Pushing the Changes

To share the fix with other developers or deploy it to production, we need to push the changes to a remote repository. Run the following command:

git push origin main

This command pushes the changes from the main branch to the remote repository (replace origin with the appropriate remote if necessary).

Rolling back to a previous version using Git is a powerful technique to resolve bugs introduced in the codebase. You can track and manage bug fixes effectively and rollback if there are errors/mistakes during your workflow.

PreviousBranching Strategies in a Team ProjectNextExperimenting with Feature Branches

Last updated 1 year ago

Was this helpful?

🦕