15 September 2023
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
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:
Overall, Git empowers developers with speed, simplicity, a distributed workflow, and powerful version control tools to aid software development.
While Git handles version control, GitHub extends its capabilities by facilitating collaboration, code review, and project management:
Together, Git and GitHub provide a complete version control and collaboration platform for managing software projects.
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:
The easiest way to install Git on Mac is through the command line:
git --version
to check if Git is already installed (usually it comes pre-installed)brew install git
to install the latest stable release using Homebrewgit --version
againAlternatively, you can download installers from the official Git website or GUI clients like GitHub Desktop.
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
.
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.
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.
With Git installed and configured, its time to initialize a new repository to begin version controlling your project:
cd <directory>
git init
.git
subdirectory containing all the Git objects for the new repository.git add <file1> <file2>
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.
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.
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
origin
with the URL:The remote master
branch now contains the same commits as your local repository.
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.
A great way to contribute to GitHub projects is by forking public repos:
git clone <fork_url>
git checkout -b <branch>
Following this workflow allows you to contribute without directly having write access.
Pull requests are GitHub's way of collaborating on code with contributions from forks:
By providing a standardized peer review process, pull requests help maintain code quality.
Here is a reference list of some frequently used Git commands:
git init
- Initialize a new local repositorygit clone
- Clone a remote repository to local machinegit add
- Stage files for commitgit commit
- Commit staged snapshot with messagegit push
- Push local commits to remote repositorygit pull
- Fetch latest remote commits and merge locallygit status
- List currently modified/staged filesgit log
- Show commit history/change loggit branch
- List, create or delete branchesgit checkout
- Switch between branchesgit merge
- Join two development histories (branches) togethergit remote
- Manage remote repositoriesgit rebase
- Reapply local commits on top of updated upstream headWith 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!
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
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