How to modify a file in your local git commit history with git rebase (with Vscode Ide)?
situation
-
I have a big file
src\build_llm_learn\p7_digitalsreeni_210_unet_segmentation_models\model_examine\model_exec.ipynb
in my local git commit history.
Its file size is 160mb.- (also another file
examine_main.ipynb
).
- (also another file
I want to manually go to each commit point,
to remove that file, or to modify the file to reduce its size,
for each of the commit point in the git history.
procedure
create a backup branch
- create a backup branch:
git branch backup-before-cleanup
git rebase
In vscode git panel, get the init commit hash.
Open git bash CLI.
git rebase to that point.
git rebase -i 75eedea9e6bf96a62ac7f2f8392de48b7660f8c5^
mark commits for editing
- An editor will open with a list of your last commits, something like this:
pick 75eedea First commit with the big file
pick b2c3d4e Second commit, more changes
pick c3d4e5f ...
pick 3c4d5e6 ...
pick 4d5e6f7 The last commit where I fixed the file
-
Change the word
pick
at the beginning of each line toedit
.
edit 75eedea First commit with the big file
edit b2c3d4e Second commit, more changes
edit c3d4e5f ...
edit 3c4d5e6 ...
edit 4d5e6f7 The last commit where I fixed the file
- Save the file and close the editor.
start editing in rebase
- Git will now drop you back at your very first commit in the list.
Now your current file system should have changed to contain all the old git committed project files at that point.
-
(note: it includes the .gitignored files you have, just normal git behavior)
- To remove that large file:
# Replace with the actual path to your file
git rm --cached path/to/your/large_file.ext
Then commit the change:
git commit --amend --no-edit
Then continue the rebase to go to next commit point:
git rebase --continue
-
example cli:
nsht@Azih MINGW64 /d/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models (main|REBASE 2/13) $ git rebase --continue Stopped at 128ca80... ^ You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue nsht@Azih MINGW64 /d/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models (main|REBASE 3/13) $
- using Vscode Ide to change the file instead
in your file system or vscode ide,
open that file, edit it, save it.in the vscode git panel.
stage the change. click commit.
(just like how you normally commit a file in vscode.)click continue (continue rebase).
continue editing in rebase
- after
git rebase --continue
you are now at the next commit. just repeat the above process.
dealing with merge problem
- At some point you will face a merge problem when you modified the file
CONFLICT (content): Merge conflict in src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models/model_exec.ipynb
-
example cli:
nsht@Azih MINGW64 /d/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models (main|REBASE 9/13) $ git rebase --continue Stopped at b9b9224... @minor @ready for train You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue nsht@Azih MINGW64 /d/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models (main|REBASE 10/13) $ git commit --amend --no-edit [detached HEAD 42118d6] @minor @ready for train Date: Fri Sep 19 20:04:50 2025 +0800 6 files changed, 671 insertions(+), 3741 deletions(-) create mode 100644 src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models/model_examine/examine_smp.ipynb nsht@Azih MINGW64 /d/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models (main|REBASE 10/13) $ git rebase --continue Auto-merging src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models/model_exec.ipynb CONFLICT (content): Merge conflict in src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models/model_exec.ipynb error: could not apply f8b9733... ^ @working weak hint: Resolve all conflicts manually, mark them as resolved with hint: "git add/rm <conflicted_files>", then run "git rebase --continue". hint: You can instead skip this commit: run "git rebase --skip". hint: To abort and get back to the state before "git rebase", run "git rebase --abort". hint: Disable this message with "git config advice.mergeConflict false" Could not apply f8b9733... ^ @working weak nsht@Azih MINGW64 /d/usp/uhpsj/study/build_llm_wsp/build_llm/src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models (main|REBASE 11/13) $
- You can choose to just click on the vscode git panel to deal with the merge, (just like how you deal with a branch merge everyday. )
-
Or, vscode git diff cant open the file, cuz it is too large.
- I chose to blindly accept the old file at that point of commit. so:
git checkout f8b9733 -- src/build_llm_learn/p7_digitalsreeni_210_unet_segmentation_models/model_exec.ipynb
but then unstage the file at vscode git panel.
and then open the file and manully change the file content.
then stage it again, commit it, continue rebase.
-
note:
- i mean, you can always just blindly accept a merge from any side.
and then unstage it.
and then overwrite the file completely with your desire content.
- Here I did discard the modified file from the previous rebase edit. As long as I know what the final file should look like. Then it doesnt matter.
- i mean, you can always just blindly accept a merge from any side.
and then unstage it.
and then overwrite the file completely with your desire content.
misc
answer with help from AI, tested.
- Here is my first AI prompt:
1.
its a remote lib
1.
i clone it
1.
i modify and commit things locally
1.
after 10 local commits
1.
i found one of the existing file was 100mb and i was editing and committing on it
1.
i change the code of that file, now its just 1mb.
1.
I now want to go back to the point where i first clone the repo
and find that file
remove that file from my local commit
and go to next commit , remove that large file , or some other large files
and go to next commit , remove that large file , or some other large files
and go to next commit , remove that large file , or some other large files
and until the lasted commit where i refactor that file to 1mb.
and keep just that 1mb file.
- PS: again, the markdown syntax i use is github style, it is incompatible with dev.to style. especially for the nested list. so the article structure will be unreadable.
Top comments (0)