How to Git merge one branch into another |Video upload date:  · Duration: PT4M36S  · Language: EN

Quick guide to merge one Git branch into another using command line with practical steps to avoid conflicts and keep history clean

So you need to merge one Git branch into another and you want to do it without creating a soap opera in your repo history. Welcome to the short guide that will save you from awkward merge commits and the blame game. This is a command line focused git tutorial that covers merging, branching, conflicts and basic version control hygiene with a little sarcasm thrown in for flavor.

Quick checklist for the anxious

  • Fetch and update your local repo
  • Switch to the target branch
  • Merge the source branch or rebase if you like neat history
  • Resolve conflicts and run tests
  • Push and tell your teammates you did not break the build

Step 1 update local repo

Start by fetching remote changes so you do not get surprised by someone else s midnight refactor. Fresh local branches reduce merge conflicts and blame wars.

git fetch
# then on the branch you intend to merge into
git pull

git fetch updates remote tracking branches and git pull will merge remote changes into your current branch. If you prefer to inspect before merging run git fetch and then git log origin/target-branch to look around.

Step 2 switch to the target branch

Make sure you are on the branch that will receive the changes. Modern and clear commands work fine here.

git switch target-branch
# or the older form
git checkout target-branch
git status

Confirm you are on the right branch with git status. Stash or commit any local work first so you do not create a mess.

Step 3 merge or rebase choose your history

To bring changes from source-branch into your current branch use git merge. If you crave a linear history use git rebase but avoid rebasing branches that are public or shared without coordination.

git merge source-branch
# or if you want to rebase instead
git rebase source-branch

Merge creates a merge commit when needed. Rebase rewrites commits so the history looks like a neat single line. Both are valid workflows depending on your team s preferences.

Step 4 resolve conflicts and do not panic

If Git cannot reconcile changes it will mark conflict blocks inside files. You will see markers like this in files

<<<<<<< HEAD
your changes
=======
other changes
>>>>>>> source-branch

Edit the file to the desired state, remove the markers, then stage and finish the merge.

git add path/to/conflicted-file
# if the merge did not auto complete
git commit

For a less manual experience use a merge tool or run git mergetool to get a visual helper. Resolve conflicts locally rather than forcing questionable pushes.

Step 5 test and inspect history

Run your unit tests and quick smoke checks. Verify the merge did what you expected and that nothing is silently broken. Inspect history to ensure the commit graph looks acceptable.

git log --oneline --graph --decorate -n 20
# or view the merge commit
git show --name-only HEAD

If you used rebase check for accidentally dropped commits. If something looks wrong you can abort a rebase with git rebase --abort or undo the merge with git reset --hard ORIG_HEAD if you are sure.

Step 6 push and coordinate

Push the merged branch back to the remote to make your changes available to the team.

git push origin target-branch

If the remote rejects the push it means there are newer commits on the remote. Fetch the remote and merge or rebase again, or coordinate with your teammates before forcing anything. Force pushes to shared branches are a fast track to grief.

Quick tips for fewer headaches

  • Use clear branch names like feature/login or fix/session-timeout
  • Keep merges small and focused so reviews are quick and conflicts are smaller
  • Open a pull request for review when collaboration is needed
  • Resolve conflicts locally with a good merge tool rather than guessing at the command line
  • If you prefer tidy history consider a rebase workflow but never rebase public branches without a team plan

Summary recap You updated the repository, switched to the target branch, performed a merge or rebase, handled conflicts, tested the result and pushed the merged branch. Follow these steps and your version control workflow will be less dramatic and more reliable. Now go forth and merge responsibly. And maybe buy coffee for your reviewer.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.