I made changes to the wrong branch, how to reverse the changes in this branch without deleting what was done?

2

I was working on a branch called task-1. Unintentionally, I changed the branch and made several modifications to the branch called task-2. How do I reverse the changes in branch task-2 without having to commit and delete everything that was done?

    
asked by anonymous 20.11.2018 / 17:28

1 answer

0

If you:

  • I had modified files in a task-1 branch and
  • You went straight to task-2 and kept changing these (and other) files thinking you were in task-1
  • Do you want to keep these changes in task-1 and remove them from task-2

You have the following options:

Return to task-1

Simply go back to task-1 .

git checkout task-1

git will load all changes from task-2 to task-1 . When you commit everything in task-1 and return to task-2 , you will see that the task-2 branch is intact

Save task-2 changes and apply them to task-1

Still in task-2, use the command:

git stash -u # o -u inclui os arquivos que criou, também

Go to branch task-1 and apply the command to download the changes:

git checkout task-1
git stash pop

Use git diff

In branch task-2 you can create a file with all changes:

git diff HEAD >> arquivo_todas_alteracoes.diff

Clear branch modifications:

git reset HEAD . # mover todas alterações do stage
git clean -f -d  # limpar todas as alterações

Go to branch task-1 and apply changes to diff file:

git checkout task-1
git apply arquivo_todas_alteracoes.diff
    
14.12.2018 / 19:47