Skip to content

Instantly share code, notes, and snippets.

@johnsogg
Last active June 7, 2024 15:56
Show Gist options
  • Save johnsogg/56df0f970bd9e019da861fd37ceb42f8 to your computer and use it in GitHub Desktop.
Save johnsogg/56df0f970bd9e019da861fd37ceb42f8 to your computer and use it in GitHub Desktop.
Git and GitHub Crash Course

Git and GitHub Crash Course

This crash course is meant for my CS 2270 students at CU-Boulder. It might be helpful for others, but there might be course-specific content in here. The internet has goo-gobs of tutorials for git. Students: you should totally check those out! I'm making this as a sort of one-stop-shop for your 2270 needs. If you need advanced documentation, the git website has more than you could ever want.

If you've not familiar with command line interfaces, you might want to read the command line interface crash course to help you understand this.

Git is an open source source code management tool, and it is easily the most widely used tool in its category. GitHub is a company that provides git-related services, including repository hosting, user and team management, security, and collaboration interfaces. Git exists independently of GitHub. There are other systems like GitHub such as Gitlab, BitBucket. We happen to use GitHub in this course. GitHub is a good tool to know, but git is essential.

I'll start with the helpful "cheatsheet" stuff first, and then launch into a flowery exposition of various git-related concepts. If you want to know more about a line in the cheet sheet there might be a section below that expands on the ideas.

Cheat Sheet

(recall that $ represents the CLI prompt and # is a comment that extends to the end of line - see the CLI thing if you need help making sense of this)

$ git clone git@github.com:cu-cspb-2270-Fall-2023/pa8-johnsogg.git # clone a repository using SSH
$ git clone https://github.com/cu-cspb-2270-Fall-2023/pa8-johnsogg.git # clone a repo using HTTPS
$ git status # print a list of interesting files: new, modified, deleted, tracked or untracked
$ git add . # adds all new/updated files in this dir to a changeset
$ git add my_awesome_code.cpp # adds a single file to the changeset
$ git add -u # adds all updated (modified) files that git knows about
$ git commit # commit all changeset files to a new local git revision - it prompts you for a message
$ git commit -m "I perfected my code!" # commit all changeset files to a new git revision with a message
$ git push # pushes local commits to the 'remote', which is GitHub in our case
$ git fetch # asks the remote for a list of branches
$ git checkout -b some-feature # create a new branch called some-feature and switch to it
$ git checkout some-feature # switch to a branch called some-feature that already exists

Branches

If you collaborate with other people, or if you just want to keep your experimental changes separated from other code, you can use branches. A branch is for storing changes to code, starting at some commit. It is kept separate until it is merged back to another branch.

Git doesn't force you to use any particular strategy for branching and merging, but this is a pretty typical flow:

  1. The primary branch (sometimes called the "trunk") can be called main. It used to be called master but we prefer main now. This is the branch that you check out by default when you clone a repository.
  2. To create a new branch based on the commit that you're on right now, use git checkout -b my-feature-name and it will create a branch called my-feature-name. Obvs use whatever name you want there.
  3. Then you can use the edit/add/commit/push commands mentioned above, except you're working on the branch, even when you push it to the remote.
  4. To bring changes from a branch into main, you can either...
    • create a pull request, which is a Github-centric term, and is available on the Github UI. or...
    • Check out main locally, merge your branch locally, and then push it to the remote:
      • git checkout main
      • git merge my-awesome-branch
      • git push

Steady State Commands

When you're in the thick of programming, your general flow will look like this:

$ git clone https://github.com/johnsogg/my-awesome-project.git
$ cd my-awesome-project                        # Be sure you're in the directory that you just cloned
$ git checkout -b whizbang-feature-x           # Create and check out a branch for whizbang feature X
$ code .                                       # Edit some code...
$ git add -u                                   # Stage all modified files that git knows about
$ git commit -m "Perfect and bug-free code".   # Commit staged changes locally to your branch
$ git push                                     # Send feature branch changes to the remote (e.g. to Github)
$ git checkout main                            # Check out main branch without the -b since it exists
$ git merge whizbang-feature-x                 # Merge contents of feature branch into main
$ git push                                     # Send main branch changes (with merge) to remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment