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?
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?
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.
git rebase -i A
B
to edit.B
.git add file
(where file is the file you want to add).git commit --amend
. Fix the commit message and commitgit rebase --continue
to get the rest of the late commits. git push --force
. 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.
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.`