Git Submodule Merge History Lost

There’s a Git repository locally where submodules were in temporary branches during the initial pull. I made commits on that temporary branch, and then switched back to the main branch. However, those committed changes seem to be lost and cannot be found within the main branch. I also can’t find any record of that temporary branch.

Solutions

After committing to a temporary branch within the Git submodule and switching back to the main branch, these commits are no longer found. This situation can be resolved by following the steps below:

  1. Verify Commit History: Navigate to the submodule directory and use reflog to find the lost commits.
  2. Create a New Branch to Save Commits: Create a new branch based on the lost commit.
  3. Merge or Cherry-Pick Commits to Main: Integrate the code into the main branch.

Here are the specific steps:

# Enter the submodule directory
cd path/to/your/submodule

# View reflog records all HEAD changes (including commits not associated with branches)
git reflog

PS F:\dev\notebook\scripts\hugo-content-suite> git reflog
de05175 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: checkout: moving from c8d070651310e90d283cb64d98da088c5fe05e73 to main
c8d0706 HEAD@{1}: commit: feat: Add Markdown tag symbol usage documentation, provide detailed syntax examples and effect display
48250f5 HEAD@{2}: commit: feat: Remove article translation preview function, simplify the translation process
b8280b6 HEAD@{3}: commit: feat: Add absolute path acquisition function, support converting relative paths to absolute paths
92c354b HEAD@{4}: commit: fix: Fix article scanning logic, ensure scanning using absolute paths
de05175 (HEAD -> main, origin/main, origin/HEAD) HEAD@{5}: checkout: moving from main to de05175d4ec0828e3ae95d726b09dfff18f67a23
de05175 (HEAD -> main, origin/main, origin/HEAD) HEAD@{6}: clone: from https://cnb.cool/ttf248/hugo-content-suite.git

# Create a new branch based on the lost commit (e.g., using 456def commit)
git checkout -b saved-work 456def

# Switch back to the main branch
git checkout main

# Merge the saved work into main (or use cherry-pick to select specific commits)
git merge saved-work
# Or
git cherry-pick 456def

# Return to the parent project directory and commit submodule updates
cd ..
git add path/to/your/submodule
git commit -m "Update submodule to include new changes"

Key Operational Instructions

  1. git reflog: Displays the entire history of HEAD, including commits not associated with any branch.
  2. git checkout -b: Creates a new branch from any commit and saves your work.
  3. git merge/cherry-pick: Integrates saved commits into the target branch.

If the reflog doesn’t contain records, you may need to try using git fsck --lost-found to find orphaned commits, but this is a rare occurrence.

Licensed under CC BY-NC-SA 4.0
Last updated on Jun 02, 2025 20:54
A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy