How to Use Git Merge [the Correct Way]
5281 단어 Git
At some point, a piece of code will reach a state where you'll want to integrate it with the rest of the project. This is where the git merge command comes in.
Preparing to Merge
Let's assume that we want to merge branch hotfix into your master branch.
Before we start, how to make sure that you are ready to merge your changes?
1. Check if your local repository is up to date with the latest changes from your remote server with a
git fetch
.2. Once the fetch is completed
git checkout master
.3. Ensure the master branch has the latest updates by executing
git pull
.4. Checkout to the branch that should receive the changes, in our case that is master.
Merging
Once the preparations are completed, you can start the merge with
git merge hotfix
command.Fast Forward Merge
A fast-forward merge can occur when there is a linear path between branches that we want to merge. If a master has not diverged, instead of creating a new commit, it will just point master to the latest commit of the hotfix branch. All commits from hotfix branch are now available in master.
However, a fast-forward merge is not possible if the branches have diverged. In this case, you want to use a Three-way merge.
Three-Way Merge
When there is not a linear path to the target branch, Git has no choice but to combine them via a three-way merge. This merge uses an extra commit to tie together the two branches.
Test this out! Create your own project with an RSpec test branch and at the same time edit the Controller tests in master. Now, try to merge.
How to Deal With Merge Conflicts
A merge conflict occurs when two branches you're trying to merge both changed the same part of the same file, Git won't be able to figure out which version to use.
For example, if the file
example.rb
was edited on the same lines in different branches of the same Git repository or if the file was deleted, you will get a merge conflict error when you try to merge these branches. Before you can continue, the merge conflict has to be resolved with a new commit.Merge conflicts will only occur in the event of a 3-way merge.
Generate a list of the files which need to be resolved:
git status
2. # On branch master
# You have unmerged paths.
# (fix conflicts and run "git commit")
# Unmerged paths:
# (use "git add ..." to mark resolution)
# both modified: example.rb
# no changes added to commit (use "git add" and/or "git commit -a")
<<<<<<<
- Conflict marker, the conflict starts after this line. =======
- Divides your changes from the changes in the other branch. >>>>>>>
- End of the conflicted lines.
<<<<<<< HEAD(master)
conflicted text from HEAD(master)
=======
conflicted text from hotfix
>>>>>>> hotfix
git add
command on the conflicted files to tell Git they're resolved. git commit
to generate the merge commit. This article is originally published on Kolosek Blog .
Reference
이 문제에 관하여(How to Use Git Merge [the Correct Way]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Kolosek/items/0c8a8943a10a7c6ac6f7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)