View on GitHub

presentations

Presentation notes from JMU Unix Users Group meetings

Git 101 - Warmup

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

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

Follow me @ripleymj.


Roadmap


What is git?


What is version control?


“Distributed”?


What’s the alternative?


Git ecosystem survey


height:15cm https://survey.stackoverflow.co/2022/


height:15cm https://survey.stackoverflow.co/2022/


height:15cm https://survey.stackoverflow.co/2022/


height:15cm https://survey.stackoverflow.co/2022/


So what about GitHub?


So what about GitHub?


What is git?


A note on branching and language


Help along the way


Demo Time


Understanding GitHub security

GitHub no longer allows 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

Disposable “Personal Access” Token


Generating SSH keys


Uploading SSH keys


SSH key bonus feature


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"
git config --global color.ui true

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


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.


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.


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.


Creating a repository on the GitHub website

Using the + button in the top right corner or 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. Remember the name; you will need this later!

Select the HTTPS or SSH button towards the top of this guide, and notice the section for push an existing repository. No need to do anything with this now. Just keep the page open.


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 https://github.com/YOURUSERNAME/REPONAME.git

Remember later that you have named your GitHub URL origin.


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

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.


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).


See a record of commits

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

git log

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

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.


Reverting a commit

As you’re making changes, you very often will want to undo one of your previous commits. git allows for this with its revert command.

Let’s modify our directory and commit that change.

git rm README.txt
git commit

Later, we realize that we regret this change. We can undo this. The way that reverting works in git is by taking 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”’. Push this change back to GitHub:

git push

gitignore


Hacktoberfest

Stay tuned for Hacktoberfest where you can win prizes for contributing to projects on GitHub during the month of October


Going futher

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