Skip to main content

Welcome to Git and GitHub

This quickstart guide will walk you through the fundamental Git workflow, from installing Git to making your first commits and collaborating with others. By the end of this guide, you’ll understand how to create branches, make commits, and sync your work with GitHub.
Git is a distributed version control system that saves changes over time without overwriting previous versions. Every developer working with a Git repository has a copy of that entire repository – every commit, every branch, every file.

What You’ll Learn

In this guide, you’ll learn how to:
  • Configure Git on your local machine
  • Create and work with branches
  • Make commits to track your changes
  • Push your work to GitHub and collaborate with others
1

Install and Configure Git

First, install Git on your system. Download the appropriate version for your platform:After installation, configure your user information for all local repositories:
git config --global user.name "[name]"
This sets the name you want attached to your commit transactions.
git config --global user.email "[email address]"
This sets the email you want attached to your commit transactions.
git config --global color.ui auto
This enables helpful colorization of command line output.
Use git config --list to verify your configuration settings.
2

Create or Clone a Repository

You have two options to start working with Git:Option 1: Clone an existing repository
git clone [url]
Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits.Option 2: Initialize a new repository locally
git init
The git init command turns an existing directory into a new Git repository. After using this command, link the local repository to an empty GitHub repository:
git remote add origin [url]
This specifies the remote repository for your local repository. The URL points to a repository on GitHub.
Use git remote -v to show the associated remote repositories and their stored name, like origin.
3

Create a Branch and Make Changes

Branches are lightweight and cheap in Git, making them perfect for safe experimentation. The main branch is usually called main. Create a new branch to work on features or fixes:
git branch [branch-name]
Creates a new branch. We recommend naming branches based on the function or feature that will be the focus of this branch.
git switch -c [branch-name]
Switches to the specified branch and updates the working directory.
Always check which branch you’re on with git status. This command shows you what branch you’re on, what files are in the working or staging directory, and any other important information.
Now make your changes using your favorite text editor or IDE. When you’re ready to save your work:
git add [file]
Snapshots the file in preparation for versioning, adding it to the staging area.
git commit -m "[descriptive message]"
Records file snapshots permanently in version history.
Commits are immutable, meaning they can’t be changed. This creates a safer environment where you can be braver, trusting that Git has your back. If you make a mistake, you can easily revert that change or roll back to where everything was fine.
4

Push Your Changes to GitHub

So far, if you’ve made a commit locally, you’re the only one that can see it. To let others see your work and begin collaboration, push your changes:
git push
Uploads all local branch commits to GitHub.If you’re pushing from a branch for the first time that you’ve created locally, you’ll need to give Git more information:
git push -u origin [branch-name]
This tells Git to push the current branch and create a branch on the remote that matches it with the same name – and also create a relationship with that branch so that git push will be enough in the future.
If there has been a new commit on the branch on the remote, you may be blocked from pushing. Start with git pull to incorporate the changes on the remote into your own local branch, resolve any conflicts, and then try the push again.
5

Sync Changes from the Remote

To keep your local repository up to date with changes from GitHub:
git fetch
Downloads all history from the remote tracking branches.
git pull
Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge.
By default, git push only pushes the branch that you’ve currently checked out to, keeping your other branches unaffected.
6

Collaborate with Pull Requests

When you’re ready to merge your work, open a pull request on GitHub. A pull request is a comparison of two branches – typically main and your feature branch.Pull requests are the powerhouse of GitHub:
  • Integrated tests can automatically run on pull requests, giving you immediate feedback
  • Peers can give detailed code reviews
  • You can collaborate and iterate on your work before merging
Once you and your team decide that the pull request looks good, you can merge it. By merging, you integrate the feature branch into main. Then, main will be updated with your changes.
Pull requests should be opened when work is beginning, not when it’s finished! The earlier you open a pull request, the more visibility the entire team has to the work you’re doing.
After merging, don’t forget to delete your branch. Remember, branches are lightweight and cheap – create a new one when you need it based on the most recent commit on the main branch.

Essential Git Commands Reference

Here are the most important commands you’ll use regularly:
# Check your current branch and file status
git status

# View commit history
git log

# Show associated remote repositories
git remote -v

Next Steps

Now that you understand the basic Git workflow, explore these guides to deepen your knowledge:
Practice makes perfect! The best place to practice using Git and GitHub is the Introduction to GitHub Learning Lab course.

Why Git and GitHub Matter

Speed: Git uses SHA compression, which makes it very fast. Safe Collaboration: Git can handle merge conflicts, which means it’s OK for multiple people to work on the same file at the same time. Cheap Branches: By using branches, developers can make changes in a safe sandbox without affecting production code. Ease of Roll Back: If you make a mistake, it’s OK! You can easily revert changes or roll back to any previous commit. Version control is essential – without it, you risk losing your work. With Git, you can make a “commit” as often as you’d like, and you can always go back to previous commits. This takes the pressure off while you’re working. Commit often and commit early, and you’ll never have that gut-sinking feeling of overwriting or losing changes.

Build docs developers (and LLMs) love