CUSTOMISED
Expert-led training for your team
Dismiss
Git and GitHub for Beginners: A Step-by-Step Guide

15 September 2023

Git and GitHub for Beginners: A Step-by-Step Guide

Version control systems allow developers to collaborate on code and track changes over time. Git is one of the most popular distributed version control systems, used by teams large and small to manage code. GitHub is a web-based platform built around Git, facilitating social coding and open source project management.

This comprehensive guide will take you through Git and GitHub from the ground up, providing hands-on training to boost your skills. Follow along to learn the fundamentals of using Git locally and collaborating with Git and GitHub workflows. This guide is a support resource for JBI Trainings courses in Git and GitHub and is intended to be used in conjunction with Git for Beginners

What is Git and How Does it Help Developers?

Git is an open source distributed version control system that tracks changes in any set of files, enabling coordination between multiple developers working on a project simultaneously.

Unlike centralized version control systems, Git users have an entire copy of the project repository locally. This allows them to work offline and switch context quickly between different branches and workflows.

Some key advantages of Git include:

  • Track Changes Over Time - Git captures the history of project files and allows reverting to any previous version. Developers can analyze how the codebase evolved over time.
  • Branching and Merging - Multiple developers can work in parallel on independent branches of code and merge their changes back together.
  • Rollback Mistakes - Since every change is tracked, mistakes can be easily undone or reverted to earlier functioning versions.
  • Collaboration - Git enables social coding through built-in collaboration tools and integrates with platforms like GitHub for further collaboration.

Overall, Git empowers developers with speed, simplicity, a distributed workflow, and powerful version control tools to aid software development.

How Does GitHub Enhance Git Functionality?

While Git handles version control, GitHub extends its capabilities by facilitating collaboration, code review, and project management:

  • Remote Repositories - GitHub provides cloud-based remote repositories to store a central project copy and enable collaboration.
  • Fork and Pull Model - Developers can fork a GitHub project, make local changes, and submit pull requests from forks into the main repository.
  • Visibility and Social Coding - By making projects publicly visible on GitHub, developers can easily inspect codebases and contribute.
  • Project Management - GitHub includes project boards, issue tracking, wikis, and other tools to manage tasks and organize workflows.
  • Code Reviews - Pull requests facilitate peer review of code before merging into the shared mainline repository.
  • Integrations and Extensibility - GitHub provides OAuth authentication and an API to integrate with external applications.

Together, Git and GitHub provide a complete version control and collaboration platform for managing software projects.

Installing Git Locally

To get started, you'll need to install Git locally on your computer. Here are simple steps to install Git on Mac, Windows, and Linux machines:

Installing Git on Mac

The easiest way to install Git on Mac is through the command line:

  1. Open Terminal
  2. Run git --version to check if Git is already installed (usually it comes pre-installed)
  3. If not installed, run brew install git to install the latest stable release using Homebrew
  4. Verify the install by running git --version again

Alternatively, you can download installers from the official Git website or GUI clients like GitHub Desktop.

Installing Git on Windows

On Windows, download the latest installer from git-scm.com. Execute the installer and follow the setup wizard, selecting your preferences.

During installation, you can choose to enhance your PATH for command line access and integrate Git with visual tools like Visual Studio.

After completing setup, test it out by opening Git Bash and running git --version.

Installing Git on Linux

For Fedora/RHEL/CentOS systems, use the following command:

$ sudo yum install git

For Debian/Ubuntu systems, use:

$ sudo apt install git

For Arch Linux, use:

$ sudo pacman -S git

Verify with git --version.

With Git installed locally, you can now initialize repositories and perform version control on your projects.

Configuring Git With Your Username and Email

Once Git is installed, set your username and email to associate commits with your identity:

$ git config --global user.name "Your Name" $ git config --global user.email "[email protected]"

The --global flag sets credentials for all repositories on your system. To configure them individually per project, omit --global when inside a repository.

You can edit more Git configuration like default editor, coloring, aliases etc. with the git config command.

Initializing a New Git Repository

With Git installed and configured, its time to initialize a new repository to begin version controlling your project:

  1. Navigate into your project directory using cd <directory>
  2. Initialize an empty Git repo with git init
  3. This creates a .git subdirectory containing all the Git objects for the new repository.
  4. Begin tracking files with git add <file1> <file2>
  5. Commit the files with git commit -m "Initial commit message"

Repeat steps 3 and 4 to save subsequent changes. Use descriptive commit messages to document updates.

Once initialized, Git will now track changes within the project enabling features like history, branching, merging and more.

Cloning an Existing Repository

Rather than initializing a new repo, you can clone an existing repository to create a local working copy:

$ git clone <repo_url>

For example, to clone the Definitive Guide repo from GitHub:

$ git clone https://github.com/swcarpentry/git-novice.git

This downloads the repository contents onto your machine, enabling you to modify files locally.

Pushing Local Commits to Remote Repositories

So far we have only interacted with local repositories. But Git's power lies in publishing commits and collaborating remotely.

Let's push our local repository to a hosted Git platform like GitHub or BitBucket:

$ git remote add origin <remote_url>
$ git push -u origin master
  1. Create a new empty repository on the remote host (e.g. on GitHub)
  2. Copy the remote URL under the Quick Setup section
  3. In your local repo, add a remote called origin with the URL:
  4. Now push local commits to the remote with:

The remote master branch now contains the same commits as your local repository.

Git Branching for Parallel Development

Branching allows diverging from the main development line to enable parallel workflows:

To create a new branch and switch to it:

$ git checkout -b <branch_name>

Add commits locally as normal. Then switch back to master and merge:

$ git checkout master $ git merge <branch_name>

Branches let you context switch quickly between independent streams of development.

Forking and Cloning GitHub Repositories

A great way to contribute to GitHub projects is by forking public repos:

  1. Fork the project repository on GitHub
  2. Clone your fork locally with git clone <fork_url>
  3. Create a new branch git checkout -b <branch>
  4. Make code changes, commit and push to your fork
  5. Open a pull request from your fork to the upstream repo

Following this workflow allows you to contribute without directly having write access.

Creating and Merging GitHub Pull Requests

Pull requests are GitHub's way of collaborating on code with contributions from forks:

  1. Fork the upstream repository
  2. Clone and modify code locally
  3. Push changes to your fork
  4. Open a pull request to the upstream repository
  5. Discuss, review code and iterate
  6. An admin merges the PR into upstream to publish contributions

By providing a standardized peer review process, pull requests help maintain code quality.

Common Git Commands for Everyday Use

Here is a reference list of some frequently used Git commands:

  • git init - Initialize a new local repository
  • git clone - Clone a remote repository to local machine
  • git add - Stage files for commit
  • git commit - Commit staged snapshot with message
  • git push - Push local commits to remote repository
  • git pull - Fetch latest remote commits and merge locally
  • git status - List currently modified/staged files
  • git log - Show commit history/change log
  • git branch - List, create or delete branches
  • git checkout - Switch between branches
  • git merge - Join two development histories (branches) together
  • git remote - Manage remote repositories
  • git rebase - Reapply local commits on top of updated upstream head

Conclusion

With this introduction, you should now feel confident using Git locally and GitHub for remote collaboration.

There are many more powerful features and workflows to master, but these basics will take you far. The Git documentation provides an excellent in-depth resource to continue learning.

GitHub also offers interactive courses like Introduction to GitHub and Communicating using Markdown - highly recommended to supplement this guide.

Happy Gitting!

Frequently Asked Questions

Here are some common questions about Git and GitHub:

Q: How is Git different from GitHub?

Git is a version control system that allows tracking code changes, branching, merging, and collaboration. GitHub is a web platform built around the Git toolset that adds collaboration, code review, and project tracking capabilities.

Q: Why is Git better than other version control systems?

Git is distributed rather than centralized, meaning every dev has the full codebase locally. This enables speedy branching and merging, offline work, and multiple workflow approaches based on branches.

Q: What benefits does GitHub provide?

GitHub facilitates collaboration through forking, pull requests, and code reviews. It also provides issue tracking, project boards, CI/CD integration, Wikis and other tools to manage the software development lifecycle.

Q: How do I undo a commit in Git?

Use git reset HEAD~1 to undo the last commit, keeping changes locally. Use git reset --hard HEAD~1 to permanently undo and delete the commit.

Q: Can I delete a remote branch on GitHub?

Yes, remote branches can be deleted using:
git push origin --delete <branchName>

Q: How do I rebase my local branch with the latest remote changes?

First fetch the latest remote changes:
git fetch upstream
Then rebase your local branch:
git rebase upstream/master

This will replay your commits on top of the latest upstream commits.

Our next article is Advanced Git and GitHub: Taking Your Skills to the Next Level check our our article Learn Git and GitHub with these 10 Essential Commands or you might like to take one of our courses in Git and Github

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course