Branching, Merging and Other Useful Commands

Beginner's guide to branching, merging and other Git commands

Branching

What is a branch?

A branch is like a snapshot of the different changes you've made on your project - essentially a pointer to a commit. New branches in Git are normally created when adding new features or trying to fix a bug. When working on group projects, each member of the group might have a separate branch to work on their code. The main branch in a project is called the master. The master branch is where the most up to date, error free version of the code is kept.

To show a list of all the branches in your repository,

git branch

To create a new branch,

git branch <branch-name>

Remember, branches are pointers to commits. When you create a new branch, you don't make any changes to the actual repository or its history.

You can switch between branches (in your local repository) using the checkout command.

git checkout <branch-name>

If you want to switch to a new branch,

git checkout -b <new-branch-name>

If you want to switch to a branch from your remote repository then you must first fetch the contents of that branch by running the command git fetch --all

Merging

The most difficult part of using Git is merging. By creating separate branches we have different lines of development. Merging is the process of integrating these branches into a single branch so we can have finalised versions of the project.

Preparations for merging

  • Make sure your local repository is up to date with remote changes by running git fetch

  • Checkout to the target branch that you'll be merging into by running git checkout <target-branch>

  • Ensure the target branch is up to date by running git pull

  • Checkout to the target branch with git checkout <target-branch>

Once preparations are complete, You can merge your branch by running

git merge <source-branch>

Merge Conflicts

Merging doesn't always go smoothly. Merge conflicts occur when two branches have changed the same part of the same file and Git doesn't know which version to use.

Useful commands for handling merge conflicts

Git status is used to check the current state of the working directory and the staging area. When trying to resolve conflicts, this can show us the files that have been modified which means we need to inspect them to solve the conflict.

git status

Git status is used to identify which files were changed that are causing merge conflicts. With git diff, you can find out how these files were changed in detail.

git diff

After manually changing your files, you must run the git add <file-name> command to mark them as merged to show you've resolved the conflicts. Then proceed to commit your changes.

One of the easiest way to resolve merge conflicts is to inspect your code in a code editor like Visual Studio Code. It has brilliant version control support where you can compare code when there are merge conflicts, with the conflicting sections of code highlighted.

Other Useful Commands

  • Display the current commit history

git log
  • Undoing changes - create a new commit that undoes all the changes made in <commit>, then apply it to the current branch

git revert <commit>
  • Current branch version moves back towards <commit> version and resets the staging area, but doesn't change the working directory

git reset <commit>

And that's a very quick introduction on using Git! Take a look at the further resources on the next page to learn more about version control.

Last updated