A, git merge
1. Use
- Git checkout Master
- Git merge feature: git merge feature
Characteristics of 2.
- Only one conflict is handled
- A merge history has been introduced, and all merged commits are sorted from old to new by commit time
- All processes have more information, which may make it harder to find problems later
Second, the git rebase
1. Use
Like Git merge, git rebase is used to merge changes made in one branch into another branch.
- Git rebase master rebase the current branch feature relative to the branch master
- In case of conflict, the latest content of the Master branch and the content submitted for the first time of the feature branch are respectively compared.
- After we resolve the conflict, git rebase –continue is required to continue the rebase operation.
- After execution, there was another conflict. This time, it was compared with the second submission of feature branch, which meant that we needed to solve the conflict in the same place for many times.
Characteristics of 2.
- Changes where the current branch is pulled from the master branch
- There is no redundant merge history, and the commit order after the merge may not be the same as the commit time
- Conflicts in the same place can be resolved multiple times.
- Even more refreshingly, each commit point on the Master branch is a relatively independent, complete unit of functionality
3. Interactive mode
git rebase -i HEAD~4
Copy the code
Specifies the last four commits for the current branch to operate on.
The red box in the middle contains commands that can be used to process a commit. You can use a squash to squash all commits into a commit. After editing and saving the commit, you are prompted to edit the commit.
Git merge git rebase git rebase
- Rebase puts the commit of your current branch at the end of the common branch, so it’s called rebase. It’s like you’re pulling out the branch from the common branch.
- Merge merges the common branch with your current commit to form a new commit
Advantages and disadvantages:
- Git merge has the advantage of not destroying the commit record of the original branch code after the merge, but has the disadvantage of generating additional commit records and merging the two branches
- Git Rebase has the advantage that it can trace the commit record of the object branch to the target branch, forming a linear commit history record, which is more intuitive when reviewing
5. When to use Rebase
- Git rebase cannot be performed on a shared branch
-
- Since all the subsequent commits are new, other people who pull from the public branch need to merge again, resulting in a mixup of commit records
The diagram below:
conclusion
- Use Git merge to merge code on a common branch
- Git rebase is used to form a linear commit history when combining code into individual branches
Third, git cherry – pick
1. Basic use
- Git cherry-pick is used to merge partial commits from one branch into other branches
git checkout master
git cherry-pick <commitHash>
Copy the code
After using the above command, the commit will be at the top of the master’s list
2. Merge multiple commits
Git cherry-pick <hashA> <hashB> <hashB> // Merge all commits from A to B but not A git cherry-pick <hashA>^.. <hashB> // merge all commits from A to B, including ACopy the code
3. Conflicts arise later
If a conflict occurs after the cherry-pick command is executed, a conflict error is reported
git cherry-pick --continue // 1. Git cherry-pick --abort // 2. Git cherry-pick --quit // 3. Do not want to resolve the conflict, abandon the merge, and leave the situation as it is, do not go back to the previous operationCopy the code
4. Move to another code base
Git remote add target git://gitUrl // add a remote repository target git fetch targetCopy the code
5. Application scenarios
You want to merge something, but don’t want to include the whole branch. At this point, single commits are merged with cherry-pick
The resources
Git merge and rebase merge commands