1 of 47

Fixing

A presentation by Tlaloc Barajas and Jonathan Chancey

2 of 47

Image Source: https://xkcd.com/1597/

3 of 47

Topic List

  • Installing/Configuring Git
  • Basic Commands
  • Manipulating the Example Repo
  • Exploring Unapparent Yet �Fundamental Concepts
  • Takeaways

Image Source: https://xkcd.com/1597/

4 of 47

But First!

5 of 47

What is Git?

  • Git is a widely used and modern VCS (version control system)
  • Git stores source code used in software development in a data structure called a repository

6 of 47

What is Git?

  • The purpose of Git is to manage a project, or a set of files, as they change over time
  • Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel

7 of 47

Why is Git Useful?

Git is good

  • Git has the functionality, performance, security, and flexibility that most teams and individual developers need
  • The raw performance characteristics of Git are very strong when compared to many alternatives

8 of 47

Why is Git Useful?

  • Committing new changes, branching, merging and comparing past versions are all optimized for performance
  • The algorithms implemented inside Git take advantage of deep knowledge about common attributes of real source code file trees, how they are usually modified over time and what the access patterns are
  • In addition to being distributed, Git has been designed with performance, security and flexibility in mind

9 of 47

Why Should You Learn Git?

  • A staggering number of software projects rely on Git for version control, including commercial projects as well as open source
  • Developers who have worked with Git are well represented in the pool of available software development talent and it works well on a wide range of operating systems and IDEs (Integrated Development Environments)

10 of 47

Prerequisites

11 of 47

GitHub Account

https://github.com/

Know your GitHub credentials.

If you’re new to GitHub, create an account with your school email.�

12 of 47

Git on Windows

There’s 2 main options for getting git on windows

13 of 47

Git on Windows - WSL

14 of 47

Git on Windows - MinGW Instructions

15 of 47

Git on Mac - Instructions

If you already have XCode Command Line Tools you may have git.

Try running:� $ git --version

you want an output that looks like this:�

Otherwise just download Git�https://git-scm.com/download/mac

16 of 47

Git on Linux - Instructions

Check your version with:� $ git --version

Otherwise, install Git using your package manager�For example on Ubuntu:� $ apt update� $ apt install git

17 of 47

Environment Setup

Configure your git username and email— this step is required to make commits$ git config --global user.name "Your firstname and lastname"� $ git config --global user.email "yourschoolemail@ucmerced.edu"

18 of 47

Commands We’re Using Today

19 of 47

The Basic Commands

  1. $ git status
  2. literally the best command
  3. it’s basically ls
  4. spam it after you do anything

20 of 47

The Basic Commands

  • $ git add FILENAME
    • stages files to be committed
    • needed to commit
  • $ git add .
    • adds everything
  • $ git rm --cached FILENAME
    • unstages FILENAME

21 of 47

The Basic Commands

  • $ git commit -m "YOUR_MESSAGE"
    • takes a snapshot of the repo
    • everything in git is a commit, we’ll come back to that

22 of 47

The Basic Commands

  • $ git push
    • uploads your local changes to the remote repo
  • $ git pull
    • downloads remote changes to your local repo

23 of 47

Commands We’re Looking at Today

  • $ git checkout COMMIT_ID
  • updates files in your working directory to match the version stored in that COMMIT_ID or BRANCHNAME
  • moves HEAD pointer to COMMIT_ID
  • $ git branch BRANCHNAME COMMIT_ID
  • Create BRANCHNAME or change existing BRANCHNAME to point at COMMIT_ID
  • $ git branch -d BRANCHNAME
    • Delete branch pointer BRANCHNAME
    • Use -D to force a delete

24 of 47

Clone a Repo

25 of 47

Fork and Clone the Example Repo

  1. Navigate to the repo https://github.com/UCMercedACM/fixing_git
  2. Click fork
  3. Select your account
    1. Forking complete!
  4. Copy the url by clicking�

26 of 47

Create a Local Copy of the Git Repository

Create/navigate to where you want to store your repo� $ mkdir ~/src� $ cd ~/src�Configure your git username and email—this step is required to make commits$ git clone THE_URL_YOU_COPIED� $ cd fixing_git

27 of 47

Create a file you want to be added to the original repo

Create a branch� $ git branch BRANCHNAME� $ git checkout BRANCHNAME�Create a file in a text editor or just type� $ echo wow this is a nice message >> myFile.txt�Add the file� $ git add myFile.txt

Commit the file� $ git commit -m "Add myFile.txt"

Push the commit� $ git push

28 of 47

Create a Pull Request

Head to github repo that you forked

Click the “New Pull Request” button

SELECT the_wild_west on the base repo

When approved it will be a part of the example repo

29 of 47

Let’s continue

Checkout master� $ git checkout master

30 of 47

Concepts

31 of 47

DAG: Directed Acyclic Graphs

  • formed by nodes and edges
  • edges have direction (arrows not lines)
  • nodes have zero or more parents or children
  • inbreeding is allowed
  • no one is their own ancestor

in other words “non-time-travelling Royal family rules”

32 of 47

All Commits Are Nodes in a Single DAG

  • literally every commit
  • this includes
    • commits that are in other branches
    • commits that have been uncommitted
    • commits that were amended

33 of 47

Branches Are Not Places

  • Branches are pointers
  • You’ve heard this said before
    • “The Commit is on the branch”
    • “Merge it into the branch”
    • “Cherry pick it from the branch”
  • A branch is a named label �pointing to single commit

34 of 47

Everything in Git is a Commit

  • Commits include
    • the hashes of its parent commits
    • the author
    • the commit message
    • other various metadata

35 of 47

Situations

Following along is encouraged

36 of 47

Situation 1 - Branch Named Incorrectly

Let’s list all of our branches

$ git branch

It looks like the person who made the repo �named the “dev” branch “deb”�1. Create a new branch called dev that �points to where deb does currently

$ git branch dev deb

$ git checkout dev

2. Delete the old branch

$ git branch -D deb

37 of 47

Situation 2 - Committed on the Wrong Branch

38 of 47

Situation 2 - Committed on the Wrong Branch

Let’s mess this up ourselves

$ git checkout master� $ echo some words > some_file� $ git add some_file� $ git commit -m "add some_file"� $ echo some more words >> some_file� $ git add some_file� $ git commit -m "add more to some_file"� $ git log --oneline --decorate --graph --all�

39 of 47

Situation 2 - Committed on the Wrong Branch

40 of 47

Situation 2 - Committed on the Wrong Branch

master

HEAD

???

HAVE

WANT

7db37e4

"add some_file"

"add more to some_file"

7db37e4

"add some_file"

"add more to some_file"

41 of 47

Situation 2 - Committed on the Wrong Branch

7db37e4

"add some_file"

"add more to some_file"

master

HEAD

7db37e4

"add some_file"

"add more to some_file"

master

HEAD

someNewBranch

HAVE

WANT

42 of 47

Situation 2 - Committed on the Wrong Branch

Create a new branch pointer at our latest commit

$ git branch someNewBranch master

Point HEAD to our new branch

$ git checkout someNewBranch

Forcefully point the wrong branch back to where we want it

$ git branch --force master 7db37e4

43 of 47

Situation 2 - Committed on the Wrong Branch

44 of 47

Key Takeaways

Branches are pointers, not places

It’s better to have an amazing understand of the core commands than to know many commands

Every commit that ever existed still exists

Git hates you but it will always keep committed work

There’s no shame in recloning, just save your work

45 of 47

Sources

46 of 47

Thank You

47 of 47

Now Git Out