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
Install and Configure Git
First, install Git on your system. Download the appropriate version for your platform:This sets the name you want attached to your commit transactions.This sets the email you want attached to your commit transactions.This enables helpful colorization of command line output.
- GitHub Desktop: desktop.github.com
- Git for All Platforms: git-scm.com
Create or Clone a Repository
You have two options to start working with Git:Option 1: Clone an existing repositoryClone (download) a repository that already exists on GitHub, including all of the files, branches, and commits.Option 2: Initialize a new repository locallyThe This specifies the remote repository for your local repository. The URL points to a repository on GitHub.
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:Use
git remote -v to show the associated remote repositories and their stored name, like origin.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 Creates a new branch. We recommend naming branches based on the function or feature that will be the focus of this branch.Switches to the specified branch and updates the working directory.Now make your changes using your favorite text editor or IDE. When you’re ready to save your work:Snapshots the file in preparation for versioning, adding it to the staging area.Records file snapshots permanently in version history.
main. Create a new branch to work on features or fixes: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.
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: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: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.Sync Changes from the Remote
To keep your local repository up to date with changes from GitHub:Downloads all history from the remote tracking branches.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.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
main. Then, main will be updated with your changes.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:Next Steps
Now that you understand the basic Git workflow, explore these guides to deepen your knowledge:- Learn more about Git concepts and workflows
- Understand Git commits and version history
- Explore advanced Git topics including submodules and subtrees
- Master Git commands with our comprehensive cheat sheets