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

Asked

Viewed 3,965 times

5

I’m in the following situation: I have a Git repository, and at a certain point I created a branch from master. I didn’t do much in that branch at the time, and then I advanced the master branch through several commits, and the code changed a lot.

Now I needed to resume work on this branch and complete it, only it got complicated because the branch is really far behind. There are two problems:

  1. It gets hard to implement back there when the code was one way, if now changed a lot.

  2. Even if I implemented what I wanted based on that old code, then to match what’s in the master branch I have no idea what it would be like, because it turns out it would be very different code.

The only solution I could think of was to find a way to take this branch and advance it to look like the master branch and redo its work. But it doesn’t seem perfect either, because after all I would miss what had already started in this branch.

In that sense how do I fix it? How can I get back to work on that branch if I’ve advanced too far to master branch and now the code is very different?

  • It is not an issue properly classified as software project (design software). Removing this tag will make it easier to locate issues.

2 answers

5

The most common is:

  • Create another branch from the updated MASTER;
  • Do the git merge with your branch that wants to continue;

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

2

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

git branch <branch_save> <branch>

Try switching Rais from branch:

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

The Rais of the branch will be master. If you have conflicts, resolve them and rebase --continue.

In the end, you will have your work updated with master: the sera master branch Rais 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 safeguard:

git branch -D <branch_save>

Browser other questions tagged

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