Master git thoroughly (3)


1、 Transmission

Mastering git thoroughly (1)
Master git thoroughly (2)

2、 Branch basic operation

The branch is actuallyIt’s a pointerPoint to a commit commitFor each commit, the pointer moves to the latest commit position. Similar to string beads, each submission is like a string of beads, which are concatenated through the branch pointer. As shown in the figure below
Master git thoroughly (3)

implementgit initAfter the command, GIT will give the projectAutomatically create a blank main branch, i.eMaster branch
① View branches
Can passgit branchcommandView which branches exist in the current project, but this command can only view local branches,Unable to view remote branchesIf we want to see the remote branches, we need to bring-A parameter, i.egit branch -a

> git branch -a
* master
 remotes/origin/HEAD -> origin/master

The branch with an asterisk means yesCurrently activeBranch, remote branch toremotes/origin/start,The following is the branch name of the remote version library

② Create branch
branchMust be submitted at least onceA branch that has not been submitted cannot be passedgit branch -aCommand. We can go through itGit branch < branch name >Command to create a branch, which willBased on current branchThat is to say,The newly created branch carries all the commits of the current branch

//At present, in the master branch, execute the command to create branch-1
> git branch branch-1
//The branch-1 branch created has the same submission information as the master branch

Of course, we can specify the pointer position of the newly created branch, that is, as long as part of the submitted information is submitted, it can be passed through theGit branch < branch name > < commit ID >, then the submission information of the created branch,Only the first commit containing the current branch will be submitted to the part of < commit ID >

> git branch branch-2 c12ac1

By default, a new branch is created based on the current branch. We can also specify that a new branch is created based on an existing branch,Git branch < new branch name > < old branch name >For example:

//The branch-3 created will be based on the branch-2 branch
> git branch branch-3 branch-2

③ Switch branches
git branchAfter creating a branch with the command, it will not automatically switch from the current branch to the newly created branch. If we need to switch to other branches, we can use theGit checkout < branch name >Command to switch to the specified branch, such as:

//Switch to branch-a
> git checkout branch-a

Of course, checkout can also be doneCreate and switch to a new branchAnd that is to add the – b parameter, that isGit checkout - B < branch name >For example:

//Create and switch to branch-4
> git checkout -b branch-4

The checkout command can also be used through the–Orphan parameter, create aOrphan branchThis is a branch without any commit, so it cannot be viewed, also calledBlank branchFor example:

//Create a branch-5 branch without any submission information
> git checkout --orphan branch-5
Switched to a new branch 'branch-5'
> git branch

You can see that the newly created branch-5 orphan branch has not been viewed. It can only be viewed after it is submitted.
With the orphan branch, we canClear all committed records of a branchFor example:

//Create and switch to an orphan branch
> git checkout --orphan empty-branch
//At this point, all the files in the project are new to the orphan branch, which can be directly added to the staging area or deleted
> git add .
//Make an initial commit
> git commit -m 'init commit'
//Delete the branch to clear the submitted record, such as the master branch
> git branch -D master
//Change the name of the orphan branch to the one you just deleted
> git branch -m master
//Force synchronization to remote server
> git push -f origin master

It should be noted that when we switch branches, if the file we modify is just createdIt has not been submitted to the repositoryNo matter whether the file is modified in the workspace or the staging area,Can switch to any branchBecauseThe file has not been tracked by the repositoryIf we modify the documentIt has been submitted, i.eTracked by the repositoryNo matter whether the file is modified in the workspace or the staging area,Can’t switch to other branchesBut we can go through itGit checkout -- force < Branch >But this is not the caseThis will cause previous changes to be discarded

④ Delete branch
To delete a branch, we canGit branch - d < branch name >Command. It should be noted that when deleting a branch, you cannot delete yourself. That is to say,Unable to delete the currently active branch. After we create a new branch, if we haven’t made any Submission on this branch, we can delete it directly becauseNothing new has been submitted on the newly created branch, and the repository has not changed. However, if we have submitted new changes to the newly created branch, then there is something new on the new branch, then deleting the branch will prompt that there is something that has not been merged on the branch to be deleted and cannot be deleted. Of course, you can delete it through theGit branch - d < branch name >To force deletion.

//Currently on the master branch
//Branch-1 submitted new changes
> git branch -d branch-1
error: The branch 'branch-1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch-1'.
> git branch -D branch-1
Deleted branch branch-1 (was 091034a).

⑤ Restore branch
When a branch is deleted, only the pointer to the related submission is deleted, but the submitted object will still be left in the version library. Therefore, as long as we find the hash value when the branch is deleted, we can use theGit branch < branch name > < delete hash value of branch >Restore the deleted branch. For example, when the above branch is deleted, the hash value is 091034a

//Restore branch-1 branch
> git branch branch-1 091034a

Of course, we can work togethergit reflogFind the hash value when deleting the branch

3、 Merge branches

Branch merging is a very common operation. For example, when we develop new functions on the mainline branch and fix bugs on another branch, we need to merge bug repairs on the mainline. In addition to merging two branches, there is also merging on the same branch. For example, there are two developers developing on the mainline branch and one on the mainline branch After developer a submits the code, another developer B needs to synchronize the code submitted by developer a and merge the code submitted by developer a. We can go through itMerge commandalsoSpecify a branch name, yesMerges the specified branch into the current branch

//Switch to the master branch
> git checkout master
//Merge the branch-1 branch into the master branch
> git merge branch-1

git merge branch-1Equivalent togit merge branch-1 master, after mergeThe first parameter is the source branchThe second parameter is the target branch

Master git thoroughly (3)

As can be seen from the figure, branch-1 is forked from the second submission of the master branch. At the same time, the third submission is made on the master branch, and the fourth and fifth submission is made on the branch-1 branch. Then, the submission of branch-1 is merged into the master branch. In the process of merging,An additional merge submission was generated, which is the sixth submission. adoptgit logWe can see that these six submissions are in progress, and we can pass themgit log --graphView the graphical structure of the branch.

① What happens in the process of merging branches
When merging, GIT canMerge automatically. If two files are modified in the same file, butThe changes are differentThat is to say,Not the same line modifiedFor example, branch a modifies the first line, and branch B modifies the second line. For branch a, the content of the second line of the file is different from that of file B. for branch B, the content of the first line of the file is different from that of branch A. when the two branches are merged, the file There are differences between the first line and the second line, butAfter merging, there is only one version of the fileHow does git determine how the final synthesis should be displayed? Git isFind the bifurcation point of the two branchesTake bifurcation point as referenceSo the first line shows the modification of branch a, and the second line shows the modification of branch B. As shown in the figure.
Master git thoroughly (3)

As can be seen from the figure, if the bifurcation point is not used as a reference, the first line will not be displayed when mergingBranch a modifies the first linestillOriginal contentAnd the second line doesn’t know what to displayBranch B modifies the second linestillOriginal content

② Conflict
In the actual merge, two developers may modify the same line of code in the same file at the same time. Then git will have a merge conflict. Because these two modifications are the latest and the same line relative to the bifurcation point, GIT does not know which modification should be used at this time. At this time, we need to start the merge We need to resolve the conflict.

//Merge the commit from branch-1 branch to master branch
> git merge branch-1 master
Auto-merging foo.txt
CONFLICT (content): Merge conflict in foo.txt
Automatic merge failed; fix conflicts and then commit the result.
> git diff foo.txt
**diff --cc foo.txt**
**index 5f5fbe7,da8092b..0000000**
**\--- a/foo.txt**
**+++ b/foo.txt**
@@@ -1,3 -1,3 +1,7 @@@
++<<<<<<< HEAD
++>>>>>>> branch-1
> git add .
> git commit -m 'merge branch-1 to master'

There were conflicts during the merger,We need to remove the conflict flag, then add it to the temporary storage and submit it to complete the merge
Of course, after modifying the conflict file and adding it to the staging area, we can alsogit merge --continueCommand to continue the merge operation, the submit interactive window will pop up automatically. Enter the submit information and exit editing to complete the merge.

It should be noted that during the merge operation,No matter whether there is a conflict between the two branches after merging, an additional commit will be generatedHowever, if there is no conflict after the merger, thenThe merge operation is not interrupted and a merge commit is automatically addedIf there is a conflict after merging, the merge operation will be interrupted. You need to solve the conflict, add it to the staging area, and thengit commitperhapsGit merge — continue merge operation

③ Quick merge
Under normal circumstances, the merge operation is carried out, regardless of whether the automatic merge is successful or not,Will generate a commit to record the merge operation, so that we can track the development of the version library, but when we branch out from a certain point,Never submitted on this branchThen, when we merge on this branch, it becomes very simple. We just need to move the pointer of the branch once, and there is no need to generate a merge submission. We call this kind of submission fast merge submission. Fast merge submission is not conducive to version library tracking, so we can force fast merge to generate a merge submission by — on FF, such as:

> git merge --on-ff master branch-1

4、 Rebase variable base purification submission history

Because merge command will generate an additional submission about merge after merging, which will cause our branches to continuously bifurcate and merge, and the branch structure will become very complex. The function of rebase command is the same as mergeMerge the contents of a branch to the current branch (target branch), butRebase does not generate an additional commitInstead, take the branch you want to merge into the target branch(Basal branch)Based on, and thenReplay differential commit on target branchThat is to sayRebase places the commit on the target branch at the top of the branch
The principle of variable basis is very simple,Git will make the submission sequence we want to move reappear in the same order on the target branch, which is equivalent to making a copy of each original submission with the same modification set, the same author, date, and comment information.
① Smooth merge branches
Git rebase < source branch > < target branch >Similar to the merge command, the function isMerge the contents of the source branch into the target branch, but there will not be an additional merge commit. The source branch is actually the base branch, that is, theSource branch basedReplays the new commit on the target branch

> git checkout master
//Merge the bug branch into the master branch
> git rebase bug master

It should be noted that although the final result is to merge the new changes on the bug branch relative to the master branch into the master branch, when changing the base, it is necessaryBug Branch Based, i.eThe commit record on the bug branch remains unchanged, and then replay the commit record on the master branch, soThe submission ID on the master branch changesAt this time, the bug branch does not change, so its branch pointer is at the base point. At this time, compared with the master branch after changing the base, the bug branch does not commit,It is equivalent to creating a bug branch from the base pointAt this time, you only need to modify the master branch on the bug branchQuick mergeYou can make the pointer of the bug branch jump to the pointer of the master branch.

Or, first of allCancel the commit on the master branch relative to the location of the bug and master bifurcationAndTemporarily save them as patches and put them in the. Git / base directory, and thenUpdate the head of the master branch to the head of the bug branchAnd finallyApply the just saved patch to the master branch

//Switch to the bug branch
> git checkout bug
//Fast merge master branch content
> git merge master
//At this point, the bug branch pointer and the master become the same

When rebase, if there is a conflict:
git rebase –abortThat is to say, if the merge is abandoned, it will return to the state before rebase, and the previous submission will not be discarded;
git rebase –skipThe conflicting commit in the target branch is skipped, i.eKeep commit in base branch, moveIn addition to the current target branch of the commit that will cause a conflict, automatically resolve conflicts,After rebase, the target branch will not see the submission that will lead to conflict. Use it with caution
git rebase –continue It needs to be used with git addThat is, after the conflict is resolved, the final modification is added to the staging area, and then the rebase operation is continued through — continue, that is, the conflict is resolved manually.

② Copy committed to a branch
Git rebase startpoint endpoint -- onto < target branch >
firstSwitch to the source branch of the copy, find the submission interval to copy, and then specify the target branch through — onto. After executing the command, a free branch will be generated, which is the same as the final result we want, but it has not been copied to the target branch, and thenSwitch to the target branchAt this time, we will be prompted that the target branch has left a commit, that is, the commit that we copied, and then there will be the head position of the free branch, we willReset the branch pointer to the head of the free branch on the target branch

//Switch to master branch
> git checkout master
//Copy the commit of (5af873,89da14] on the master branch to the bug branch
> git rebase 5af873 89da14 --onto bug
//View the resulting free branches
> git branch
* (HEAD detached from 838de4f)
//Switch to target branch
> git checkout bug
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
 7a48d4c commit-4
 78c3b66 commit-3
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 7a48d4c
//You can see that git prompts that the current branch has left two submissions copied from the master, and the header is in 7a48d4c. Then reset the target branch pointer to 7a48d4c
> git reset --hard 7a48d4c

Of course, we can use the cherry pick command more flexibly to copy one or some items to a branch. We just need to find the commit ID that needs to be copied, and thenSwitch to the target branch to copy, executegit cherry-pick <commit-id1> <commit-id2> ...Just order

//Switch to the master branch
> git checkout master
//Copy the ee4dd5 commit to the master branch
> git cherry-pick ee4dd5
//Check whether the submission corresponding to ee4dd5 has been copied successfully
> git log

③ Merge multiple commit into a complete commit
Rebase provides a-I parameter, which allows us to enter the interactive operation interface,git rebase -i startPoint endPointFind the start point and end point to be merged into a submission, and then enter an interactive operation interface, such as:

> git rebase -i 5af873 89da14
pick 82a82f3 commit-3
pick 89da145 commit-4
//Now we can merge the two commits into one. For example, we can merge commit-4 into commit-3. Then we can change the pick in front of commit-4 to S. s means merge commit
pick 82a82f3 commit-3
s 89da145 commit-4
>: WQ exit edit mode
//Then, an interactive interface will pop up to modify the information after submitting and merging. By default, you can leave it unchanged
>: WQ exit edit mode
//Looking at the submission history, you can see that two submissions merged into one
> git log

Summary of the usage of [git] rebase
④ Modify a submission a long time ago
For the most recent submission, we cangit commit --amendCommand to modify, so we can modify the modification a long time ago through rebase command,git rebase -i startPoint

//Because the starting point cannot be obtained, the last submission of which submission to be modified can be found as the starting point
> git rebase -i 5af873
pick f42d811 commit-3
pick 07fe470 commit-5
//Change the pick before the submission to e, which means to edit
e f42d811 commit-3
pick 07fe470 commit-5
>: WQ exit edit mode
//Enter modify mode
> git commit --amend
//Save after modification
>: WQ exit
//At this time, the log pointer is only in the modified position, because rebase is not finished yet, so you need to continue
> git rebase --continue
//Looking at the log again, you can see that a submission long ago was modified

⑤ Multi branch variable basis
If there are three branches, we can apply the change of a branch relative to a branch to a base branch,Git rebase -- onto < base branch > < relative branch > < target branch >, i.eBased on base branch, and thenFind the change of the target branch relative to the relative branchAnd make a copy of the change, and thenApply it to the base branch, i.eReplay it on the base branch, and thenMove the target branch pointer to the position after replayThe execution process is as follows: first, check out and switch to the base branch, then apply the change, switch back to the target branch in check out, and move the pointer to the changed position, such as:
Master git thoroughly (3)
If you want to apply C8 and C9 to the master branch first, you cangit rebase --onto master server clientBased on the master branch, the command finds the changes of the client relative to the server common bifurcation point, that is, C8 and C9, and then applies them to the master branch, that is, the base branch is connected with the changes, and then moves the pointer of the client branch to the connected position, that is, C8 and C9
Master git thoroughly (3)
Detailed explanation of GIT branch rebase

⑥ Delete a commit or segment on a branch
If an error is found in the previous submission, we can delete it directly,Git rebase -- onto startpoint endpointThe startpoint here is not available, but the endpoint is available, soIt’s a front open back closed interval, which is based on the startpoint of the current branch and changes the submission of the current branch relative to the end point. That is to say, the submission after the end point is played from the startpoint. Therefore, it is equivalent to deleting the submission between the startpoint and the end point

//Delete the commit between (5af8735305f6] on the master branch
> git rebase --onto 5af873 5305f6 master

In the process of rebase, if there is a conflict, the rebase operation will also be terminated. After the conflict is resolved, it needs to be added to the temporary storage, but there is no need for git commit operation at this time, because the rebase operation will not produce a new submission, but directly through theGit rebase — continue rebase operationOr byGit rebase — abort the rebase operation

5、 Practical application of rebase variable basis

① Avoid diamond chain in multi person collaborative development
In the actual development,Not everyone will use rebase to purify submitted history, so there are often diamond chains. If a developer submits once and pushes it to the cloud, then another developer modifies it, butThey do not modify the same fileAnd then it passesgit pullPull down the content submitted by the last developer, due togit pullA merge operation will be performed and a merge submission will be automatically created, so the second developer will get a diamond chain and push it to the cloud. Then another developer will also make a modification,He did not modify the same documentEven if he’s usinggit pull --rebaseCommand, then the diamond chain generated by the previous two developers is still not disappearing. What should we do at this time? First usegit log --graphFind the submission ID of the fork and use theGit rebase - I < forked position submit ID >It means to change the base of the submission after the fork point, and then an interactive window will pop up. Since the submitted and modified files by three people are not the same file, no operation is needed at this time. You can clean up the submission history by directly exiting the editing of the interactive window.

//The third developer got the diamond chain created by the combination of the first and second developers
> git log --graph
//Find bifurcation point submit ID
> git rebase -i 5af873
//Open the interactive command window and exit directly without any operation
>: WQ exit edit mode
//Check again to see if the submission history has been purged
> git log --graph

If all three developers modify the same file, the change base operation willto replaySo the three submissions will be replayed on the current branch one by one, and they will be rotated one by one. Because the three people modify the same file, there will be two merge conflicts. Each time the conflict is resolved, the modified file will be added to the temporary storage area, and then executedgit rebase --continueAfter two conflicts have been resolved, execute againgit log --graphCheck to see if the submission history has been purged.

Note: of course, it can also be executed directly on the current branchgit rebaseCommand, without any parameters, will automatically change the base operation on the current branch, for simple can be used directlygit rebaseCommand purification, ifgit rebaseIf the command cannot be completely purified, you need to rebase step by step in the above way.

Recommended Today

MySQL partition

MySQL partition Advantages of partitioning Store more data To optimize the query, only scan one or more necessary partitions. For count () and sum (), only summarize the partition statistics For data that is out of date or does not need to be saved, partitioning is faster Data queries are distributed across multiple disks to […]