Git commands

git merge

git merge: merge changes from another branch into the current branch.

The git merge command is used to integrate changes from one branch into another. The purpose of merging is to combine the work done on different branches into a single branch, typically the main branch of development.

Merging is a common operation in Git workflows, especially when working on features or bug fixes in separate branches. Once the work on a branch is complete and has been reviewed, it can be merged back into the main branch (or any other target branch) to incorporate those changes.

There are two main types of merges in Git:

  1. Fast-forward merge: This type of merge occurs when the target branch has not diverged from the source branch. In this case, Git simply moves the target branch pointer to the latest commit of the source branch. No new merge commit is created because the source branch is already ahead of the target branch.

  2. Merge commit: When the target branch has diverged from the source branch (i.e., both branches have new commits), Git creates a new commit that combines the changes from both branches. This is called a merge commit, and it has two parent commits: one from the target branch and one from the source branch.

Merging can sometimes result in conflicts if the same lines of code have been modified in both branches. In such cases, Git will mark the conflicting lines in the affected files, and you'll need to manually resolve the conflicts by editing the files and deciding which changes to keep.

Remember, before merging, it's often good practice to make sure your current branch is up-to-date with the latest changes from the remote repository by doing a git fetch followed by a git merge or git rebase with the remote branch. This can help prevent conflicts and keep your history cleaner.

Examples

Merge the branch new-feature into the current branch:

git merge new-feature

Merge the branch login-form-bugfix into the current branch, creating a merge commit even if it could be a fast-forward merge:

git merge --no-ff login-form-bugfix

Abort a merge that has conflicts:

git merge --abort

After resolving conflicts, finalize the merge:

git add .
git merge --continue

Merge a specific commit (rather than a whole branch) into the current branch:

git merge 1a2b3c4d
© 2024-2025 GitByBit.All rights reserved.

Hi! I'm Alex, creator of GitByBit.

And this is Gitopedia—a reference that summarizes knowledge in my Git course.

Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:

Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.

The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.

Learn Git in VS Code