Skip to main content

Cloning a git workshop

computing
git
github
In which mathematicians pretend to be programmers for an afternoon
Published

June 15, 2016

When I was at the MathBookXML workshop at AIM in April, Rob Beezer gave us a quick two-hour workshop on git and github. Although I had been using it to manage some of my projects, I couldn’t quite understand how it worked in a collaborative environment (which is, after all, what it is chiefly designed for). After a couple of hours, I could finally say that I got git.

At least enough to teach my coworkers. That’s what I did today. I made a fork of Rob’s gitpractice repository, and we all practiced the development process.

Here is the basic flow:

  1. In github, fork the central repository to your own account.

  2. Clone your personal fork to your own machine. The url of your fork can be copied from the repository page on github. In a terminal window run:

    $ git clone <forkurl>
  3. Create a branch of the repository on your clone of the fork, and switch to that branch.

    $ git branch <branchname>
    $ git checkout <branchname>

    Creating a branch does not automatically switch to it! If you want to do both at the same time, at the expense of a more cryptic command, run

    $ git checkout -b <branchname>
  4. Edit files and commit changes to the branch.

    $ git add <changed files>
    $ git commit -m "<message>"

    The message goes into the repository’s log. There are differing conventions on commit message styles. Rob’s advice is to write in sentence predicates (verb + object), with the verb in imperative mood. So something like use display style by default instead of configured to use display style by default. Another convention would be to start with a verb in past tense, but I must admit, after trying Rob’s suggestion for a while, I find it more succinct and easier to write good messages.

  5. Push changes to your fork on github.

    $ git push origin <branchname>

    In many development environments the changes would be merged into the local master branch first, and then pushed to origin. But here we plan to have this branch merged into the central repository, which will eventually reach our local and fork.

    Going back to github you can see that a new branch has been created.

  6. Submit a pull request to have the central repository merge (or “pull”) the changes on your branch.

Everybody got it done. You could say we successfully cloned the workshop. Or is it forked?

The “exciting” part was that one of the pull requests created a conflict. So I got to demo that as the master of the central or base repository. The process is:

  1. Clone the central repository to a local machine

    $ git clone <baseurl>
  2. Create and switch to a temporary branch. The branch can be called anything, but github suggests username-theirbranchname

    $ git checkout -b newbranch
  3. Merge the changes from the fork branch (the one initiating the pull request) into the temporary branch (which has been checked out already).

    $ git merge <forkurl> <forkbranch>
  4. Checkout the master branch and merge in the temporary branch.

    $ git checkout master
    $ git merge newbranch

    This recreates the conflict, only now between branches in the same repository.

  5. Fix conflicts and push up to github.

I wrote up my workshop script (again, stolen from Rob) into the repository’s README.md file. I also created a new organization and moved my repository over to it.

Image by Pete Ashton via Flickr (CC-BY-NC)