View on GitHub

presentations

Presentation notes from JMU Unix Users Group meetings

Git 101 - Warmup

bg 60% right Take a moment to create a GitHub account, or verify your password.

Please post your username in Discord (UUG welcome over there –> ) so we can get a little social network going.

Follow me @ripleymj.


What is git?


What is version control?


“Distributed”?


What’s the alternative?


So what about GitHub?


So what about GitHub?


What is git?


Branch naming


Help along the way


Demo Time

We’ll work through some git basics with a partner. Player 1 should be the person with less git experience, and player 2 the more experienced.


All: Understanding GitHub security

GitHub does not allow plain password use from the command line

  1. Are you on a computer you frequently use?
    • Yes? Use SSH keys
    • No? ssh to stu, use SSH keys
    • No? Generate a disposable personal token
  2. Do you already have SSH keys? ls -l $HOME/.ssh and look for id files
    • Yes? Cool!
    • No? Time to generate some

All: Generating SSH keys


All: Uploading SSH keys


All: Basic git configuration

By default, git will have your name and email configured to be your login name@hostname. This is nice in some ways, but probably not your actual email address. Let’s fix this. Make sure you substitute all of your own info in the following commands:

git config --global user.name "Your Name"
git config --global user.email "your@email.address"

Now git will always know who you are, and this information will be in all your commits.


P1: Starting your first git repository

First, we will create a directory to use for this tutorial.

mkdir -p ~/UUG/uug-git-intro && cd $_

If we try to use a git command here, we will get an error:

git commit -am "Test commit"

Notice that you got an error when you tried to do this! You cannot create a commit in git without that directory being a part of a git repository.


P1: Starting your first git repository

Let’s initialize your repository.

git init

If you run ls -al, you will now see a .git directory there. This is where git stores all of the data about your repository. Do not go in here and make changes without really knowing what you’re doing.


P1: Making your first commit

Commits are basically a record of changes to your directory. Each time you make a change, you need to tell git to create a new record. You do this with the commit command.

Before you commit, you have to make a change to the directory. Let’s create a file:

nano README.txt

P1: Making your first commit

Now that we have created a file, we can ask git to tell us the status of of our directory.

git status

Normally git can tell exactly what’s different in the file.

git diff

But that only works against the last commit, so it will not show our README.


P1: Making your first commit

We should instruct git to start tracking changes to our README.txt file. We also need to add it to our ‘stage’. To do this, we will use the following command.

git add README.txt

Let’s tell git to create a record of our change

git commit

Vim (or your default editor) will open. Type a description of your changes, called a commit message, and save the file (in Vim, you can type :wq in normal mode).


P1: See a record of commits

git keeps a record of each of your changes in a log. To see this:

git log

P1: Changing branch name

git log should show that git has automatically chosen the name master for your primary branch. Let’s change that now.

git branch -M main

All: About GitHub URLs

When connecting your local repository to GitHub, there are two types of URLs you can use.

This should make more sense in a few minutes. If you change your mind in the future, you can update URLs with git remote set-url.


P1: Creating a repository on the GitHub website

Using the + button in the top right corner of the webpage, choose “New Repository”. Pick a name and optionally a description. For this tutorial, we will not be using a pre-provided README, .gitignore, or license.

Once you’ve created the repository, select the HTTPS or SSH button towards the top of this guide, and notice the section for push an existing repository.


P1: Starting your first git repository

You now have to tell git where your code should be pushed to. We are using GitHub for this. Run the following command, but change YOURUSERNAME to your

git remote add origin git@github.com:YOURUSERNAME/REPONAME.git

Remember later that you have named your GitHub URL origin.


P1: Synchronizing GitHub

The first time you push to an upstream copy, you can use the -u option to tell git your preferred default.

git push -u origin main

This will tell git to push your current branch main (since you just renamed it) to the main branch at the origin URL, and to remember that for future use. Now that you’ve set the default, you can simply use:

git push

Go check GitHub now to confirm your changes were uploaded.


P1: Inviting a GitHub friend


P2: Cloning a repository


P2: Making a bad change

Let’s modify our directory and commit that change.

git rm README.txt
git commit
git push -u origin main

P1: Reverting a commit

Player 1 runs git pull and sees this terrible change and wants to undo this. Git can take the state of the repository at a point in time and bringing it back to the present.

So what we need to do is check our log, and find the hash of the commit we want to revert. Use the following command (replacing the hash):

git log
git revert "hash"

This will create a commit with the default message, ‘Revert “Old Commit”’. Do not push yet.


P2: Keeps working and pushes another change

nano BetterReadme.txt
git add BetterReadme.txt
git commit
git push

P1: Push, with conflicts


All: Time permitting, create and resolve a merge conflict


gitignore


A note about danger


More fun with GitHub


Going futher

For a git reference, check out GitHub’s Git cheat sheet or Git cheat sheet for education