A Beginner’s Guide to Handling Git Merges and Conflicts in the Terminal

How to Merge Branches and Resolve Merge Conflicts from the Command Line

A Beginner’s Guide to Handling Git Merges and Conflicts in the Terminal


Merging code is a daily task for developers working in collaborative environments. While tools like VSCode provide graphical interfaces for resolving conflicts, mastering the command line gives you more control and speed — and is a must-have skill for every new developer.

In this guide, we’ll walk step-by-step on how to:

  • Merge code from one branch into another.

  • Handle merge conflicts directly in the terminal.


1. Prerequisites

Before starting, make sure you:

  • Have Git installed on your system.

  • Have cloned your repository locally.

  • Have committed any uncommitted changes (so your working directory is clean).

Check your Git version:

git --version

2. Switch to the Target Branch

Decide which branch you want to merge into. Usually, this is main or develop.

git checkout main

Update your branch with the latest changes:

git pull origin main

3. Start the Merge

Now, merge another branch (e.g., feature-branch) into main:

git merge feature-branch
  • If there are no conflicts, Git will complete the merge and create a commit automatically.

  • If there are conflicts, Git will stop and tell you which files need attention.


4. Identifying Merge Conflicts

When conflicts occur, Git marks the conflicting sections inside the affected files:

<<<<<<< HEAD
This is code from the current branch (main)
=======
This is code from the branch being merged (feature-branch)
>>>>>>> feature-branch
  • <<<<<<< HEAD: your current branch’s code.

  • =======: separator.

  • >>>>>>> feature-branch: incoming branch’s code.


5. Resolving Merge Conflicts

Open the conflicted files in any text editor (or even nano, vim, or gedit from the terminal).

You must choose what to keep:

  • Keep your branch’s code.

  • Keep the incoming branch’s code.

  • Or manually combine both.

After editing, remove the conflict markers (<<<<<<<, =======, >>>>>>>).

Example resolved version:

print("Hello from both branches!")

6. Mark Conflicts as Resolved

Once you fix the conflicts:

git add <filename>

Do this for all conflicted files.

Then complete the merge:

git commit

This will open your default editor to write a merge commit message. Save and close it.


7. Abort a Merge (If Needed)

If things get messy and you want to restart the merge:

git merge --abort

This resets your branch back to the state before the merge.


8. Push the Merged Branch

Finally, push your merged changes to the remote repository:

git push origin main

9. Tips for Beginners

  • Always git pull before starting a merge, so you work with the latest changes.

  • Use git status often to see which files are in conflict.

  • Resolve conflicts in small steps — don’t try to fix too many files at once.

  • Practice merging in a test repository before doing it in production code.


✅ Summary

  • Switch to the target branch (git checkout main).

  • Merge the feature branch (git merge feature-branch).

  • Resolve conflicts by editing files.

  • Add and commit the resolved changes.

  • Push to remote.

Mastering command-line merges makes you a stronger developer, especially when working on servers or environments without GUIs.


Would you like me to also add example conflict resolution workflows (like choosing all incoming changes vs. all current changes using git checkout --ours or --theirs)? That makes resolving big conflicts much faster.

Comments