Introduction to Git/GitHub

You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://petrock.mit.edu) to authenticate, and then you will be redirected back to this page.

Practice and review what you have learned about Git/GitHub using the following problems! This is not a comprehensive list of practice problems, but we hope it is still helpful!

1) Automated Version Control

Just as a reminder, Version Control is useful for things such as having a thorough record of work, providing a convenient way to undo any mistakes, and allowing multiple people to work in parallel.

2) Setting Up Git

You should configure your git environment for a specific project or all projects using git config.

Type out the flag the git config flag you would use to if you wanted that configuration to be used for all projects (include the --):

Type out the command that would let you see the git config manual:

3) Creating a Repository

Type out the command to initialize a git repository:

You initialized a git repo in directory cooking which contains some files and subdirectories breakfast, lunch, and dinner. You want to track files in your lunch subdirectory, what should you do?

4) Tracking Changes

Type out the command to see the current state of your git repo:

Type out the command to stage the file myfile.txt:

Type out the command to see the git repo commit history:

Type out the command to show changes between commits, commit and working tree, etc:

Your files are staged and you want to record these changes to the repository, what command should you use?

5) Exploring History

what does HEAD~1 refer to ?

Which commands below will let you recover the last committed version of a Python script called data_cruncher.py?

You made a mistake and want to undo the last commit in the project repository. Type out the command to undo the commit with ID 123456789:

6) Ignoring Things

If you don't want git to track certain files or directories, you should use a .gitignore file.

Some things to remember:

  • Order of rules matters
  • The ! character negates a rule
  • You can use wildcards in your rules

7) Remotes in GitHub

Here is a useful page for creating remotes

8) Collaborating

Use git remote -h or git remote --help to find out more about setting up your remote repo.

Type the command to download and automatically merge changes from a remote repo that is conencted to your local repo:

Type the command to download changes from a remote repo that is conencted to your local repo, but it does not automatically merge changes:

You finish working for the day and you stage and commit your changes to your local repo. The next day you want to work on the project some more, but you know that your collaborators have also been working on the project. What is a good practice?

9) Conflicts

Unfortunately, we can't simulate a merge conflict here on CAT-SOOP, but it is important to not be afraid of them.
While it is best to try to avoid merges by:

  • Frequently pulling from upstream
  • Using feature branches
  • Making smaller commits
  • Pushing finished work
  • Breaking large files into smaller ones when appropriate
  • Discussing project responsibilities with collaborators
  • Establishing style rules

Merge conflicts can still happen to the best of us.
Use the following commands as a starting point and explore suggestions on the internet (always make sure you understand the commands you are running before you run them).

  • git rebase -h or git rebase --help
  • git merge -h or git merge --help