1 of 51

Intro to Git

2 of 51

Link to Slides

http://tinyurl.com/5zn2xfad

➡️ ➡️ ➡️

⬅️ ⬅️ ⬅️

README: http://tinyurl.com/into-to-git-README

3 of 51

Introduction ❤️‍🔥

README: http://tinyurl.com/into-to-git-README

4 of 51

What is Git?

Git is a Distribution Version Control System (VCS)

  • Used to track changes in source code by taking “snapshots” at each step of development

README: http://tinyurl.com/into-to-git-README

5 of 51

What is Git? 🤔🤔🤔

What does VCS really mean… 🤨🤨🤨

Think of Git like a Google Doc:

  • You are assigned a group project
  • A google doc is made so multiple people can work remotely on the same project
  • Each person is assigned a different part of the project to work on
  • Each time a change is made, Google Docs saves the changes (and a list of edits)
  • Uh oh, someone makes a mistake
    • Google Docs allows you to revert the current document to an old version
    • No worries about big mistakes!
  • When it is time to turn in the project, each individuals changes are saved onto the same document

README: http://tinyurl.com/into-to-git-README

6 of 51

What is Git?

Git works in a similar way to Google Docs, but in a more structured format

Git allows …

  • Multiple developers to work together:
    • Developers can work on multiple versions of the same source code individually
    • Changes in code made by multiple people can then be merged together

  • A way to keep track of code history:
    • Each time a change is made, git keeps a log of what changed, who changed it, and when it was changed
    • At any time, you can revert unwanted changes in code

README: http://tinyurl.com/into-to-git-README

7 of 51

Benefits of Git

  • Team Collaboration
    • multiple developers working on the same project simultaneously!
  • Track progress and history
  • Experiment on code by creating new branches
  • Work remotely
  • Reliable backups
  • Fast merging of code

💜

Hack

Git

README: http://tinyurl.com/into-to-git-README

8 of 51

Installation + Configuration✌️

README: http://tinyurl.com/into-to-git-README

9 of 51

Installing Git (Linux or Windows)

Linux:

$ sudo apt-get install git-all

Windows:

Click this link

** commands that start with “$” are meant to be run on the terminal (cmd for windows). Ignore the “$” when actually running the command **

README: http://tinyurl.com/into-to-git-README

10 of 51

Installing Git (Mac)

Step 1: Install Homebrew Package Manager

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install Git using Homebrew

$ brew install git

** commands that start with “$” are meant to be run on the terminal (cmd for windows). Ignore the “$” when actually running the command **

Homebrew simplifies the process of installing software on Mac by automating the download, installation, and configuration of packages

README: http://tinyurl.com/into-to-git-README

11 of 51

Configuring Git

Git asks for information about you so others know who is doing what changes on a coding project. Configuring Git is the process to telling Git the information needed to collaborate with others on projects.

Commands to Paste:

$ git config --global user.name “John Doe”

  • Logs your name

$ git config --global user.email johndoe@example.com

  • Logs your email

“git config” sets Gits options with user information and default behavior

README: http://tinyurl.com/into-to-git-README

12 of 51

Demo 👩🏻‍💻

Time to install and configure git!

README: http://tinyurl.com/into-to-git-README

13 of 51

Git vs. GitHub 😯🤟

README: http://tinyurl.com/into-to-git-README

14 of 51

Git vs. GitHub → they are different! 🙅🏻‍♀️🙅🏻‍♀️🙅🏻‍♀️

Git:

A version control system that manages and keeps track of code history.

GitHub:

A platform hosted on the web that creates online Git repositories to save code history and shares it with other developers. GitHub provides additional features on top of Git that ease the process of collaborating with others

  • GitHub is a platform or a service that uses servers as part of its infrastructure to host, share, and manage code repositories using Git version control

README: http://tinyurl.com/into-to-git-README

15 of 51

Git Workflow

README: http://tinyurl.com/into-to-git-README

16 of 51

Git: Three Main States

  1. Working Directory
    1. The files on your personal device
  2. Staging Area
    • All the changes that you want to make a “snapshot of”
  3. Repository
    • Collection of snapshots

README: http://tinyurl.com/into-to-git-README

17 of 51

Working Directory

  • Where you do all your work: edit files, add new ones, and delete other ones
    • Essentially your project folder on your local machine

How does this relate to git?

  • You need to tell git to track the changes to the files on your working directory!

Jim is on his working directory

README: http://tinyurl.com/into-to-git-README

18 of 51

Staging Area

  • The staging area will hold all of the changes that git will include in the next snapshot
    • Similar to a rough draft space for your next commit
  • These are all the changes that have been made since your last commit
  • Think of it as an intermediate area between where you make changes to files (working directory) and the Git repository (where changes are permanently recorded)

README: http://tinyurl.com/into-to-git-README

19 of 51

Repository (Repo)

  • A location where all of the files of a project are stored
    • All of the previous versions of each file are also stored in the repo
  • Holds snapshots which take all of the files that have been staged and make a permanent record in the repo
    • state of a project’s files at a particular point in time
  • A repository contains everything needed to track changes made in a project
  • A new repository can be made for new projects

README: http://tinyurl.com/into-to-git-README

20 of 51

Repo Visual

⬅️ version 2 has an additional 2 files, files 4 and 5, but is stored in the same area as version 1

README: http://tinyurl.com/into-to-git-README

21 of 51

Local vs. Remote Repository

Local:

  • Repository stored on your local machine
  • Can work on the local repository when offline!

Remote:

  • Versions of a project hosted on the internet like GitHub
  • A location where you can push local changes
  • Allow multiple developers to work on the same project
    • Keeps track of individuals changes and synchronizing the work

Vs.

README: http://tinyurl.com/into-to-git-README

22 of 51

Review of the 3 Main States

README: http://tinyurl.com/into-to-git-README

23 of 51

Terminology 🏃🏻‍♀️

Now that git is installed, what do all these words mean?

README: http://tinyurl.com/into-to-git-README

24 of 51

Clone

  • Cloning is the process of creating a repository from a remote server (like GitHub) onto your computer
  • By cloning a repositories, you have access of all of the files and can then work on code locally
  • Cloning repositories is a useful when working on a team project
    • Each team member gains a complete copy of the repository
    • This ensure everyone is working with the same codebase
      • Each member can then work individually on different parts without interfering with each other’s work

🦖🦖🦖🦖🦖🦖🦖

README: http://tinyurl.com/into-to-git-README

25 of 51

Branch

  • A branch is a movable pointer that represents an independent line of development
  • The “main” branch is the default branch created when a new repository is initialized
    • “Main” serves as the baseline for all other branches
    • Is the most up-to-date version of your project
  • Other developers can then work on different aspects of a project on their own individual branch
    • When a new branch is created, commits and changes can be made to only the single branch
    • Each developer work without affecting the main branch

Visualization of branching ⬇️⬇️⬇️

README: http://tinyurl.com/into-to-git-README

26 of 51

Branch Visual

A branch is useful to fix an issue in code and to make new features for a project.

These changes will not be seen on the main branch until they are merged together, where the red circles are (will go over soon)

README: http://tinyurl.com/into-to-git-README

27 of 51

Commit

  • A commit is a snapshot of your repository
  • Commit when a set of changes has been made that you want to save to your project
  • Each commit creates a record of your project
    • What allows you to revert back to earlier versions of the code
  • When making a commit:
    • First add the current files in the directory to prepare for the next commit
    • Then leave a short descriptive message is left to describe what changes have been made
  • Git commit saves repository changes on the local but not the remote repository

README: http://tinyurl.com/into-to-git-README

28 of 51

Push

  • When working on a project, you are making changes to a local repository
  • After finishing changes and committing, you need to upload the content in the local repository to the remote repository
  • After pushing, others can see the updated version on your code

To do this we “push” the commits from a local branch to the remote repository 🥸

README: http://tinyurl.com/into-to-git-README

29 of 51

Push Example

Once you use the command to push your changes, the following screen will populate. Follow the link to make your pull request on GitHub!

README: http://tinyurl.com/into-to-git-README

30 of 51

Pull Request (PR)

  • After making changes to your branch, committing those changes, and pushing, code is still separate from the main branch
  • To merge changes into the main branch, you need to create a pull request
  • A pull request is a a way to propose changes when working with a team
    • PRs allows for code to be reviewed before integrated into the overall project
    • Think of a PR as an extra check to ensure your code will not break the overall project and will integrate smoothly

👉👈

README: http://tinyurl.com/into-to-git-README

31 of 51

Pull Request Example

  • When you push on your remote repo, it will provide you with a link to the pull request on GitHub
  • Following the Link will refer you to this page
  • Once you write the description, click the green “Create pull request button”

You will write a ⬆️

more descriptive explanation of what changes were made

README: http://tinyurl.com/into-to-git-README

32 of 51

Merge

  • Merging is the process of combining two or more branches into a single branch
    • Typically incorporating changes made on one branch into another branch
  • This processes is necessary when there are multiple developers working on the same project
  • Can encounter merge conflicts
    • Occur when you attempt to merge two branches that have conflicting changes
    • Git does not know what changes should take precedence

README: http://tinyurl.com/into-to-git-README

33 of 51

Merge Visual

README: http://tinyurl.com/into-to-git-README

34 of 51

Merge Example

  • If there are no merge conflicts the following screen will populate that allows you to merge your commit

Press the green ➡️

“Merge Pull Request” button to merge !

README: http://tinyurl.com/into-to-git-README

35 of 51

Merge Conflict Example

If there is a merge conflict, this box will appear instead of the previous slide. Click the “Resolve conflicts” box.

GitHub will highlight where the conflict is.

You can then fix the conflict on GitHub!

*Note GitHub may not allow you to resolve the conflict on GitHub if the issue is too complex

README: http://tinyurl.com/into-to-git-README

36 of 51

Pull

  • A command that will take the content in the remote repository and update the local repository to match the latest changes
  • Ensures that the local branch is up to date with the remote repo
  • Good practice to pull regularly to keep your local repo up-to-date with the remote repo
    • Prior to working on a new feature, pull the latest changes
    • Helps avoid conflicts
    • Ensures you are working with the most recent database

*Git pull –force forces the local branch to update the remote repos changes, even if it conflicts or overwrites local changes

README: http://tinyurl.com/into-to-git-README

37 of 51

Git Walkthrough

README: http://tinyurl.com/into-to-git-README

38 of 51

Check that Git is installed

Type the following command to verify that Git is installed:

$ git --version

README: http://tinyurl.com/into-to-git-README

39 of 51

Create a Repository

  • (1) When starting any project (individually or with a group) create a GitHub repository
    • Now there is a private/public repository under your GitHub profile
  • (2) Now initialize a repository on your personal device by typing the following command in the terminal of the correct file path
    • $ git init (initializes a new Git repository in the current directory)
  • (3) Create an initial commit to this repository
    • $ git add . (adds all the files on your computer to the repository)
    • $ git commit -m “(message)” (commits your changes to the repository, within the quotations add a meaningful description of what changes were made)

Initializing the repo in your folder is now in the working directory!

README: http://tinyurl.com/into-to-git-README

40 of 51

Create the “main” branch

  • Now, the local device needs to sync to the GitHub repository
    • (1) $ git remote add origin [GitHub repository link] (syncs repo to local machine YAY) ➡️ allows for pushing and pulling
    • (2) $ git branch -M main (renames the current branch to “main”
    • (3) $ git push -u origin main (pushes local “main” branch to remote repository) ) ➡️ allows you to use git push in the future without specifying the branch name

⬅️ these commands creates this main branch!!!

Make sure to type all 3 commands!!!

README: http://tinyurl.com/into-to-git-README

41 of 51

Demo 👯‍♀️

Time to create our first repository and initialize the main branch!

README: http://tinyurl.com/into-to-git-README

42 of 51

Overview of Branches

Feature1

Main

Feature2

  • Originally you will be on the main branch.
  • To make a new feature, Feature1, a new branch Feature1 must be created.
  • To work on a separate feature, another branch Feature2 will be made.
  • Both branches will not affect the main branch until they are merged together

README: http://tinyurl.com/into-to-git-README

43 of 51

Make Additional Branches

  • After setting up the main branch, you can create additional branches, pushing to main once changes have been made
  • Create a new branch

$ git checkout -b “branch name” (creates a new branch and switches to it)

(2) Make changes to your project on this branch

(3) Stage/commit/push changes

$ git add .

$ git commit -m “message”

$ git push -u origin new-branch-name

** repeat these steps as many times as needed to work on additional branches **

When you “git add” you are adding changes made from the working directory since the last commit to the staging area

→ everything you want to add to the staging area

README: http://tinyurl.com/into-to-git-README

44 of 51

Review Branch Commands

$ git checkout - b “branch name”

➡️ creates a new branch [branch name] and switches you to that branch

$ git checkout “branch name”

➡️ switches you to the branch [branch name]

** [branch name] is a placeholder for the actual branch name **

Example: ‘git check -b gitWorkshop’ creates a branch named ‘gitWorkshop’ and switches to ‘gitWorkshop’

README: http://tinyurl.com/into-to-git-README

45 of 51

Push Branches 🧍🏻‍♀️ ➡️ ➡️ ➡️ ➡️ 🪨

  • Once a commit has been made on the local repository branch, it needs to be pushed to main
    • $ git push -u origin [branch name]
      • ‘git push’ pushes commits from the local to remote repository
      • [branch name] is the name of the current remote repository
  • If you forget the command above, use:
    • $ git push
      • From there, copy and paste the last line (starting with ‘git push’ into the command line

This will populate ⬇️

README: http://tinyurl.com/into-to-git-README

46 of 51

Merge Branches

  • After pushing the changes, you can then make a pull request on GitHub
  • To do this, go to the repository on GitHub and select the branch you want to merge
    • GitHub will then determine whether or not there are any merge conflicts
  • (if there are merge conflicts) GitHub will highlight the conflict and allow you resolve the conflicts on GitHub web interface
  • If there are no conflicts, you can merge commit the two branches
    • Need to $ git pull to see the changes of the merge on your local branch

💭

🧍🏻‍♀️

When there are merge conflicts

README: http://tinyurl.com/into-to-git-README

47 of 51

Git Workflow: Review 🙋🏻‍♀️🙋🏻‍♀️🙋🏻‍♀️

  1. Pull from the main branch
  2. Create a branch
  3. Do some work
  4. Add
  5. Commit
  6. Push
  7. Pull Request
  8. Fix merge conflicts
  9. Merge

README: http://tinyurl.com/into-to-git-README

48 of 51

Demo 😎

Now let’s make a new branch, make some changes, and then merge it back into main (uh oh merge conflict 😯)

README: http://tinyurl.com/into-to-git-README

49 of 51

Review of command steps

  1. $ git checkout main
  2. $ git pull (integrates changes from remote to local repository… aka gets you the most updated version of the main branch)
  3. $ git checkout -b “[branch name]”
  4. $ git add .
  5. $ git commit -m "[message]"
  6. $ git push -u origin [branch name]
  7. Pull request
  8. Resolve merge conflicts
  9. Merge pull request

README: [Link]

50 of 51

Review of fundamental git commands

$ git status ➡️ show changes made since the last commit

$ git init ➡️ create a repository in current file path

$ git checkout -b “[name]” ➡️ create and switch to a new branch

$ git checkout “[name]” ➡️ switch to an existing branch

$ git add . ➡️ add current changes to the next commit

$ git commit -m “[message]” ➡️ commit changes

$ git push -u origin [branch name] ➡️ push changes to remote repo

$ git pull ➡️ integrate changes from remote repo to local branch

README: http://tinyurl.com/into-to-git-README

51 of 51

Thank you for watching!! ❤️❤️❤️

If you have any question before HOTH XI please ask the discord channel!

README: http://tinyurl.com/into-to-git-README