Skip to content

Latest commit

 

History

History
245 lines (142 loc) · 7.5 KB

File metadata and controls

245 lines (142 loc) · 7.5 KB

Git & Github

Learning Outcomes

  1. Understand how to take a real project and integrate it with Git/GitHub.
  2. Practice basic pull requests and resolve basic merge conflicts.
  3. Use the GitHub Desktop tool to see your changed files and make commits.

Initial Exercise [5 mins]

Think of 3 challenges you have faced when using Git or GitHub.

Then, share with a partner and compare/contrast your experiences.

Version Control

What is Version Control?

Version control is software that helps a software team manage changes to source code over time. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.

Source: Atlassian Git Tutorial

Benefits of Version Control

Version control allows you to:

  • Review previous versions of your code
  • Revert to a previous version in case of errors
  • Experiment with new features without fear of breaking your code
  • Collaborate effectively & split up work without blocking each other's changes

Git is Decentralized

A centralized file system is like a bank: There is one central "source of truth" with a ledger of all changes made.

GitHub is more like Bitcoin: Every person can have their own version of a repository that is equally valid!

Review: Bash Commands

Bash Commands

How do we do the following in Bash?

  • View the current file path? pwd
  • Make a new directory? mkdir name_of_directory
  • Navigate into (or out of) a directory? cd name_of_directory or cd ..
  • See all files in the current directory? ls
  • Create a new file? touch file_name

Git Workflow

Basic Git Workflow

Here is an example of how to initialize a Git repository:

$ git init
$ git remote add <remote_name> <remote_url>

And how to make changes and push them to GitHub:

$ git add <your_file_name>
$ git commit -m "Update server code and add 2 routes"
$ git push <remote_name> <branch_name>
  • git init will initialize a Git repository inside of the current directory. We can prove it was initialized by using ls -a to see hidden files.
  • git remote add origin adds a remote (short for "remote repository") named origin.
  • git add is like adding our files to a shopping cart. We may not want to check out just yet - this allows us to still make some changes before pushing.
  • git commit makes a commit to our local repository containing our changes. It still won't be reflected in GitHub!
  • git push sends all of the commits we've made to the remote we added. The remote name by default is "origin", and the branch name by default is "master".

Usually, the remote name is called "origin" and the branch name is called "master". It's good to follow this convention unless you have multiple remotes or branches.

Pull Changes

You can pull in changes someone else has made with:

$ git pull <remote_name> <branch_name>

Git Clone

Another way to initialize a repository is to clone an existing one.

$ git clone <remote_url>

Note that if you are not the repository's owner (or a collaborator), you will not be able to push your changes! Instead, create a new, empty GitHub repository and add it as a remote with git remote add as shown above.

Git Debugging

There are a few useful commands to get the "lay of the land" and aid in debugging:

  • git status - Run before and/or after adding files
  • git log - shows previous commits
  • git reflog - shows all changes made to the repository (not just commits)
  • git diff - shows diffs in untracked files

gitignore

Add a file called .gitignore to specify any files that you don't want to be tracked. For example, my .gitignore file might include the following contents:

env/
__pycache__
.DS_STORE

Check out gitignore.io to automatically generate a .gitignore file!

Activity: Set up your Homework Repository [5 min]

If you haven't yet, initialize a repository in your BEW1.1 Homework folder, and follow the steps to push your changes to GitHub.

Break [10 minutes]

Activity: Create a Merge Conflict

What is a merge conflict?

Merge conflicts occur when you attempt to pull in changes that conflict with (i.e. change the same lines as) your changes.

They are a normal part of working with Git and it's good to practice them!

Let's Create a Merge Conflict! [10 mins]

In your homework directory, change a few lines of code, add, and commit (but don't push).

Then, go to the corresponding GitHub repository and change those same lines of code in a different way.

Try git pull origin master. You should get a merge conflict! See if you can resolve it.

Merge Conflicts with a Partner [15 mins]

Pair up with someone you haven't worked with yet.

Clone your partner's repository. Then, both make changes to the same lines of code and try to push your changes. See if you can resolve the merge conflict!

After resolving, commit and push your final changes. Now you should both see the same code.

GitHub Desktop

What is GitHub Desktop?

GitHub Desktop is a desktop client that takes a lot of the pain out of using command-line Git. It will help you do all of the basic commands - add, commit, and push - and will give you visual reminders of what changes you have made.

Screenshot of GitHub Desktop

Why GitHub Desktop?

It is...

  • Easy and straightforward to use
  • More visual
  • Shows side-by-side file diffs

How do I install it?

Download for MacOS!

Demo

Homework

  • Homework 1 - Due Monday at midnight. Link your repo in the Progress Tracker to submit!

Additional Resources

  1. Atlassian Git Tutorial - highly recommended if you want a deeper dive into Git, but most of it is not necessary for your day-to-day work
  2. Fixing Common Mistakes (video)
  3. MS Branching tutorial
  4. Info in commit messages
  5. More sources on resolving merge conflicts