πŸ“œ ⬆️ ⬇️

Git Rebase Usage Guidelines

Rebase is one of two ways to merge changes made in one branch with another. Beginners and even experienced git users sometimes feel reluctant to use it, because they don’t see any reason to master another way to merge changes when they already have a good command of the merge operation. In this article I would like to examine in detail the theory and practice of using rebase.

Theory


So let's refresh the theoretical knowledge of what rebase is. To begin with briefly - you have two branches - master and feature , both local, feature was created from master in state A and contains commits C, D and E. In branch master after separating from it feature was made 1 commit B .



After applying the rebase master operation in the feature branch, the commit tree will look like this:
')


Please note that commits C ', D' and E 'are not equal to C, D and E, they have other hashes, but the changes (deltas) they carry are ideally the same. The difference in commits is due to the fact that they have a different base (in the first case - A, in the second - B), the differences in the deltas, if any, are due to the resolution of conflict situations that occurred during a rebase. More on this in more detail.

This state has one important advantage over the first one. When merging a branch of a feature into a master, a branch can be merged by fast-forward, which eliminates the occurrence of conflicts when performing this operation, in addition, the code in the branch of feature is more relevant because it takes into account changes made master in commit B.

The rebase process is detailed


Let's now understand the mechanics of this process, how exactly tree 1 turned into tree 2?

Let me remind you that before rebase you are in the feature branch, that is, your HEAD looks at the feature pointer, which in turn looks at commit E. You give the master identifier to the command as an argument:

git rebase master

git β€” . A. HEAD git , A , Ξ”AC. master. , C', C' = B + Ξ”AC. master feature , , HEAD (C'), Β« Β» (detached HEAD).


C', git β€” Ξ”CD. , C' . rebase ( , git status , detached HEAD). , Ξ”CD ( ) ( stage-):


:

1. rebase

git rebase --abort

HEAD, feature, ( ) .

2. merge-tool', , git add %filename%. , rebase-

git rebase --continue

, , D' rebase , .

3. , B D , «» B, git rebase --continue, , . D',

git rebase --skip

Ξ”DE E', feature E', HEAD feature β€” , , rebase . C, D E .


, rebase-, (Author), , rebase (Commiter):

commit 0244215614ce6886c9e7d75755601f94b8e19729
Author:     sloot69 <***@****.com>
AuthorDate: Mon Nov 26 13:19:08 2012 +0400
Commit:     Alex <***@****.com>
CommitDate: Mon Nov 26 13:33:27 2012 +0400


β€” rebase


, : master, origin/master, feature origin/feature. rebase origin-, feature origin/feature, feature master.

Rebase origin-


rebase, . , , , - merge. :



β€” 3 master . 1 . . git push origin master, , . (2 3) git pull origin master, merge- . git push . git pull, merge-. , git push origin master. github, network, :

6 ( ), , , , . , , , :



. rebase?
git pull origin master git pull --rebase origin master, , C, D E origin/master, git push origin master. :



, «» merge- , C C'. C , C'. git pull --rebase, .

, rebase


rebase- master . , rebase . , merge, (master). rebase . , ? push , fast-forward, , , :


, :

git push origin feature --force

Force- feature origin , .

! , force-push , . , . , origin-. , β€” . β€” , . push, origin. , .

, pull, push. feature origin/feature . git pull origin feature, merge. , . :

git pull --rebase origin feature

feature .
git pull --rebase origin feature β€” , rebase , fast-forward.

master


rebase. master git merge feature. , rebase- master fast-forward, .

, β€” merge fast-forward , , , . β€” merge --no-ff. merge-, master, β€” feature. , fast-forward () c merge-, --no-ff ():

, , merge- . , .


, , .

git rebase , , , merge- , rebase ( -, ) , , , git pull --rebase.

. β€” master , . , rebase- . .

, merge rebase . , , . , . rebase-, . , , rebase .

PS. , , .

Source: https://habr.com/ru/post/161009/


All Articles