Fundamentals 13 min read

Git Exception Handling Checklist: Solutions for Common Issues

This article provides a concise, step‑by‑step checklist of common Git exceptions—including file recovery, branch deletion, commit amendment, branch renaming, commit rollback, merge reversal, and branch restoration—along with the exact commands and explanations needed to resolve each situation efficiently.

政采云技术
政采云技术
政采云技术
Git Exception Handling Checklist: Solutions for Common Issues

Git is a distributed version control system that has become essential in front‑end, back‑end, and many other development scenarios.

This guide focuses on several frequently encountered Git exception cases and offers practical solutions with the exact commands required.

1. Local Working Directory File Recovery

If a file in the working directory is deleted, it can be restored instantly using Git:

Syntax: git checkout <filename/dirname> Command example: git checkout 1.js The command restores the deleted file to its previous state.

2. Deleting a Remote Branch and Removing Its Local Association

After a remote branch is deleted, the local branch must detach from the now‑nonexistent upstream. First delete the remote branch: git push origin --delete feature/test Then remove the upstream link:

Syntax: git branch --unset-upstream <branchname> Command example: git branch --unset-upstream feature/test Verify the removal with:

git branch -vv

3. Modifying the Commit Message of the Latest Commit

If the message of the most recent commit needs to be changed, use:

Syntax: git commit --amend After amending, view the log to confirm: git log --pretty=oneline Note: this rewrites the commit ID, so use with caution.

4. Renaming a Branch Seamlessly

When a branch name contains a typo, rename it without losing history:

Syntax: git branch -m <oldbranch> <newbranch> Command example:

git branch -m feature/stor-13711 feature/story-13711

5. Reverting Commits

Three common rollback scenarios are covered:

Undo a commit that has already been staged.

Replace a commit with new changes.

Discard a commit that introduced wrong files.

Soft reset (keep changes staged): git reset --soft HEAD~1 Mixed reset (unstage changes): git reset --mixed HEAD~1 Hard reset (discard changes):

git reset --hard HEAD~1

6. Reverting a Merged Branch

To undo an unwanted merge, you can use either git reset (which removes the merge commit) or git revert (which adds a new commit that undoes the merge). Example of revert:

Syntax: git revert <commit-id> Command example:

git revert 700920

7. Restoring an Accidentally Deleted Local Branch

Use git reflog to locate the commit where the branch existed, then recreate it:

Syntax: git checkout -b <branch-name> <commit-id> Command example:

git checkout -b feature/delete HEAD@{2}

8. Identifying Which Branch Contains a Specific Commit

When a problematic commit has been merged into multiple branches, find the branches that contain it with:

Syntax: git branch --contains <commit-id> Command example:

git branch --contains 700920

Summary

The article presents a practical checklist of common Git exception scenarios and their corresponding command‑line solutions, helping developers avoid errors and recover quickly. Understanding the underlying Git commands behind GUI tools is essential for reliable version‑control workflows.

References

Git error collection and fixes (https://www.edureka.co/blog/common-git-mistakes/#pushed)

Git .gitignore syntax (https://www.jianshu.com/p/ea6341224e89)

git reset and git revert (https://juejin.im/post/5b0e5adc6fb9a009d82e4f20)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Exception Handlingsoftware developmentcommand-lineTutorial
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.