Link Search Menu Expand Document

Git

Table of contents

  1. Intro
  2. Getting started
  3. Merge Conflict with an Imaginary Collaborator

Intro

The following are selected chapters from Pro Git, if you are new to Git please read carefully these chapters as they pose the foundation of git.

  1. Getting Started
  2. Getting a Git Repository
  3. Recording Changes to the Repository
  4. Viewing the Commit History
  5. Undoing Things
  6. Working with Remotes
  7. Branches in a Nutshell
  8. Basic Branching and Merging
  9. Branch Management
  10. Branching Workflows
  11. Remote Branches

Also visit Git command reference to get help with commands and command syntax.


The following exercises are designed to help you to experiment and learn how these commands are used in practice.

Getting started

  1. Install Git with (requires internet connection). For this you need to update APT cache and then install the git-core package
  2. Add your name/email to your Git configuration (system-wide)
git config --global user.name YOUR_NAME
git config --global user.email YOUR_MIT_EMAIL_ADDRESS
  1. Generate SSH keys (do not forget the passphrase if you choose to set one)
  2. Add SSH keys to your github.mit.edu account
  3. Create a new repository on https://github.mit.edu
  4. Open a terminal and create a new directory using mkdir named vnav19 in your HOME directory
  5. Clone your (empty) Git repo (earn street cred by calling by using “repo” instead of “repository”)
git clone git@github.mit.edu:USERNAME/REPO.git

Merge Conflict with an Imaginary Collaborator

Now we simulate a common situation that arises when two or more people use the same repo.

  1. Navigate to your repo and create new me.txt with your name and MIT email, e.g.
    $ cat me.txt
    Jon Snow
    lordsnow@mit.edu
    
  2. Check the status with git status
  3. Add (stage), check the status, commit and push your changes – commit message can be “Added my email”
    add me.txt
    git status
    git commit -m "Added my email"
    git push
    
  4. Inspect the log with git log

Now you can go to your repo’s page on Github and inspect the commit history and contents of your repo.

Let’s continue editing the files

  1. Let’s create a new branch
    git checkout -b new_branch_to_merge_later
    
  2. Edit the file me.txt with completely different content, e.g.
    $ cat me.txt
    Arya Stark
    astark@mit.edy
    
  3. Add (stage), check the status, and commit your changes (you can push too if you want) – commit message can be “Somebody added another email”
  4. Now switch branch to master with git checkout master
  5. Inspect the output of git log --graph --oneline --all
  6. Append your course number to the file
    $ echo "Course 16" >> me.txt
    $ cat me.txt
    Jon Snow
    lordsnow@mit.edu
    Course 16
    
  7. Add (stage), check the status, and commit your changes – commit message can be “Added my course number”
  8. Merge the two branches
    $ git merge new_branch_to_merge_later
    Auto-merging me.txt
    CONFLICT (content): Merge conflict in me.txt
    Automatic merge failed; fix conflicts and then commit the result.
    

BOOM 💥. A conflict appears. Thanks, Git for letting us know about this! Let’s resolve the conflict

  1. Inspect the file me.txt, you should see something like - Git helps us by marking the conflict region with special characters: HEAD refers to your current branch/commit and below the ======= the other commit
    $ cat me.txt
    <<<<<<< HEAD
    Jon Snow
    lordsnow@mit.edu
    Course 16
    =======
    Arya Stark
    astark@mit.edu
    >>>>>>> new_branch_to_merge_later
    
  2. In this case, we would like to have Jon name so we simply remove everything else (including <<<<<<< HEAD) from the file
  3. After resolving the conflict, it is time to stage our file and create our merge commit - inspect the log, see the diff, and check the status
    git add me.txt
    git commit -m "Merge commit"
    git push
    
  4. Inspect the output of git log --graph --oneline --all
  5. Inspect the output of git diff HEAD~2 - what does this command do?

Copyright © 2018-2022 MIT. This work is licensed under CC BY 4.0