Merger and redistribution achieve similar goals, but go through them in different ways. Both help to manage working with branches and collaborating with multiple people, but they cannot be replaced, and downgrading can be harmful if not done properly.
What’s up with a reboot?
Upgrading is very complicated, so it is important to understand how
git works under the hood if we are to make any sense of it.
Git stores your code as a series of edits. You can think of this as a chain working backwards. Each edit, or “commit”;, refers to the ID of the previous engagement and includes what has changed from the previous engagement. This chain is only stored on your computer; your Git client does not talk to any other Git clients unless it performs one
pullis really just
fetch + mergewith your local branch), and even then it only speaks with a shared remote repository.
Branches are more complicated. Git only stores one thing when it comes to branches: ID for the commitment at the end of the branch. This way you can think of them as the playing head of a turntable; you place the branch head in a certain position, and it goes back through the chain, “plays” your code and reaches a final version. When you commit, your local git client will automatically move the game head forward to the new engagement.
If you wanted to merge the function into a master, you would run:
git checkout master git merge feature
This creates a new merge, and if there are any conflicts you will need to resolve them manually. The
git merge the command moves the main game head to the new merge proposal and deletes the function game head, as it is no longer necessary.
This method of merging code presents three problems:
- There may be changes to the main branch that the function branch wants to include, especially if it takes a while to develop the function.
- Having to go through the merging process every time you want to work with branches is annoying.
- Commit history is messy, although this is largely an aesthetic problem.
Rebasing tries to solve these problems, with varying degrees of success. Revise changes where you started your branch. The entire branch is lifted and transported to the end of the current master branch, where it connects to the end. The main branch is left untouched and is free to continue accepting commitments.
In fact, the commitments are not movedhowever, because commitments are immutable. Rather, they are copied, resulting in new engagement IDs. The previous commitments are left, hidden in your Git files but will never be seen again, as the game head has moved elsewhere.
To perform this process from the command line, you would run:
git checkout feature git pull git rebase master
This opens the branch, drags the current changes to the master and then phases back the function branch to the master branch.
At this time, the code in the function branch is now more up to date, which is the only real function of
git rebase. Phasing does not merge branches, as it does not create merging obligations or move the master’s game head.
To merge after reboot performance, run:
git checkout master git merge feature
Which would look like this, with the master’s game head replacing the game head function:
So re-based does not solve the problem of managing mergers, because you still have to merge at the end to update the main branch. The real
merge however, the command at the end should go smoothly, as the reboot process requires you to “merge” into changes, which can still cause conflicts. And if you still want to continue working with your branch, you still need to “merge” changes.
Do not interrupt split branches
Do you remember how re-based copies undertake and leave a stranded game head? This is actually a big problem if you work with shared code. Let’s say you created the feature branch and push it to your repo so your colleagues can test it. That’s fine, but if one of them wanted to branch out from your branch, eventually going back, you’ll end up with this:
Your colleague’s feature2 branch now refers to an old chain of commitments. Your Git client has no way of knowing this because the feature2 branch is stored on your employee’s computer. They also have no way of knowing that you have stripped down until you make your changes.
When you rebuilt, it did not copy the feature2 function when it copied all commitments. Even if it could, it would not affect your employee’s local Git repo, which does everything out of sync. The solution here would be to rebase feature2 to the function in the place it should be, but it’s messy, even by Git standards, and this is just a very simple example.
The conclusion is, do not restart unless you are working locally.
When is rebasing better than merging?
If your branch will take a while to develop, it solves the “branch syndrome” problem, where your code is too outdated with the working master, and you need to update it to continue working. Overall, you should try to avoid this problem as much as possible, but rebooting can fix it when it occurs.
If you only make small, incremental, daily changes, you should instead work on a local main branch and use Pull Requests when you are ready to drive your changes. These use the model for current branches, created specifically to store your code before it is approved for merging.
But if you work a week and will stop making multiple pull requests and merge several times, you can work on your code a little longer, rebase locally for updates, and perform a pull request at the end to reduce the number of tests and talk to supervisors. Redistribution is primarily a local matter, so you can do it at your engineering branches without waiting for approval.
If no one else is dependent on your branch, you Can rebase before a branch is merged to make the engagement history clean and one-dimensional. But it can be argued that traditional merging, even if it is uglier, is easier to follow and troubleshoot, because merging obligations are completely non-destructive. In any case, the restart must be done just before your changes are merged, or you may encounter a Master update before your changes are approved.