🐙
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
  • Recovering Lost Commits with Git Reflog
  • Recovering Lost or Orphaned Commits with Git fsck
  • Recovering Deleted Branches

Was this helpful?

  1. Git Troubleshooting

Recovering Lost Commits or Branches

Losing commits or branches in Git can be a frustrating experience, but fortunately, Git provides several strategies and techniques to help recover them.

Recovering Lost Commits with Git Reflog

Git reflog (reference log) is a powerful tool that keeps a record of all the local branch updates and other Git operations. It can be extremely useful for recovering lost commits. Follow these steps to recover lost commits using Git reflog:

  • First, make sure you are in the repository where the lost commits were made.

  • Run the following command to view the reflog:

git reflog
  • The reflog will display a history of all branch updates and other Git operations. Identify the commit hash or the branch name that you want to recover.

  • Once you have identified the commit or branch, use the following command to restore it:

git checkout <commit or branch>

For example, to restore a lost commit with the hash abcdef1, run:

git checkout abcdef1

Recovering Lost or Orphaned Commits with Git fsck

Git fsck (file system consistency check) is another powerful tool that can help recover lost or orphaned commits.

It scans the Git object database and identifies any dangling commits that are not reachable from any branch or tag. Follow these steps to recover lost or orphaned commits using Git fsck:

  • Make sure you are in the repository where the lost or orphaned commits exist.

  • Run the following command to perform the fsck check:

git fsck --lost-found
  • Git fsck will display any dangling commits with their corresponding commit hashes. Take note of the commit hash of the lost commit you want to recover.

  • Create a new branch at the lost commit using the following command:

git branch <branch-name> <commit-hash>

Replace <branch-name> with the desired name for the new branch and <commit-hash> with the commit hash you noted earlier.

  • Switch to the newly created branch using:

git checkout <branch-name>

Recovering Deleted Branches

Accidentally deleting a branch can happen, but Git provides a way to recover them as long as they were not explicitly pruned. Follow these steps to recover a deleted branch:

  • Identify the commit hash or the reference where the deleted branch last pointed to. This can be found in the Git reflog or by inspecting the commit history.

  • Use the following command to recreate the deleted branch:

git branch <branch-name> <commit-hash>

Replace <branch-name> with the name of the deleted branch and <commit-hash> with the commit hash or reference you identified earlier.

  • If you want to switch to the recovered branch, use the following command:

git checkout <branch-name>
PreviousUndoing Changes with Git - Reverting and ResettingNextDealing with Repository Corruption or Other Issues

Last updated 1 year ago

Was this helpful?