Intro to git workshop

A 4 hours introduction to git workshop for my PhD fellas. Based on an old git crash course blog post.

git logo

Why?

  • Keep your projects organized.
  • Collaborate with others.
  • Get involved with open source.

Command what?

Introduction to the command line. From now on, the rest is done there.

Working locally

When git manages a directory on your computer we call this directory a local git repository.

There are two ways to start to work in such a repository:

  • git clone https://github.com/some_user/some_repo.git to download a project into a new directory some_repo.
  • git init to make the current directory a git repository.

Unstaged ➜ staged ➜ committed

git local workflow

Remember! you can always see the current state and the staging / unstaging commands with git status, so don't try to memorize them.

When you are satisfied with the changes commit them:

git commit -m "an informative message describing your change"

Exercise

  1. Create a directory called jokes.
  2. Make it a local git repository.
  3. Create a file called jokes.txt in that directory.
  4. Write some funny stuff.
  5. Add and commit. Try to have an informative commit message.
  6. Repeat steps 4-5 as many times as you want 😄.

Explore your repo

git log                 # see the history
git diff                # see the unstaged changes
git diff --staged       # see the staged changes
git show <COMMIT_HASH>  # see the changes in a commit

Collaborating through GitHub

GitHub is a place to share and collaborate on git repositories. Go head and create an account!

Your local git repository can be "linked" to remote repositories. To see them run git remote (or with extra info using git remote -v). If you cloned an existing repository you should see one remote, called origin, in the list. Otherwise, create a new GitHub repository and add it as a remote with:

git remote add origin https://github.com/you/your_repo.git

BTW, origin is just a default name. It has no special meaning.

pull

To get the latest changes (commits) from your remote run:

git pull origin master

What's master? The name of the default branch. More on that later.

push

If you have permissions to push code to the remote repository you can update the remote with your changes (commits) by running:

git push origin master

Remember! Always pull before you push to avoid unnecessary conflicts.

git Austin Powers meme

Assuming that you already have a local repository with a remote (called origin) that you can push code to, a simple but complete workflow might look like that:

git pull origin master         # to get the latest changes
# work work work...
git status                     # to see all of the changes you did
git diff                       # optional but handy
git add FILE_WITH_CHANGES      # repeat as necessary
git commit -m "your message"   # commit the changes to the repository
git push origin master         # to upload your changes

Exercise

  1. Create a new GitHub repo, called jokes.
  2. Add it as a remote for your local jokes repo.
  3. Push your jokes to GitHub.

Forks and pull requests

If you don't have permissions to push code to a remote repository.

  1. Fork the repository. It will copy the remote repository to your account.
GitHub fork button
  1. Add your fork as a remote.
git remote add mine https://github.com/you/your_repo.git
  1. Work on your fork.
  2. Send your commits to the owner(s) of the projects using a pull request.
GitHub pull request button

Exercise

Follow this exercise. But! with this remote repository instead.

Where to go next?