2

I have three branch on my git tree. master contains validated source version, develop contains staging versions, then I have some feature branches.

For now, I have the current strucutre; The numbers represents some individual commit hashes for simplicity.

master              develop     feature
  o----------o----------o----------o
  1          2          3          4

Commit 2 and 3 come from a feature I merged on the develop branch. I then started working on a new feature branch.

I need to rewrite what was done in commit 2 and 3. For this, I need to branch my current work on new-feature from master and re-build the merged branch I had containing commits 2 and 3.

Here is what my git repo tree should look like.

[What I want]
master/develop
      o 1
      |                rewrite-feature
      +----------o-----------o
      |          2           3
      |
      |       feature
      \----------o
                 4

master and develop are pushed. feature is a local branch.

I am the only one working on this project, so I can mess up with the origin if needed without impacting anyone.

I'm really not sure of what I should do, specialy regarding to the pushed master and develop branches.

I think I should rebase the feature branch on commit 1. But I don't understand how to deal with those commits 2 and 3 without messing with the public repository.

0

Is it really important to rewrite the history? I mean, I myself like clean history, but in the end what really matters is the state of the repo not its history.

If it's not that important, just do the fix like any others: create a new feature, fix, commit, push, merge in develop, merge in master. Then rebase the other features on develop.

I don't know if you'll have the graph you want, but if it's really important to fix the history or you just want to:

Rename develop and the feature branch as something else as backup.

  • create a new branch "Master2", rebase interactively and amend the commits.
  • Force push on Master2 on Master
  • Create develop from master.
  • Create featureA. Rebase featureA on oldFeature.
  • Check that everything is OK
  • Delete old branhces
0

Do not rewrite public history.

The easiest way would be to revert develop to master as a new commit. A simple git checkout master -- . will stage changes reverting commits 2 and 3. Commit those changes to develop and note the commit Id of master that you are reverting too in the commit message. This gives you a history similar to:

master                          feature
  o----------o----------o----------o
  1          2          3\         4
                          \
                           \    develop
                            \------o
                                   5
                                   ^
                                   |
        Reverts commits 2 and 3 ---+

If the feature branch is a private tree (it has not been pushed) then just rebase the feature branch on to develop.

If the feature branch has been pushed, then just do a merge from develop into feature, and don't worry about the commit history. Clean history is nice, but not a requirement. Once branches have been pushed, merge commits are bound to happen. Don't sweat this too much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.