You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何删除旧commits?保留代码且起始提交为2年前版本

Trim Old Git Commits (Older Than 2 Years) While Preserving Current Code

Got it, let's break this down clearly. You've got a 5-year-old project, and you want to strip out all commits older than 2 years, make the 2-year-old version your first commit in the new history, and keep your current code exactly as it is. Here are two solid approaches depending on your needs:

Approach 1: Keep Commits From the Last 2 Years (Trim Only Older History)

This method preserves all commits made in the last 2 years, but deletes everything before that, making the 2-year-old state your initial commit.

  • First, back up your repo!
    Always do this before rewriting history—you don't want to lose work. Clone a copy to a safe location:

    git clone your-repository-url project-backup
    
  • Find the hash of your 2-year-old commit
    Run this command to locate the most recent commit that's older than 2 years (this will be the starting point of your new history):

    git log --before="2 years ago" --oneline | tail -1
    

    Note down the commit hash (let's call it <OLD_COMMIT_HASH>).

  • Rewrite history to remove older commits
    Use interactive rebase from the root of your history to drop all commits before <OLD_COMMIT_HASH>:

    git rebase -i --root <OLD_COMMIT_HASH>
    

    A text editor will open listing all commits from the start. For every commit before <OLD_COMMIT_HASH>, change the word pick to drop. Save and close the editor.

    If you hit conflicts during rebase, resolve them, then run git rebase --continue until it finishes.

  • Push the rewritten history to remote
    Since you've rewritten history, you'll need to force-push. First, make sure no one else is working on the branch:

    git push -f origin main  # Replace "main" with your branch name if needed
    

Approach 2: Simplify to Two Commits (2-Year-Old State + Current Code)

If you don't need the intermediate commits from the last 2 years and just want a clean history with two points (the 2-year-old baseline and your current code), use this method:

  • Backup your repo (same as the first step above—don't skip this!)

  • Find the 2-year-old commit hash (same as the second step above, get <OLD_COMMIT_HASH>)

  • Create a new branch based on the 2-year-old commit

    git checkout -b clean-history <OLD_COMMIT_HASH>
    
  • Overwrite with your current code
    Pull all files from your main branch into this new branch:

    git checkout main -- .
    
  • Commit the current code
    Stage all changes and commit with a clear message:

    git add -A
    git commit -m "Preserve current code, base history on 2-year-old version"
    
  • Update your remote repo
    Set this new branch as the default on your remote platform (GitHub/GitLab/etc.), then force-push:

    git push -f origin clean-history
    

Important Notes

  • Communicate with your team: If others are working on this repo, make sure everyone knows you're rewriting history—they'll need to rebase their local branches or clone the updated repo.
  • Tags and branches: If you have tags or other branches pointing to old commits, you'll need to update or delete them to avoid confusion.

内容的提问来源于stack exchange,提问作者cianos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:49:59