GitHub Setup Tutorial
Get Started with GitHub
GitHub Hierarchy
GitHub consists of a local workspace (a set of working directories on your local computer) and a remote workspace (a set of working directories on an external server, known as the GitHub repository).
The local and remote workspaces each consist of the following three directories:
-
Working directory
-
Staging directory
-
Repository directory (also sometimes referred to as the HEAD directory)
Common GitHub Workflow
GitHub offers flexibility for users to create and implement many possible workflows. For example, many developers use the following simple workflow:
-
On their computer, in the local environment, a developer changes or creates a file in the working directory.
-
When the changes are complete, the developer wants to save and push those changes to the GitHub repository. To accomplish that, they will need to stage the files. They can do this by running the
add
command, to add the files to the Staging Directory. -
After staging the files, the developer will want to move the files to the Repository (HEAD) Directory. They will do that by running the
commit
command.
This workflow can be summarized as follows:
-
Working Directory—Files are created or edited.
-
Staging Directory—Files are added using the
add
command. -
Repository (HEAD) Directory—Files are committed using the
commit
command.
Understanding this important distinction of where files exist at any given time (local vs. remote) is critical to managing files on your local computer and on the remote GitHub repository.
Important: Remember, files that have been committed and moved to the Repository (HEAD) Directory are still only present on your computer in your local repository!
Ultimately, the goal is to push the files out to the remote GitHub repository so that other developers on your team can view and edit the files. You can use the following commands to move files:
-
Use the
push
command to move files from your local Repository (HEAD) Directory to the remote GitHub Repository (HEAD) Directory. -
Use the
pull
command to move files from the remote GitHub Repository (HEAD) Directory to your local Repository (HEAD) Directory.
To determine which files exist on your local directory vs. the remote GitHub directory, remember the following:
-
When logged into your GitHub UI, you are viewing files in the remote GitHub repository.
-
When browsing files in your computer file directories, you are viewing your local environment repositories.
Files can have the following three possible states:
-
The file is in your local repository AND in the remote repository.
-
The file is in your local repository but NOT in the remote repository.
-
The file is in your remote repository but NOT in the local repository.
Note: In this boot camp, you will learn numerous ways to determine whether files on your computer are also on the GitHub repository. Just remember that you can always quickly and easily compare file status between the local and remote repositories by logging in to the GitHub UI and comparing those files to your local computer repository.
GitHub Guide
Create a Project in the GitHub Repository and Clone to the Local Workspace
-
On the GitHub UI, navigate into the Repositories tab and click the New button to create a new repository named
github-test-project
.-
Choose the default option to give the repository Public access.
-
Under Initialize this repository with, select Add a README file and Add .gitignore.
- When you select Add .gitignore, a dropdown menu will offer options for
.gitignore
templates. Select Java to generate a Java.gitignore
file.
- When you select Add .gitignore, a dropdown menu will offer options for
-
When you're ready, select Create repository.
-
-
The
github-test-project
has been created! Note that the repository contains two files—the.gitignore
file and theREADME.md
file. -
Next, locate the Code button and select the dropdown arrow. From the dropdown menu, select HTTPS, and click the copy icon to copy the repository URL to your clipboard.
-
From your terminal window, navigate to your workspace folder. Here you will run the Git
clone
command, to copy the repository onto your computer in your workspace directory.-
From inside your workspace directory, enter the command
git clone [REPOSITORY_URL]
, where[REPOSITORY_URL]
is the URL that you copied to your clipboard in the preceding step.-
Windows users: note that the Ctrl+V and Ctr+C shortcuts do not work for copy and pasting in Git Bash. To paste the URL that you copied to the Git Bash terminal, use the Insert key.
-
As the cloning process begins, a message will indicate that the project code is being copied from the remote GitHub repository to your local workspace folder (something like
Cloning into github-test-project...
).
-
-
-
After the cloning process completes, run the command
ls -a
from inside your workspace directory. Running this command should reveal thegithub-test-project
folder and confirm that the project has been copied into your workspace directory. -
Next, change directories to navigate into the
github-test-project
folder, by running the commandcd github-test-project
. -
Having navigated into the
github-test-project
directory, run the commandls -a
. This time you will find a few files, including theREADME.md
and.gitignore
files that were created on the repository when you created the project on the GitHub UI.- The
.gitignore
file is a hidden file, as indicated by the dot (.
) preceding the file name. But the-a
inls -a
allows us to view hidden files from the terminal!
- The
-
Next, let's confirm that your local
github-test-project
folder is in sync with your remote GitHubgithub-test-project
folder, by running the commandgit status
from inside thegithub-test-project
directory.-
A response should indicate that you are
On branch main
and thatYour branch is up to date with origin/main
. -
This tells us that the files in the local
github-test-project
directory match the files in the remote GitHubgithub-test-project
directory. The local repository and the remote repository are in sync!
-
-
Next let's create a new file in the local
github-test-project
directory, by running the commandtouch TestFile
. -
Confirm that the new file was created—by running the
ls -a
command—and confirm that there is now a file namedTestFile
in your local repository. -
At this point we should expect that the newly created file is in the local repository but not in the remote GitHub repository—indicating that the local repository and the remote repository are out of sync.
- To confirm this, refresh the GitHub UI. Note that we find no reference to any
TestFile
in the UI view.
- To confirm this, refresh the GitHub UI. Note that we find no reference to any
-
In the terminal, from inside the
github-test-project
directory, run the commandgit status
.-
Once again, a response should indicate that you are
On branch main
and thatYour branch is up to date with origin/main
. However, this time you should also find a reference toUntracked files:
, withTestFile
listed in red text. The red text indicates that this file is now present in your local Working Directory. -
Remember, to move this file from the Working Directory to the Staging Directory and then to the Repository (HEAD) Directory, we will need to
add
the file to the Staging Directory and thencommit
the file to the Repository (HEAD) Directory. Let's do that next!
-
-
To move the file from the Working Directory to the Staging Directory, run the command
git add TestFile
from inside thegithub-test-project
directory. -
Now run the
git status
command.- Note that this time
TestFile
is listed in green text—indicating that the file is now staged in the Staging Directory and is ready to be committed.
- Note that this time
-
Next, run the command
git commit -m "[COMMIT_MESSAGE]"
.-
The
-m
portion of the command indicates that you will commit the file and leave a message to describe the commit. For example,git commit -m "Adding TestFile to the repository"
. -
Remember that the
commit
command will move the file from your local Staging Directory to your local Repository Directory. The file is still on your local computer but not yet present in the remote GitHub repository.
-
-
Next we will want to
push
the file from the local Repository (HEAD) Directory to the remote Repository (HEAD) Directory. To do so, execute the following commands:-
Run the
git status
command, and note that the response indicates thatYour branch is ahead of origin/main by 1 commit
. -
Now it is time to
push
the file up to the remote GitHub repository, by running the commandgit push
.
-
-
Confirm that the
push
command was successful by refreshing the repository page in the GitHub UI.-
Previously we confirmed that
TestFile
was in the local repository but NOT present in the remote repository. -
Now, after refreshing the repository page in the GitHub UI, you should find
TestFile
listed along with theREADME.md
and.gitignore
files—confirming that thepush
command was successful. The local repository and the remote GitHub repository are once again in sync!
-
Pull Changes from the Remote to the Local Workspace
Now that we have created a file on the local repository and then pushed that file out to the remote GitHub repository, let's explore the opposite situation—when we pull a change from the remote repository into the local repository.
-
Select
TestFile
in the GitHub UI. Then locate and click the trash can icon, to deleteTestFile
from the remote GitHub repository.- When you click the trash can icon, a new page will open and ask you to commit the changes. Enter a description for the commit in the text area provided, then click Commit changes to commit the deletion of
TestFile
.
- When you click the trash can icon, a new page will open and ask you to commit the changes. Enter a description for the commit in the text area provided, then click Commit changes to commit the deletion of
-
You should be returned to the main repository page after clicking the Commit changes button. Now you can confirm that the
TestFile
is no longer listed as a file on the remote GitHub repository—meaning that the remote GitHub repository and the local repository are out of sync again. -
To confirm this, go back to the terminal and run the command
ls -a
from thegithub-test-project
directory. You should still findTestFile
in your local repository, confirming that the remote and local repositories are out of sync. -
Next, from the
github-test-project
directory in your local environment, run the commandgit pull
to pull the changes from the remote GitHub repository into your local repository.-
In this example, that change is represented by the deletion of the
TestFile
from the remote GitHub repository. -
When you run the
git pull
command, you should find thatTestFile
has been deleted.
-
-
Once again, run the
ls -a
command from inside thegithub-test-project
directory, to confirm thatTestFile
has in fact been deleted and removed from the local repository.- At this point, the local repository and the remote GitHub repository are in sync!
Congratulations! You have now successfully created a GitHub repository, cloned that GitHub repository to your local workspace, made changes to the files on your local workspace, and pushed those changes up to the GitHub repository—successfully re-syncing the local and remote repositories. Then you made changes to the remote GitHub repository and pulled those changes into your local repository—again, successfully re-syncing the local and remote repositories.
These GitHub skills will prove to be valuable for this course as well as your professional software development career!
Personal Access Token Overview
Compared to password authentication, tokens offer numerous security benefits. For one thing, they are unique and random, helping to prevent brute force attacks. We can also limit their scope depending on needs, and we can revoke them individually without updating other credentials.
Token Setup Guide
To complete the following steps, you'll need to sign up for an account on the GitHub website if you haven't already:
-
Click your profile image in the top-right corner of the GitHub website, and choose Settings from the dropdown menu.
-
Scroll down and select Developer settings on the left, as shown in the following image:
-
Then click Personal access tokens from the left, as shown in the following image:
-
Click Generate new token in the upper right, configure for No expiration, and check the repo box for full control of private repositories, to match the following image:
-
After generating the token, leave the page open. You will need to use the token for authentication in a later step.
Next, follow these steps in your command line:
-
Checkout the branch you want to push to GitHub.
-
Run
git push origin <branch_name>
, pushing your local branches to your shared repository. -
When you receive the
Username for 'https://github.com':
prompt, enter your GitHub username. -
When you receive the
Password for 'https://<username>@github.com':
prompt, copy and paste your personal access token from the GitHub window left open in the previous step. -
You have now pushed the local branch to GitHub, allowing your teammates to access it later!
-
Checkout the
main
branch and then rungit pull
. -
Show students the output and explain that if anyone else had committed and pushed changes to the remote repository,
git pull
would update their local repository with those changes.
Nice work! As you will learn, GitHub is a collaborative tool—you can pull any changes down to your local repository quickly and easily, allowing you to easily share different versions of your code across workstations and to easily checkout and test those versions on your local computer.