How to "synchronize" a branch with master before working on it?

5

I have the following situation: I have a Git repository, and at one point I created a branch from master. I did not do much in this branch at the time, and then I made the master branch for several commits, and the code changed a lot.

Now I needed to resume the work on this branch and complete it, but it was complicated because the branch is really long overdue. This has two problems:

  • It's hard to implement back there when the code was in a way, if it's changed a lot now.

  • / li>

    The only solution I thought was to find a way to get this branch and move it to be the same as the branch master and redo its work. But it also does not look perfect, because after all I would lose what I had already started on this branch.

    In that sense how do I solve this? How can I get back to working on this branch if I have made the branch master a lot and now the code is very different?

        
  • asked by anonymous 18.07.2016 / 15:33

    2 answers

    5

    The most common is:

    • Create another branch from the updated MASTER;
    • Make git merge with your branch you want to continue;

    So you see if there is any conflict, check the code with the changes and have an updated branch.

        
    18.07.2016 / 15:37
    2

    First, save your branch (in case of any problem) during the following:

    git branch <branch_save> <branch>
    

    Try to change branch roots:

    git rebase master <branch>
    # OU
    # Se você quiser trocar ordem dos commits ou apagar alguns:
    git rebase -i master <branch>
    

    The branch root will be master. If you have conflicts, solve them and rebase --continue .

    At the end, you will have your work updated with master: the branch root will be master and the work will be adapted.

    To restore the initial situation in case of a problem:

    git rebase --abort # Se rebase em curso
    git checkout <branch>
    git reset --hard <branch_save>
    

    If you are satisfied with the result: delete the save:

    git branch -D <branch_save>
    
        
    31.07.2016 / 11:18