-
Notifications
You must be signed in to change notification settings - Fork 2
Rebasing In General
Rebasing VS Merge? What's better?
Choosing between git rebase and git merge remains one of the most discussed topics in the dev world, in Smartlogic land this can come down to what the client wants but most of the time you can make either work. Some may say that you should always use merging, some may say that rebasing is a more correct way to do things. There is no right or wrong way of using these two commands. It mainly depends on the user and the workflow.
Here are the steps to follow while rebasing a branch, You should receive the latest changes from a remote git repository. Thus the first step is running git fetch:
git fetch
The second step is running git rebase. Rebase is a Git command which is used to integrate changes from one branch into another. The following command rebase the current branch from master (or choose any other branch like develop, suppose, the name of remote is origin, which is by default):
git rebase origin/master
After git rebase, conflicts may occur and there could be MANY, so many in fact you can find yourself stuck in a little place I like to call "git hell". Anyways tho you should resolve them and add your changes by running git add command with the play tag aka -p:
git add -p .
IMPORTANT: Do not run git commit after git add -p .\
Instead you must run git rebase with the --continue option:
git rebase --continue
If you're stuck in git hell tho and wanna start over run the abort command:
git rebase --abort
The final step is git push (forced). This command uploads local repository content to a remote repository. To do that, run the command below:
git push origin HEAD -f
Avoid doing this tho, as you can upset alot of folks by accidently overriding code. --force that is the same as -f overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch.
Here is an alternative and safer way to push your changes:
git push --force-with-lease origin HEAD
--force-with-lease is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's work by force pushing.
Rebasing vs Merging
Rebasing and merging are both used to integrate changes from one branch into another differently. They are both used in different scenarios. If you need to update a feature branch, always choose to rebase for maintaining the branch history clean. It keeps the commit history out of the branch, contrary to the git merge command, which is its alternative. While, for individuals, rebasing is not recommended in the case of feature branch because when you share it with other developers, the process may create conflicting repositories. Once you need to put the branch changes into master, use merging. If you use merging too liberally, it will mess up git log and make difficult to understand the history. Merging preserves history whereas rebasing rewrites it. If you need to see the history absolutely the same as it happened, then use merge.
🏳️🌈🦄 Sharing is caring. 🦄🏳️🌈
TODO: can we recreate the tag system here roughly with sections about certain subjects?