0

I have this situation:

  • The main branch is Develop
  • I created a new branch from Develop called Task1
  • I made changes in Task1 and made a commit
  • I made further changes in Task1 and made another commit
  • It turned out that the task should not be called Task1 (for some reasons). A subtask called Task2 had to be created
  • Task2 was created from Task1 branch (in Task2 needed changes were made in Task1)
  • In Task2, I made further code changes

Now I would like to delete Task1 branch from the history

Changes made in Task1 must remain in Task2

Now is:

Develop - Task1 : (commit1, commit2) - Task2 : (commit1, commit2 etc.)

It should look like this:

Develop - Task2 : (commit1, commit2, commit3 etc.)

EDIT:

Task1 with commits must be removed but changes made remain in Task2

  • 2
    Possible duplicate of [How do I delete a Git branch locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely) – Liam May 21 '19 at 09:51

1 Answers1

2

A branch is just a pointer to a commit, so if Task2 is ahead or at the same level of Task1, you can just delete Task1:

# delete the branch locally
git branch -d Task1
# delete the branch remotely (if you remote is not called origin, change that with your remote name)
git push origin :Task1

The commits accessible from Task1 will still be in Task2.

padawin
  • 4,230
  • 15
  • 19
  • This will only delete the local branch – Liam May 21 '19 at 09:51
  • Yes, there is no mentioned that the branch is pushed. I add this case though, in case. – padawin May 21 '19 at 09:52
  • Is it possible to move commits from `Task1` to `Task2`? –  May 21 '19 at 09:57
  • Run `git branch --contains lastCommitOfTask1`, if `Task2` is displayed you have nothing to do, every commits of `Task1` will be accessible from `Task2`, otherwise you can rebase or cherry-pick the missing commits in `Task2`. – padawin May 21 '19 at 09:59
  • 1
    Yes @DevNet read about [cherry picking](https://git-scm.com/docs/git-cherry-pick). Please avoid asking multiple questions. Padwin has answered your question. If you want to ask more questions please use the ask a question button – Liam May 21 '19 at 10:00
  • Only that you change history would be useful to those commits –  May 21 '19 at 10:01
  • Can you rephase that please? – padawin May 21 '19 at 10:03