Git
Table of contents
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.
- Getting Started
- Getting a Git Repository
- Recording Changes to the Repository
- Viewing the Commit History
- Undoing Things
- Working with Remotes
- Branches in a Nutshell
- Basic Branching and Merging
- Branch Management
- Branching Workflows
- 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
- Install Git with (requires internet connection). For this you need to update APT cache and then install the git-core package
- 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
- Generate SSH keys (do not forget the passphrase if you choose to set one)
- Add SSH keys to your github.mit.edu account
- Create a new repository on https://github.mit.edu
- Open a terminal and create a new directory using
mkdir
namedvnav19
in your HOME directory - 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.
- 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
- Check the status with
git status
- 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
- 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
- Let’s create a new branch
git checkout -b new_branch_to_merge_later
- Edit the file
me.txt
with completely different content, e.g.$ cat me.txt Arya Stark astark@mit.edy
- Add (stage), check the status, and commit your changes (you can push too if you want) – commit message can be “Somebody added another email”
- Now switch branch to master (or main) with
git checkout master
(orgit checkout main
) - Inspect the output of
git log --graph --oneline --all
- Append your course number to the file
$ echo "Course 16" >> me.txt $ cat me.txt Jon Snow lordsnow@mit.edu Course 16
- Add (stage), check the status, and commit your changes – commit message can be “Added my course number”
- 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
- 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
- In this case, we would like to have Jon name so we simply remove everything else (including
<<<<<<< HEAD
) from the file - 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
- Inspect the output of
git log --graph --oneline --all
- Inspect the output of
git diff HEAD~2
- what does this command do?