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

Asked

Viewed 870 times

2

I was working on a branch called task-1. I accidentally switched branches and made several modifications to the task-2 branch. How do I reverse the changes in the task-2 branch without committing and deleting everything that was done?

  • Where are these changes? You’ve committed or are on staging area?

  • I didn’t commit. You’re not staging.

  • 2

    So just switch to the right branch.

  • 4

    Can perform git stash, effect the checkout to the correct branch and apply with git stash apply.

  • 2

    Or else git stash pop, that cleans the stash

1 answer

0


If you:

  • It had modified files in a task-1 branch and
  • Went straight to the task-2 and kept changing these (and other) files thinking it was in the task-1
  • Wishes to maintain these changes in task-1 and remove them from task-2

You have the following options:

Back to task-1

Just go back to the task-1.

git checkout task-1

git will load all changes from task-2 to the task-1. By committing everything in task-1 and go back to task-2, will see that the branch task-2 will be intact

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

Still on task-2, use command:

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

Go to the 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 the 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 the branch task-1 and apply the diff file changes:

git checkout task-1
git apply arquivo_todas_alteracoes.diff

Browser other questions tagged

You are not signed in. Login or sign up in order to post.