Skip to content

Latest commit

 

History

History
255 lines (197 loc) · 8.14 KB

git.md

File metadata and controls

255 lines (197 loc) · 8.14 KB

Table of Contents generated with DocToc

to-do list: Dollars to Cents, Gallons to Liters, Hours to Minutes, Feet to Inches, Degrees to Radians

0. Prerequisites

Make sure (1) you have git in bash and (2) get an account with github.

1. Collaboration problem

  • File locking has its downsides.
  • Better use automatic version control!
  • Possible uses: collaboration, keep track of changes and logs, backup, transfer files between systems.

Types of repositories in git:

  • regular git repository -- we'll study only these
  • bare repository
  • git bundle

2. Setup

$ git config --list
$ git config --global user.name "Alex Razoumov"
$ git config --global user.email "[email protected]"
$ git config --global color.ui "auto"
$ git config --global core.editor "nano -w"
$ git config --list

Exercise: configure git on your laptop.

3. First edit

$ cd   # we'll start from the home directory
$ mkdir planets
$ cd planets
$ git init    # only once for each project and its subdirectories
$ git status
$ nano mars.txt   # add the first line to mars.txt
$ git add mars.txt
$ git commit -m "started notes on mars"
$ git log

Question: how would you undo 'git init' inside a subdirectory?

4. Second edit

$ nano mars.txt   # add a second line to mars.txt
$ git status
$ git diff
$ git commit -m "continued mars notes"   # likely will be getting an error message

Need to stage changes before committing! There are two solutions:

  • either
$ git add mars.txt   # add mars.txt again to the staging area
$ git diff   # difference between working copy and staging area
$ git diff --staged   # difference between staging area and repository
$ git commit -m "continued mars notes"
  • or
$ git commit -a -m "continued mars notes"   # automatically add files that are already being tracked

Exercise: create bio.txt, add 3 lines, commit to the repository, modify first line, add line 4, display changes, commit to the repository.

5. Explore history

$ git log

HEAD is the latest version in the repository, HEAD~1 is the previous, ...

$ git diff HEAD~1 mars.txt
$ git diff HEAD~2 mars.txt
$ git diff specificVersionNumber mars.txt   # version numbers have 40 characters
$ git diff specificVersionNumberFirstFewUniquesCharacters mars.txt

Accidentally removed or changed mars.txt. How do we restore it?

$ git checkout HEAD mars.txt   # restore latest from the repository
$ git checkout specificVersionNumber mars.txt   # restore specific version from the repository

6. Ignore things

$ mkdir results
$ touch a.dat b.dat c.dat results/a.out results/b.out
$ git status
$ nano .gitignore   # add *.dat and results, one per line
$ git status
$ git add .gitignore
$ git commit -m "Add the ignore file"
$ git status
$ git status --ignored   # show the ignored files

7. Remotes

Why remotes?

There are many different types of remotes: departmental git server, usb key, git bundle, github.

Log into github.com, create a new repository called "planets", copy the repository URL.

$ git remote add origin https://github.com/razoumov/planets.git # make the github repository a remote
$ git remote
$ git remote -v
$ git push origin master   # to upload to remote, should ask for password.

If github is confused about your identity, can always specify your github username by hand:

git remote set-url origin https://[email protected]/razoumov/planets.git

Check your github repository in the web browser.

8. EXERCISE collaborating: get into pairs

Public github repositories provide read access to everyone, but only the owner can write to a repository. However, the owner can give write access to his/her collaborator.

Open your github repository for writing to your partner: your profile -> repository -> settings -> collaborators and add their github name.

Everyone, please clone your partner's github repository:

$ cd   # change to your home directory
$ pwd   # should not show 'planets'
$ git clone https://github.com/yourPartner/planets.git partner
$ cd partner
$ nano question.txt   # make some changes to this file, e.g., ask a question
$ git commit -m -a "describe your changes"
$ git push origin master

Then each one of you will download your partner's edits:

$ cd ~/planets
$ git pull origin master
$ cat question.txt    # you should see your partner's edits

9. EXERCISE conflicts

Two partners: pick one of the two repositories, synchronize them. Next, each add a line at the end of the same file. First, partner 1 pushes to the repository. Next, partner 2 tries to push to the repository ... and gets an error. Partner 2 needs to resolve conflict before he can push.

$ git pull origin master
$ cat contestedFile.txt
$ nano contestedFile.txt   # edit to resolve conflict: it's up to us what to do
$ git -a -m "resolved"
$ git push origin master

10. Collaborating through git pull requests

I will only briefly mention this. This is the feature of github, not git. A pull request has a formal review interface with line comments and other advanced features, and is a better way to push changes into large github repositories with more than a couple of users than a direct push.

laptop
git clone https://github.com/username/repository.git
git checkout -b newBranchName   # create a new temporary branch
work there
git commit -am "describe changes"
git push origin newBranchName
go to the repository's web interface and make a "new pull request" to the master brach

Alternatively, you can fork a existing repository (to which you want to commit) online, clone it locally, work on it, push to your fork, and then create a pull request on the original.

11. Putting together a simple web page

We'll now write some static pages with GitHub pages and Jekyll, using Jekyll Now as a template.

Go to https://github.com/barryclark/jekyll-now, click Fork at the top-right. Once forked, go to the repository settings and rename it to username.github.io. After the first "git commit + git push" (below) you'll be able to view your new page at http://username.github.io. Also on mobile! Now let's customize it!

$ cd   # change to your home directory
$ pwd   # should not show 'planets' or any other directory containing a git repository
$ git clone https://github.com/razoumov/razoumov.github.io.git
$ cd razoumov.github.io
$ nano _config.yml   # set name, description
$ git status
$ git commit -am "changed the name"
$ git push   # after this reload the website

How do we add a new post?

$ cd _posts
$ cp 2014-3-3-Hello-World.md 2016-01-17-test.md
$ nano !$
$ git status
$ git add !$
$ git commit -m "added a post"
$ git push   # after this reload the website

For other themes, scroll down to "Other forkable themes" in README.md -- each template is a separate repository.

Exercise 1: delete the "You're up and running!" post.

Exercise 2: write few words about yourself in ../about.md in upload them to your website.