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 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 -vv3. 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-137115. 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~16. 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 7009207. 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 700920Summary
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)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
政采云技术
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
