git initLab 2 - GitHub
Learning goals
In this lab, you are expected to learn/practice the following skills:
- Forking a repository on GitHub
- Git workflow: clone/commit/push
- Using pull requests (PRs)
Note: We are assuming that you already installed git in your system.
Part 0: Introduce yourself
Set up your git install with git config, start by telling who you are
$ git config --global user.name "Juan Perez"
$ git config --global user.email "jperez@treschanchitos.edu"Try it yourself (more on how to configure git here)
This will change the Git configuration in way that anytime you use Git, it will know this information.
Note that you need to use the email account that you used to open your GitHub account.
Part 1: First repository
We will start by working on our very first project. To do so, you are required to start using Git and Github so you can share your code with your team. For this exercise, you need to
- Log into GitHub and click on the “plus” icon (“Create new…”) in the top right, then select “New repository”
- Give your repo a name, like
BIOSTAT620-first-project, tell GitHub to add aREADMEfile, and click “Create repository”
Connect local repo to GitHub
To connect working directory to the GitHub repo
- initialize the directory on your local machine (Note: be sure to do this from a specific folder in command line where you would like your project to be, and not your home directory):
From your repository’s page, click the green “Code” button and copy the remote repository URL, which should end with `.git. We recommend that you use the HTTPS URL (see below).
Let Git know what is the remote repository URL.
git remote add origin <remote-url>You now have a local version of your repository!
origin is a nickname we will use for the remote. We can call it something else, but everybody calls it origin so best to stick with that.
Connect credentials with HTTPS or SSH
When accessing GitHub you need credentials to verify your identity.
There are two ways to connect: HTTPS or SSH, each requiring different credentials.
We recommend using HTTPS, which uses a Personal Access Token (PAT).
Note that your GitHub website password isn’t your access token.
Instructions
Detailed instructions are here.
Go to your Github user (not repo) settings, then Developer Settings, then personal access tokens.
Click Tokens (classic), and generate a new classic token.
Give your token a note, and choose the expiration. 90 days is more secure, but non-expiring is more convenient. Select the repo option in the scopes section.
Finish Generating a token:
Once you complete these steps, GitHub will display your token—a lengthy string of characters.
Immediately copy this token to your clipboard. This is the only time GitHub will show it to you.
For security, save this token in a password manager. This ensures you can access it if needed later on.
When git prompts you to enter your password, paste the token you’ve copied. After this, password prompts should no longer appear.
If you ever need the token again, retrieve it from your password manager.
If you are on Mac, then we recommend for security and convenience that you use osxkeychain to store the credentials for git, rather than a plaintext file, using the following command:
$ git config --global credential.helper osxkeychain
More details available from Happy Git and GitHub for the use.
Making changes
Now, let’s make some changes!
- Begin by syncing up your local repo to match the Github repo:
$ git pull origin main- Open the README file in a text editor and add a brief description of the project (this doesn’t have to be accurate, just add some text), then save your changes. If you check the
git statusnow, you’ll see that you have unstaged changes. - Add your changes to the staging area with
git add README.mdorgit add --all. If you check thegit statusnow, you’ll see that you have staged changes, ready to commit.
Note: Need a text editor? Checkout this website link.
- Make the first commit using the
git commitcommand adding a message, e.g.
$ git commit -m "My first commit ever!"If you check the git status now, you’ll see that you are 1 commit ahead of the remote repository (GitHub).
- Update your remote repository (on GitHub) with
git push. If you check thegit statusnow, you should see that you are fully up to date. (You may need to set the upstream repo as:git push --set-upstream origin main).
Note: if you run into a permission error, then you need to correctly configure your credentials (see above).
- In your browser, refresh the page for your repository and see if your changes to the
READMEfile are there!
Removing a file
Oops! It seems that I added the wrong file to the tree. You can remove files from the tree using git rm --cached. For example, try creating and adding for Git staging the file class-notes.txt (which you are not supposed to track), then you can remove it from staging using
$ git rm --cached class-notes.txtThis will remove the file from the tree but not from your computer. You can go further and ask git to avoid adding .docx files using the .gitignore file
.gitignore use-case
I like to have my data and code for a project all in the same place, but I don’t want to upload the data to GitHub, as this would exceed the size limit on a repository.
Open (or create) the .gitignore file in a text editor and add the following line to ignore the directory (which you should create) called data:
/dataTry git status before and after making this change to .gitignore.
More examples for .gitignore
Telling git to ignore files is a good way to make sure you don’t go over your storage limit on GitHub. It’s also just a convenient way to avoid unnecessary clutter. Example based on Pro-Git (link).
# ignore specific file (something.pdf) something.pdf # ignore all .png files *.png # but do track bird.png, even though you're ignoring .png files !bird.png # only ignore the TODO file in the root directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory and any of its subdirectories doc/**/*.pdf
Part 2: GitHub Desktop live demo
Let’s do the same sequence of tasks we just performed, but this time, using GitHub Desktop.
Footnotes
Team-members could be working on the same file but editing different lines of code. If this is the case, after pull/push, git will integrate the changes without conflicts.↩︎
For more details, take a look at the Forking Projects article in GitHub guides.↩︎
More info about how to deal with conflicts in this very neat post on stackoverflow.com How to resolve merge conflicts in Git. GitHub also has a way to solve conflicts in PRs, but this is only available to the admins of target repo. More info here,↩︎