Unboxing GIT Fundamentals: Part 10 of 10

Anand
4 min readJun 12, 2024

--

Part 10: Debugging and Troubleshooting Common Git Issues

Welcome to the final part of our Git learning series! Throughout this series, we’ve covered everything from the basics of Git to advanced commands, best practices, workflows, and CI/CD integrations. In this last installment, we’ll focus on strategies for debugging and troubleshooting common Git issues. Being able to resolve these issues quickly and effectively is crucial for maintaining productivity and ensuring smooth project workflows.

1. Undoing Changes

Sometimes, you need to undo changes in your repository. Depending on the scenario, there are different ways to revert or discard changes:

  • Undoing Uncommitted Changes:
  • Discard changes in a file:
git checkout -- <file>
  • Discard all changes:
git reset --hard
  • Undoing Commits:
  • Undo the last commit but keep changes in the working directory:
git reset --soft HEAD~
  • Undo the last commit and discard changes:
git reset --hard HEAD~
  • Reverting a Commit:
  • Create a new commit that undoes changes from a previous commit:
git revert <commit-hash>

2. Resolving Merge Conflicts

Merge conflicts occur when changes in different branches conflict with each other. Here’s how to resolve them:

  1. Identify the Conflict:
  • When merging branches, Git will notify you of conflicts and mark the conflicting files.

2. Edit the Conflicted Files:

  • Open the conflicted file in a text editor and look for conflict markers (<<<<<<, ======, >>>>>>).
  • Decide which changes to keep and remove the conflict markers.

3. Mark the Conflict as Resolved:

  • After resolving conflicts, stage the resolved files:
git add <resolved-file>

4. Commit the Resolution:

  • Complete the merge by committing the resolved changes:
git commit

3. Handling Detached HEAD

A detached HEAD state occurs when you check out a specific commit rather than a branch. This can lead to lost changes if not handled properly.

  • Check Out a Commit:
git checkout <commit-hash>
  • Reattach the HEAD to a Branch:
  • Create a new branch from the detached state to save changes:
git checkout -b <new-branch-name>
  • Switch Back to a Branch:
git checkout <branch-name>

4. Fixing Mistaken Commits

Sometimes, you commit to the wrong branch or need to move a commit.

  • Move the Last Commit to a Different Branch:
  1. Create a new branch at the current commit:
git branch <new-branch-name>

2. Switch back to the correct branch:

git checkout <correct-branch>

3. Cherry-pick the commit:

git cherry-pick <commit-hash>

4. Amend the Last Commit:

  • To modify the last commit (e.g., to fix a commit message or add changes):
git commit --amend

5. Restoring Deleted Branches

If you accidentally delete a branch, you can restore it if the branch hasn’t been garbage-collected yet.

  • List All References:
git reflog
  • Create a New Branch from the Reference:
git checkout -b <restored-branch-name> <commit-hash>

6. Recovering Lost Commits

Sometimes, you may lose track of commits due to a reset or rebase.

  • Use the Reflog to Find Lost Commits:
git reflog
  • Recover the Commit:
  • Once you identify the lost commit, you can create a new branch or cherry-pick the commit to recover it:
git checkout -b <new-branch-name> <commit-hash>

7. Solving Performance Issues

Large repositories or frequent history rewrites can slow down Git operations.

  • Optimize Repository:
  • Clean up unnecessary files and optimize the repository:
git gc
  • Split Large Repositories:
  • If your repository is too large, consider splitting it into multiple smaller repositories using Git submodules or subtree.

8. Addressing Common Git Errors

Here are some common Git errors and how to resolve them:

  • “fatal: not a git repository (or any of the parent directories): .git”:
  • Make sure you’re in a Git repository or initialize a new one:
git init
  • “error: failed to push some refs”:
  • This error often occurs due to diverging branches. Fetch and merge the changes:
git pull origin <branch>
  • “merge: no commit found”:
  • This error occurs if you attempt to merge without a commit history. Make sure you have committed changes to merge.

Conclusion

In this final part of our Git learning series, we’ve covered essential strategies for debugging and troubleshooting common Git issues. By understanding how to undo changes, resolve conflicts, handle detached HEAD states, fix mistaken commits, restore deleted branches, recover lost commits, solve performance issues, and address common Git errors, you’ll be well-equipped to maintain a smooth and efficient development workflow.

Thank you for following along in our Git learning series! I hope you’ve found these lessons valuable and that they help you become a more proficient Git user. Happy learning , and may your version control be ever in your favor!

Explore All Content of Unboxing GIT Fundamentals

  1. Part 1 of 10: Introduction to Git and Version Control
  2. Part 2 of 10: Basic Git Commands and Operations
  3. Part 3 of 10: Branching and Merging in Git
  4. Part 4 of 10: Remote Repositories in Git
  5. Part 5 of 10: Collaboration Workflows in Git
  6. Part 6 of 10: Advanced Git Commands and Techniques
  7. Part 7 of 10: Git Best Practices for Repository Management
  8. Part 8 of 10: Exploring Git Workflows — Git Flow, GitHub Flow, and GitLab Flow
  9. Part 9 of 10: Integrating Git with CI/CD Pipelines on GitHub and GitLab
  10. Part 10 of 10: Debugging and Troubleshooting Common Git Issues

About Author: An experienced IT professional worked in major IT companies for more than 20+ years as Solution Architect with core expertise on DevOps and Cloud. To get trained in DevOps from experts like Anand visit https://www.svsitsolutions.in or contact in WhatsApp

Other Interesting Blogs from the Author:

# 10 Surprising Facts About Git That You Probably Didn’t Know

# Hidden Secrets of Git : That You Never Knew

# Learning Teaser — Git in a Few Hours: A Comprehensive Training Guide

# Cinematic Experience with GIT : Lights, Camera, Action for Your Code!

--

--