首页 > 解决方案 > Git: add file to old commit being parent of a new branch already merged back?

问题描述

given the following git log, how do i add a file (which never existed in any commit) to commit B, so that this new file will appear in every other commit that A?

A --> B -----> D ------> F
       \--> C --> E --/

In my attempts with git rebase --onto i did not succeed in not having to essentially duplicate every commit amending the contents but i am sure i am not approaching it using the full power of git. Any help?

Note: remote is fine to be forced, or even deleted and pushed again, that's not a worry.

Thanks a lot

标签: gitgit-branchgit-commitgit-rebase

解决方案


The end result will be that : duplicating all the commits.

About the ways to do it, though, you can use git rebase with some options :

# -i for --interactive
# -r for --rebase-merges
git rebase -i -r A

git will open a text editor for you, asking you to describe what actions you want to take on each commit.

In that file :

  • find the line mentioning commit B
  • change the action from pick to edit
  • save and exit

git will start applying the script you instructed him to, and will pause after reaching B.

At this point, you can add the extra file :

  • git add the file you want
  • git commit --amend to add it to B
  • git rebase --continue to proceed with the rebase

This should rewrite all the commits after B.

You can check your local history, and the content of your commits,
if you are satisfied with the result you can push --force.


推荐阅读