Student Summer Sprint
  • Welcome to Hackathons for Schools Student Summer Sprint!
  • Event Information
  • What is the Student Summer Sprint?
  • Useful Event Information and Links
  • Schedule
  • Our Organisers, Panelists and Mentors
    • Organisers
    • Panelists and Speakers
    • Mentors
  • Project Details
    • Themes
    • Judging Criteria and Winning Teams
    • Presentation Advice
    • Submission Checklist and FAQs
  • Coding Platform and GitLab
  • Technical Workshops
    • How to Use Git
      • Git and the Terminal
      • How to use Git - The Basics
      • Branching, Merging and Other Useful Commands
      • How to use Git - Further Resources
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • Web Development - Resources
    • Introduction to Python
      • Variables
      • Data Types and String Formatting
      • Input and Output
      • Conditional Statements
      • Functions
      • Libraries
      • CHALLENGES
        • Solutions
      • Learning Python - Resources
    • Discord Bots in Python
      • Discord Bots - Resources
    • Web Scraping in Python
  • Careers Advice and Opportunities
    • University Advice
      • University Advice - Further Resources
    • CV, Applications and Interviews
      • Creating a Great CV
      • UCAS Personal Statement Advice
      • Interview Hints and Tips
      • Ultimate LinkedIn Guide
      • Applications - Further Resources
    • Different routes into a tech career
      • Different Routes into Tech - Further Resources
    • What Now?
      • More Opportunities!
Powered by GitBook
On this page
  • Branching
  • What is a branch?
  • Merging
  • Preparations for merging
  • Merge Conflicts
  • Useful commands for handling merge conflicts
  • Other Useful Commands

Was this helpful?

  1. Technical Workshops
  2. How to Use Git

Branching, Merging and Other Useful Commands

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

PreviousHow to use Git - The BasicsNextHow to use Git - Further Resources

Last updated 4 years ago

Was this helpful?

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.

Simple branching diagram, from http://davidjcastner.github.io/git-tutorial/Lab3
Handling merge conflicts in VSCode, from https://code.visualstudio.com/docs/editor/versioncontrol