In this unit, we will dive into specific usage of git. This is meant to be a “getting started” tutorial. This will not cover all advanced usage of git. However, there are a number of more robust tutorials to be found online.
In this unit you will login to Github in IntelliJ. Typically, you cannot use your GitHub password to login to GitHub via a third party app.
If you are having any login issues, it’s most likely that you are not logging in with a personal access token.
Review the documentation here for how to create a Personal Access Token.
When you need to select permissions, you should give the following:
Then, once you have the personal access token, you can use it as the password in either IntelliJ or any other program that requests an email/password to connect to your GitHub account.
When we create a new project, one of the first files we want to commit, even before we start writing code, is a .gitignore file. A .gitignore file tells git which files not to commit relative to its current location.
Consider the following example .gitignore
# Gradle related folders
# Local settings files
# Ignore java compiled files
# Don't ignore gradle wrapper jar
This is an incomplete excerpt from this .gitignore file We will discuss this project and gradle in a later unit. You are welcome to use the linked example as a .gitignore file, but I would encourage you to delete lines 1-3, as ignoring Excel files is unique to that project.
Here we are telling git to never commit the folders
is because we only want source code on our remote repository. We do
not want to include compiled files, as compiled files are much larger, and
unnecessary to upload and download, as we will build the compiled files
locally on our workstations.
We also want to ignore local settings files like
.classpath (which is used
by Eclipse) and
.idea/workspace.xml (IntelliJ), as these files tell our
IDEs things like where to find the JDK and JRE on our computer, or how
our IDE is configured for our personal programming. It’s very
important to .gitignore these files, as where my Java installation is
on my computer will almost certainly not match your computer. If we commit
these files, then when anyone pulls, it’s going to overwrite their
own local settings files and could create anomalous results. Further, these
files are not necessary for being able to build the source code, and
we want to limit our git repositories to storing only what is necessary
for building the source code locally.
Be aware that .gitignore files are not retroactive. .gitignore only prevent future commits from adding a file to the repository. It will not remove files that are already in the repository.
Below are a few simple ways to create a git repository. Please note that for in-class homework, Github Classroom with make a repository for you. As such, you will not do these steps for the homework.
git init to create a git repository. Then, as you
If you already have a project, and you want to start a repo, there’s a couple ways to do it.
git init as Step 3 under creating a git repository, but in the folder
containing the content you want to add to a repo. Then (after you setup
your .gitignore file), simply use
git add and git commit to add all the files in the folder to the repo.
This is the first step you will do in the homework assignments.
Cloning means downloading an existing remote repository to your local machine.
For simplicity, we’re going to focus on clone a repository from GitHub, like
this one. Each remote
repository on GitHub has a
.git link that is used to clone a repository.
You can find it under the Green “Code” button towards the top right
of the repository’s home page.
If you are comfortable using SSH, you are welcome to use that, but otherwise I generally encourage starting out with the HTTPS link. SSH can be more secure and convenient, but requires learning how to set up an SSH key.
.git link for the repository, then we can
use that .git link as the Repository URL in “Repository URL” in IntelliJ.
From there, it works the same as option 1.
git init [url] where [url] is the git link for the repository
you want to clone.
Generally, the easiest way to do this is going to be in IntelliJ. First, you can only push a repository to GitHub if it has at least one commit. Additionally, you should ensure you commit a .gitignore file before you commit any other files.