0

here's my problem: I have this development branch which I am supposed to get my new branches from it. I took this branch named descendingOrder and committed and pushed everything I wanted to do. Then I had a totally different and new task. so when I was in descendingOrder I used the command git checkout -b featureTest and made a new branch. but I had two mistakes here:

1- My last commit on descendingOrder was supposed to be on the new branch feature test. but I mistakenly committed it on descendingOrder.

2- The feature test branch was supposed to be taken from the development branch. but I mistakenly took it from descendingOrder.

Now I want to move back the feature test to development and also add the last local commit of descendingOrder at the first of the new featured branch.

I also saw this link but it didn't help much.

3
  • 2
    If you could describe with some ASCII the current branches graph (or add a screenshot if there is no confidential information) and also the expected one, it will be easier to understand and to answer to your question...
    – Philippe
    Commented Sep 2, 2020 at 8:47
  • 1
    Ensure you are on the feature test branch then rebase it onto development, removing the commits you don't want during the rebase. You can then hard reset the descendingOrder branch back one commit to remove the unwanted commit. Commented Sep 2, 2020 at 8:52
  • Why didn't the linked question help? It looks very similar to your problem.
    – knittl
    Commented Oct 24, 2022 at 10:22

1 Answer 1

0

There are multiple options which will all give you the same history eventually (with different commit hashes of course):

  1. There was only a single commit on descendingOrder. Nothing to do with feature test, it already contains the commit as first commit. You can delete or move back descendingOrder by one commit: git branch -f descendingOrder descendingOrder^

  2. There are already multiple commits on descendingOrder and feature test currently contains all of them. Rebase can help:

    git rebase --onto development 'feature test^' 'feature test'
    git branch -f descendingOrder descendingOrder^
    

    This will take the last commit of feature test copies it on top of development. Your feature test branch will then only contain this single commit (not in development). Afterwards, descendingOrder needs to be moved back one commit.

    If there are multiple commits on feature test that need to be rebased/moved, replace 'feature test^' with 'feature test~N' with N being the number of commits you want to copy.

    This option will work too if there was only a single commit on feature test, but it's more complicated than option 1.

  3. Use cherry-pick:

    git checkout -B 'feature test' development
    git cherry-pick descendingOrder
    git branch -f descendingOrder descendingOrder^
    

    This first destroys and recreates your branch at the last commit of development, then copies the last commit of descendingOrder. Again, descendingOrder needs to be moved back.

  4. Use an interactive rebase:

    git rebase -i development 'feature test'
    git branch -f descendingOrder descendingOrder^
    

    In the todo-list in the editor, remove all but the last commit (or the last N commits, if you want to keep multiple commits in the branch). After the rebase has complete, move back the descendingOrder branch.

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