0

Consider I have A, B , C , D commits and push them on remote repo.

After committing D I find there is a file that should be in B commit.

How can I edit B commit and add the missing file?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
hd.
  • 17,596
  • 46
  • 115
  • 165
  • 2
    Possible duplicate of [How to modify a specified commit in git?](http://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit-in-git) – Chris Martin Jan 23 '16 at 10:08

2 Answers2

1

This will work assuming that no one else has pulled the code from remote. If that has happened, your history is no longer yours and this is a bad idea.

  1. Make sure your repo is clean.
  2. git rebase -i A
  3. In your editor, change the entry for B to edit.
  4. Start the rebase by closing your editor. git will stop at B.
  5. git add file (where file is the file you want to add).
  6. git commit --amend. Fix the commit message and commit
  7. git rebase --continue to get the rest of the late commits.
  8. git push --force.
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
0

You can use squash which is an interactive rebase. You just have to keep in mind what is the result of a rebase. Anyone who have a copy of the branch will have to delete it and fetch it again since your history was updated.

Once you are done re-writing history you will have to delete the remote branch or push using the -f to force the push.


In order to do a git squash follow those steps:

// X is the number of commits you wish to squash
git rebase -i HEAD~X

Once you squash your commits - choose the e for editing the content of the desired commit, make your changes and commit it. In your case add the missing file to the B commit.

enter image description here


You also have the --root flag in case you need it

try: git rebase -i --root

--root

Rebase all commits reachable from <branch>, instead of limiting them with
an <upstream>.

This allows you to rebase the root commit(s) on a branch.  
When used with --onto, it will skip changes already contained in `<newbase>`   
(instead of `<upstream>`) whereas without --onto it will operate on every 
change. When used together with both --onto and --preserve-merges, all root 
commits will be rewritten to have `<newbase>` as parent instead.`
CodeWizard
  • 128,036
  • 21
  • 144
  • 167