January
January

Reputation: 17090

Clean up a messed branch with git

I seem to have messed up a development branch of a small project beyond recognition. What I would like to achieve right now is tell git the following: hey git. Here is the branch called X. And now here I am in the branch called Y, which is based on X. Please see what the differences are, commit them in a single commit and then update the origin as well.

Background: the project sits on a gitlab repository. There is a development branch called development (appropriately), and I am modifying it (branch, say, january_dev). Now due to unlucky circumstances which can be best described "I have not the faintest idea what I am doing" I have messed the history of that branch.

A while ago I have rebased my january_dev on development, meaning that at the present all but one commits from the development branch are in my branch as well.

At present, attempts of rebasing the branch on development end up in replaying the whole history of january_dev commits and it ends up as a mess, as I have working quite a lot, going back and forth between concepts etc. So, basically, I don't feel confident in resolving the conflicts as I go. All I want is to have the status quo, rebased on "development" which should, in theory, be pretty easy, as there is just two lines of code in a file I haven't even touched that need to go from development to my branch.

What I know I could do, and I think that it would work, would be the following:

  1. Create a new branch based on the development branch. Call it january_dev2: git checkout -b january_dev2 && git push
  2. Copy the files I have been working on from january_dev. There is just a couple of them.
  3. git commit -a -m 'a new beginning'
  4. git push
  5. Rename january_dev2 to january_dev: git branch -m january_dev2 january_dev ; git push origin january_dev ; git push origin -u january_dev ; git push origin --delete january_dev2

I would then loose the whole history of my commits, but I don't care and don't need them.

What do you think?

Upvotes: 0

Views: 30

Answers (1)

kirelagin
kirelagin

Reputation: 13616

Here is the branch called X. And now here I am in the branch called Y, which is based on X. Please see what the differences are, commit them in a single commit and then update the origin as well.

  1. git switch Y – switch to the branch that you want to save (you already did this, but just in case).
  2. git switch -c tmp – create a new temporary branch called tmp just in case.
  3. git reset X – point your tmp branch to the same commit as X while preserving the working tree from branch Y.
  4. git add . && git commit – to record all changes between X and Y in a single commit.

So now your X and Y branches remain in the same state as before, but, in addition, you have a new branch called tmp that is essentially X + one commit that contains the difference between X and Y.

It is not clear from your description from your description what you want to push where, but, depending on your needs, you can now do either git branch -f X or git branch -f Y to change, correspondingly, your X or Y branch to be the same as tmp. And then you just switch to it as always and push wherever you want. After you are done, you can git branch -D tmp.

Upvotes: 1

Related Questions