Git and Github are tools we will be using throughout the course to track and store changes to our code and allow you to collaborate with others and keep everything in sync.
This guide introduces using Git with the terminal and GitHub web UI.
Note: You do not need GitHub to use Git, but you cannot use GitHub without using Git.
What is Git?
Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development 1(https://en.wikipedia.org/wiki/Git).
What is GitHub?
GitHub is a code hosting platform for version control and collaboration. It let's you and others work together on projects from anywhere.
Why use Git?
Version control is very important. Without it, you risk losing your work. With Git, you can save your work as often as you like. You can also go back to previous work. This takes the pressure off of you while you're working.
Tip Commit often and commit early, this way you'll never have that gut sinking feeling of overwriting or losing changes.
To get started, you will need to download, install and configure Git on your computer and create a free GitHub account here.
git versionto verify Git was installed.
Git uses a username and email to associate commits with an identity. Let's define your user with the following commands
git config --global user.name "Your Name" git config --global user.email "firstname.lastname@example.org"
When you connect to a Github repository from Git, you will need to authenticate using either HTTPs or SSH. We recommend connecting over HTTPS and caching your GitHub credentials in Git following this instructions
The general development flow is:
Below are some walkthroughs steps for
Local Git repositories are created and managed locally on your computer.
To begin, open up a terminal and create a new directory, open it and perform
git init to create a new repository
mkdir newproject cd newproject/ git init
Now that your local Git repository is up and running, you can update it with any changes you make.
Check for any changes that have been made or new files since your last commit
Stage any files with changes (or new ones) you would like to commit
git add <path>: This stages an individual file or directory
git add .: This stages all files with changes
Check to see what was added
Commit the changes to your local repository with a brief message outlining the changes
git commit -m "your message about the commit"
Note: Git will notice any new files inside the repository but it won't track the files unless you explicitly tell it to.
To put your project up on GitHub, you will need to create a repository for it. To create a new repository:
For more details, checkout GitHub documentation on Creating a new repository.
Since we've already created our repository locally, we want to push that onto our newly created GitHub repository.
Connect your local repository to GitHub repository
git remote add origin https://github.com/yourUserName/yourRepoName.git
Push your local changes to your remote repository
git push -u origin main
Browse to your GitHub repository web page to refresh and see your code
To update your local repository to the most recent changes that you or others have push to GitHub, execute
git pulldoes two things behind the scene: fetch & merge remote changes. Git tries to auto-merge, but unfortunately this is not alway possible and may result in conflicts. You are responsible to merge those conflicts manually by editing the files shown by Git.
If you are collaborating on a project, or just want access to your project from a different computer, you can clone the project from the GitHub repository.
Cloning a repository is typically only done once, at the beginning of your interaction with the project. Once you have cloned a repository, you won't need to clone it again to do regular development.
To clone a repository:
git clone, an the paste the repository URL & press Enter
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
Throughout the program, you may be provided initial code to get you started. If so, follow this steps to setup your own repository:
Once you have the starter code in your computer, let’s open up a terminal to initialize the git repository.
cd /path/to/unzipped/project git init git add . git commit -m “initial starter code setup”