Git and GitHub for Dummies
Prerequisites:
- Git installed
- A GitHub Account
- Basic Bash Knowledge
What is Git?
Git is a Distributed Version Control System. To put it simply, it is a program that records changes to a file or directory that you can use to revisit any previous version of the specified file or directory. Git also keeps track of other information, which can be useful to compare changes and revert changes. Git is such a valuable tool because it allows multiple people to collaborate over a codebase, which is very useful for projects with more than one developer or an open-source project. Git is mainly used in conjunction with GitHub, which is a repository hosting platform that leverages Git for its users to collaborate over different repositories.
Checking if Git is installed correctly:
Git is a command-line utility, which means to interact with it, we must first open up a terminal or command prompt on windows. After you open your terminal, you have to type git --version
and make sure Git is properly installed. If you don’t get a version number or get some error, you should make sure your Git is installed correctly.
Interacting with Git:
In this tutorial, we will initialize a Git repository. To do this, make a new folder, and open a terminal inside of it. To do this in windows, open the folder, and while holding shift, right-click the folder and select the “Open Power Shell window here.” On Macs, you can click on the Finder text on the title bar, and inside of the Services tab, select New Terminal at Folder. With Git open, now you can initialize that folder into a Git repository. To do this, type git init
, this command tells Git to track all the changes that happen in this folder. Now, we can add files for Git to track. Create a simple text file, and type in whatever you want. We will now be using Git to log any changes done to this file. To do this, type in git add --all
, or git add <filename>
to add your text file to Git’s staging area. The Git staging area is where files are first moved, using the git add
command.
This feature gives us the ability to only stage portions of a file; therefore, we can always make sure we are only committing related modifications. Now that we have moved all the files we want to commit into the staging area, we can now commit these changes by doing git commit -m "a message to describe your commit"
. If you get the error, Please tell me who you are
follow the steps, and run git config --global user.email "example@example.com"
and git config --global user.name "your username"
. After you do this, you can run git commit -m "a message to describe your commit"
. Now, we have moved the changes from the staging area into the repository. Because of this, we can always traverse back to this state of our directory. To see this in action, we can edit the file and add more text to it. After doing so, we can add and commit this again by doing git commit -am "updated file"
.(a shorthand for git add --all
, git commit -m "message"
is git commit -am "message here"
) Now, to traverse back to our first commit, do git log
Here, you can see all of the previous commits that have been committed to our repository.
Now, find the commit hash. The Hash is what we use to identify each commit, and we can use the hash to travel back to that specific commit. In the first example, the first commit’s commit has is d2ae8489f04… Now, copy the hash, and then type git checkout <hash value>
. This leaves Git in a ‘detached state’ and allows us to view our directory as that commit. To leave this detached state, type git switch -
.
Incorporating GitHub
Before we continue, if you haven’t made a GitHub account, make one here.
GitHub is a service that allows its users to create cloud repositories. This allows us to host our GitHub repositories online for other people to also collaborate on. To get started, go to https://repo.new. Here, you can set basic rules, such as making the repository private or public. After you create the repository, you can add the remote repository to git by typing in git remote add origin <link to GitHub Repository>
, followed by git branch -M master
, and finally, push your changes to GitHub by doing git push -u origin master
. The git push
pushes your local commits to GitHub, allowing other people to pull down your changes. Now, you should see the file that you were editing locally on GitHub. Now, if you add any more changes, you can run git add --all
to move the files into the staging area, and then you can commit it, by running git commit -m "a descriptive message"
and finally, you can git push
your changes to GitHub. You can even see your previous Git history on GitHub by clicking on the commits icon here:
Now others can also look at your code (if you made your repository public, of course) and contribute at your discretion. Now, click on the file you created, and add more text to it inside of GitHub by clicking on the edit icon. You should be brought into this online text editor, where you can add changes to your file.
After writing your changes and adding a commit message, select Commit directly to the master branch
and click Commit changes
By doing this, GitHub now has a more recent change than what we have in our local repository on our machines. To fix this, we can run git pull
which pulls down the new changes from our online GitHub repository. It is paramount to remember that you need git push
your changes when you are done working on something and git pull
new changes that you didn’t commit.
Git Branching:
So far, we have learned how to document our changes using Git and share them online, but this is only a small part of the full power of Git. To incorporate Git more into our workflow, we need to learn about Git branching. Branching is a concept where we take our existing repository and branch off of it. This allows us to experiment on changes before committing them and have a feature-driven workflow by creating branches for each feature we are working on and then merging that back into the main master
branch. In our previous example, we were always committing linearly, which you can see here:
But let's say we want to keep a copy of the latest commit while still having a copy that we can edit. To accomplish this, we can use Git Branches! Here’s how our git log would look if we used branches:
As you can see, we split off the master branch and create a commit independent of the master branch. So let's implement this on our repository. First, we need to create a new branch by typing git branch newbranch
. Now, if you type git branch
to list out all of the branches in the repository, we can see that we have the master
branch and the newbranch
branch.
to switch to this branch, type git checkout newbranch
. We are in a separate branch that is entirely different from the main branch that we were working on before. Now, if we add more lines into file.txt and commit it, we can run git log
to see that our changes happened. newbranch
If we now go back into the master branch, by doing, git checkout master
we can see that the changes that we committed newbranch
are simply not present. Finally, let's push our new branch to GitHub so that our new changes can also be stored remotely. To do this, we need to run git push --set-upstream origin newbranch
. We can’t directly push because we created a new local branch that GitHub doesn’t have. To get around this, we tell Git to create a new branch on GitHub with the name new branch, and then push. After doing this, you should be able to go back into GitHub and select the new branch using this drop-down menu:
Finally, let's merge our two branches. Merging is the process where we combine the commits from our newbranch
back into the master
branch. To do this, go back to your terminal, and make sure you are on the master branch by doing git status
Which lists the status of your repository. If you aren’t on the master branch, you can run git checkout master
to check out the master branch. Now, you can run git merge newbranch
to merge master with the new branch. Finally, git push
your changes to GitHub! You should see this message when selecting a new branch from the drop-down menu on GitHub if you did everything correctly.
Quick Notes about GitHub:
GitHub allows us a few extra features in our project that we can look at by clicking on the settings tab. To invite other people to also be able to add changes to your repository, click on Manage Access. After entering your password, you can invite collaborators to also have control over your repository and push changes to it. Simply click on Invite a collaborator
, and type in their GitHub username. They should be prompted with an email to accept your invitation. Back in the options tab, you can also change the repositories visibility to public or private right here:
In GitHub, if a repository is public, you can “fork” the repository and own your own copy of it. This allows you to keep your own copy of the repository that you can contribute to. To fork a repository, you can go to any public repository (that isn’t your own) and click on the ‘fork’ button here:
Closing Notes:
We’ve barely scratched the surface of effectively using Git and its tools. To learn more about git, I highly recommend taking a look at the official Git documentation at https://git-scm.com/book/en/v2. This is the best resource for understanding Git you’ll find on the internet. However, simply learning the theory and spending hours mindlessly reading the documentation will only get you so far.
To get more comfortable with Git, I recommend applying your Git and Github skills to one of your own projects. If you are a high school student, feel free to check out Explore Hacks, a 3-day hackathon for high school students taking place from July 23rd to July 25th, 2021.
Author:
Editors:
David Phillips & Leo Xu