Isolating into different branches is a crucial practice for any serious developer. By separating each feature, bugfix or working experiment you will avoid a lot of problems and keep your development branches clean. features 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 command comes in. git merge Preparing to Merge Let's assume that we want to into your . merge branch hotfix master branch Before we start, how to make sure that you are ready to merge your changes? Check if your local repository is up to date with the latest changes from your remote server with a . `git fetch` Once the fetch is completed . `git checkout master` Ensure the master branch has the latest updates by executing . `git pull` Checkout to the branch that should receive the changes, in our case that is master. Merging Once the are completed, you can start the merge with command. preparations `git merge hotfix` Fast Forward Merge A can occur when there is a linear path between that we want to merge. If a master has , 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. fast-forward merge branches not diverged However, a is not possible if the branches have diverged. In this case, you want to use a . fast-forward merge 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 . This merge uses an extra commit to tie together the two branches. three-way merge Test this out! Create your own project with an branch and at the same time edit the in master. Now, try to merge. RSpec test Controller tests How to Deal With Merge Conflicts A 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. merge conflict For example, if the file was edited on the same lines in different branches of the same Git repository or if the file was deleted, you will get a when you try to merge these branches. Before you can continue, the merge conflict has to be resolved with a . `example.rb` merge conflict error new commit will only occur in the event of a 3-way merge. Merge conflicts 1. Generate a list of the files which need to be resolved: `git status` # On branch master # You have unmerged paths. # (fix conflicts and run ) "git commit" # Unmerged paths: # (use to mark resolution) "git add ..." # both modified: example.rb # no changes added to commit (use and/or ) "git add" "git commit -a" 2. When the is encountered, Git will edit the content of the affected files with that mark both sides of the conflicting content. These visual markers are: conflicted line visual indicators - , the conflict starts after this line. <<<<<<< Conflict marker - your changes from the changes in the other branch. ======= Divides - End of the conflicted lines. >>>>>>> <<<<<<< HEAD(master) conflicted text from hotfix >>>>>>> hotfix conflicted text from HEAD(master) ======= 3. Decide if you want to only your hotfix or master changes, or write a code. Delete the conflict markers before merging your changes. keep completely new 4. When you're ready to merge, all you have to do is run on the conflicted files to tell Git they're resolved. `git add` command 5. Commit your changes with to generate the merge commit. `git commit` Hope this helped you get a better understanding how to merge your branches and deal with conflicts. We automatically fetch you new articles once you subscribe! Previously published at https://kolosek.com/git-merge/