28 मध्ये 1 औँ

CSE 391

Introduction to git

Slides created by Josh Ervin and Hunter Schafer. �Based off slides made by Marty Stepp, Jessica Miller, Ruth Anderson, Brett Wortzman, and Zorah Fung

28 मध्ये 2 औँ

AGENDA

  • PollEverywhere: what are our questions?
  • Administrivia
  • Questions from Ed:
    • Going over Problem 8 in HW3
    • Will you have solutions keys? (No, but come to OH)
    • How is git actually used in industry?
  • Fun Problem 5 answers!
  • Open Q&A
  • Discussion Questions

28 मध्ये 3 औँ

ADMINISTRIVIA

  • Simon’s office hours are now:
    • Wed 3:30 - 4:30 PM in CSE 150 and Zoom
    • Sun 1:00 - 2:00 PM on Zoom only

28 मध्ये 4 औँ

AGENDA

  • PollEverywhere: what are our questions?
  • Administrivia
  • Questions from Ed:
    • Going over Problem 8 in HW3
    • Will you have solutions keys? (No, but come to OH)
    • How is git actually used in industry?
  • Fun Problem 5 answers!
  • Open Q&A
  • Discussion Questions

28 मध्ये 5 औँ

OPEN–SOURCE – AN INTERLUDE

  • What is open-source?�
  • What’s the point of open-source?�
  • How common is open-source?�
  • A little bit about Matt’s experience with open-source…

Interested in more? Definitely come to Matt’s office hours :)

28 मध्ये 6 औँ

FUN HW3 P5 ANSWERS

# How many people that prefer cats are in CSE 351 in autumn 2023?

cat intro_survey_23au.csv | grep "Cats" | cut -d, -f4 | grep "Yes" | wc -l

�# How many students chose chocolate, but did not choose cats.

grep -i "chocolate" combined_results.csv | grep -iv "cats" | wc -l

�# Is there someone else besides me whose response in the combined_results.csv says

# Twix, steak, and Cats case-insensitively? If the following command produces

# a value that is greater than 1, then the answer is yes.

grep -i "Twix" combined_results.csv | grep -i "steak" | grep "Cats" | wc -l

28 मध्ये 7 औँ

FUN HW3 P5 ANSWERS

# What is the most common favorite dinner among students who prefer dogs as pets?

grep -i 'Dogs' intro_survey_23sp.csv | cut -d ',' -f 2 | grep -v 'Favorite Dinner' | sort | uniq -c | sort -nr | head -n 1 | awk '{print $2, $1}'

# How many preferred candies were only listed once in the surveys?

cut combined_results.csv -d, -f1 | sort -f | uniq -ic | grep " 1 " | wc -l

28 मध्ये 8 औँ

DISCUSSION QUESTION #1

What are the four different phases of git that we’ve learned? ��What commands do you know to move changes from one phase to the next? ��Is there ever more than one way to move a change from one phase to another?

28 मध्ये 9 औँ

DISCUSSION QUESTION #7

One popular “version control” that you’ve probably used is Google Drive/Docs. How is this similar to git? How is it different?

28 मध्ये 10 औँ

DISCUSSION QUESTION #8

Using the man page, how would you undo (or revert) a commit. Is this what you expected? Why?

28 मध्ये 11 औँ

28 मध्ये 12 औँ

AGENDA (FROM VIDEO)

  • Logistics, Roadmap
  • Introduction to git
    • Common git commands
    • Using git locally
    • Using git with remote
    • Resolving merge conflicts

28 मध्ये 13 औँ

ROADMAP

  • Introduction to the command line
  • Input/output redirection, pipes
  • More input/output redirection, tee, xargs
  • Git: Fundamentals
  • Git: Branches and rebasing
  • Regular expressions
  • More regular expressions, sed
  • Users and permissions
  • Bash scripting
  • Industry applications

28 मध्ये 14 औँ

VERSION CONTROL - INDIVIDUAL

  • Does any of the following sound familiar?
    • Your code was working great! Then you made a few changes and now everything is broken and you saved over the previous version?
    • You accidently delete a critical file and can’t get it back.
    • Your computer was broken or stole and now all of your files are gone!
    • While writing a paper for one of your classes you save each version as final_paper.doc, final_paper2.doc, final_paper_actually_this_time.doc, UGH.doc
  • There has to be a better way to manage versions...

28 मध्ये 15 औँ

VERSION CONTROL - TEAMS

  • Does any of the following sound familiar?
    • My partner and I are paired up for a project for one of our CSE classes. We usually pair program together in the labs but sometimes we have to work remotely. Who keeps the most up-to-date version of the project? How do we share changes with each other? What if I want to compare the changes my partner made?
    • How do we keep backups of important files? Who stores them on their computer?

28 मध्ये 16 औँ

VERSION CONTROL

  • Version Control: Software that keeps track of changes to a set of files.
  • You likely use version control all the time:
    • In Microsoft Word, you might use Ctrl+Z to undo changes and go back to an earlier version of the document.
    • In Google Docs you can see who made what changes to a file.
  • Lots of people have a use-case for version control
    • We often think of version control as related to managing code bases, but it’s also used by other industries such as law firms when keeping track of document changes over time.

28 मध्ये 17 औँ

REPOSITORY

  • A repository, commonly referred to as a repo is a location that stores a copy of all files.
    • The working directory(or working tree) is different from the repository (see next slide)
  • What should be inside of a repository?
    • Source code files (i.e. .c files, .java files, etc)
    • Build files (Makefiles, build.xml)
    • Images, general resources files
  • What should not be inside of a repository (generally)
    • Object files (i.e. .class files, .o files)
    • Executables

28 मध्ये 18 औँ

REPOSITORY

  • With git, everyone working on the project has a complete version of the repository.
    • There is a remote repository, which is the defacto central repository
    • Remote repositories are hosted on services like GitHub or Gitlab
    • Everyone has a local copy of the repository, which is what we use to commit.

Developer 1

Developer 2

Developer 3

Push/Pull Changes

28 मध्ये 19 औँ

GIT: FOUR PHASES

Working Directory

Staging Area/Index

Local Repository

Remote Repository

Working changes

Changes you’re preparing to commit

Local copy of the repository with your committed changes

Remote shared repository (Usually stored with a platform like GitHub/Gitlab)

git stage

git commit

git push

NOTE: There are way more git commands than what is listed here - this is a simplified model to get us started.

28 मध्ये 20 औँ

INSPECTING A REPOSITORY

git status

git log

Working directory

Staging Area

Commit History

28 मध्ये 21 औँ

Suppose a run of git status show the following:

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git restore <file>..." to discard changes in working directory)

modified: file1.txt

modified: file2.txt

Based on this information, at what location are the changes to file1.txt and file2.txt within the git phases on the previous slides.

  • Working directory
  • Index (Staged)
  • Local Repository
  • Remote Repository

28 मध्ये 22 औँ

Suppose a run of git status show the following:

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git restore <file>..." to discard changes in working directory)

modified: file1.txt

modified: file2.txt

We want to run a sequence of commands that includes the changes to file1.txt in a single commit followed by the changes to file2.txt in another commit in the local repository. What commands should we run?

28 मध्ये 23 औँ

GIT COMMANDS

git clone url [dir]

Copy a git repository

git add files

Adds file contents to staging area

git stage files

Same as git add files

git commit

Takes a snapshot of staging area and creates a commit

git status

View status of files in working directory and staging area

git diff

Show difference between staging area and working directory

git log

Show commit history

git pull

Fetch from remote repository and try to merge

git push

Push local repository to remove repository

28 मध्ये 24 औँ

ADDING AND COMMITTING FILES

  • The first time we asked a file to be tracked, and everytime before we commit a file we must add it to the staging area. This can be done with the following command
    • $ git add file1.txt file2.txt
  • This takes a snapshot of the files and adds it to the staging area. You can still modify files in the working directory, but you will need to add again to have these changes saved in the staging area.
  • Note: To unstage a change, you can use the following
    • $ git reset HEAD filename
  • To move staged changes into the local repository we commit them from the staging area
    • $ git commit -m “Updated filename”
  • Note: All of these commands are acting on the local copy of your repository. You will need to push them to remote to see these changes elsewhere.

28 मध्ये 25 औँ

COMMIT MESSAGES

  • Running $ git commit will bring up a text editor by default for you to type your commit message.
    • Write the subject text (less than 50 characters) on the first line, followed by paragraphs describing your commit in the rest of the page.
  • Running $ git commit -m “Your Message” will allow you to type the message directly in the command line without opening a text editor.
    • This is useful for simple commits, but avoid using it for more complicated commits.
  • Regardless, the subject line should always be written with the following form
    • If applied, this commit will your subject line here

28 मध्ये 26 औँ

GIT: FOUR PHASES WITH REMOTE

Working Directory

Staging Area/Index

Local Repository

Remote Repository

git stage ...

git commit

git push

git pull

28 मध्ये 27 औँ

CSE GITLAB

  • For this class we will be using gitlab. To access:
  • Log onto CSE Gitlab (https://gitlab.cs.washington.edu)
    • Use your CSE NetID if you have one
  • Add an ssh key (https://gitlab.cs.washington.edu/help/user/ssh.md )
    • Follow the instructions in README.md which say to type
      • $ ssh-keygen -t rsa -C “yourUWNetID@uw.edu” -b 4096
      • Hit return to accept the default file location. You do not need a password, so you may hit enter twice when prompted for a password
    • Then type $ cat ~/.ssh/id_rsa.pub
    • Copy and paste the key into the SSH-Keys section under Profile Settings in your user profile on Gitlab.

For a brief introduction to SSH keys, see Ed (but ignore part about attu).

All credit to previous 391 Instructors for this slide, including Marty Stepp, Brett Wortzman, Jessica Miller, and Ruth Anderson

28 मध्ये 28 औँ

Get Ready to Use Git

  • Set the name and email for Git to use when you commit:�$ git config --global user.name “Bugs Bunny”�$ git config --global user.email bugs@gmail.com
    • You can call git config --list to verify these are set.
    • These will be set globally for all Git projects you work with.
    • You can set variables on a project-only basis by not using the --global flag.
  • The latest version of Git will also prompt you that push.default is not set, you can make this warning go away with:�$ git config --global push.default simple
  • You can also set the editor used for writing commit messages (default = vim):�$ git config --global core.editor emacs

All credit to previous 391 Instructors for this slide, including Marty Stepp, Brett Wortzman, Jessica Miller, and Ruth Anderson