Git provides a powerful tool by letting us create . When our branch is done, we'll often want to merge it back into our or branch - or perhaps even merge it into another branch entirely. Merging with git is straightforward. In this guide, we'll cover the command. branches main master merge Merging your git branches Merging is quite straightforward. Suppose you are on your branch, and you want to merge a branch called . All you have to do is run the following command: main my-branch git merge my-branch To visualize this, take a look at the diagram below. Here, the branch is shown in green. Before we perform our merge, we are on the commit labeled . After, creates a new merge commit labeled as . That means any time you make a merge, a commit will also be made: main main commit merge new merge commit Performing a merge As mentioned, you'll want to use to merge another branch into your current branch. However, you will have to commit everything on your current branch before doing that. So first, make sure you checkout the branch you want to merge something into: git merge git checkout main Then add and commit any files on your branch: git add -A git commit -m "Some merge message" Then finally, you are ready to merge something into your branch: git merge my-branch ### Fast forward merging Since the branches diverged here, only one new commit is made. However, if the branches did not diverge, then something called a "fast forward" merge is committed instead. This is where the entire commit history for the feature is copied onto the branch. For example, if we had a branch that we created a branch of, and then changed the branch, and not , a fast forward merge would be created. main main only main This means the history of our commits would look like this instead: You can avoid a fast forward merge by using the : --no-ff git merge --no-ff my-branch There are also a few other options: ff - the default - a fast forward will occur when possible. --ff - will only merge if a fast forward is possible. Otherwise nothing will happen. --ff-only Resolving merge conflicts Merging code will sometimes lead to merge conflicts. This is when Git can't automatically determine what should happen when you try to merge code. In these cases, you'll have to manually resolve a merge before you can complete it. If you don't think you can resolve it right now, you can exit out of a merge by running . git merge --abort . You can learn more about merge conflicts and resolving them here Setting a merge commit message As I've mentioned, a merge will also create a new commit in your git history. If you want to add a custom message, you can use the flag just as you would with : -m git commit git merge my-branch -m "Some commit message" ### Preventing an auto commit Sometimes, a merge message is not enough - you want to merge your code into a branch, change it a little, and then finalize your commit. You can merge a branch without creating a commit by using : --no-commit git merge --no-comit my-branch After that, you can make any changes you want to your code, and then make a new commit at your leisure: git add -A git commit -m "New commit message" Conclusion In this guide we've covered the main themes you'll need to know around merging. In git, merging your code is an important theme of collaborating with others and managing branches. . Otherwise, . If you run into merge conflicts, you can read about that here check out more git content here Also Published Here