Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit...

13
CS 390 Software Engineering Lecture 4 - Git Reference: Scott Chacon and Ben Straub, Pro Git , published by Apress, available at https://git-scm.com/book/en/v2 .

Transcript of Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit...

Page 1: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

CS 390 Software Engineering

Lecture 4 - Git

Reference: Scott Chacon and Ben Straub, Pro Git, published by Apress, available at https://git-scm.com/book/en/v2 .

Page 2: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Outline

Git terminology

Git configuration

Git structure

Local repository

CS 390 Software Engineering - Lecture 4August 29, 2018 2

Page 3: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Git Terminology

Term Explanation

Change Whenever a file in the working directory is altered such that it isdifferent from the one that is stored in the repository, this is called a change. Adding or removing a file is also a change. Changes are not saved until they have been committed to the repository.

Clean A working directory is clean if there are no changes. That is, there are no files that are different from the ones in the most recent commit in the repository.

Commit A commit is a set of staged changes that are added atomically to the repository. Committing in git is a 2-phase process. First changes are staged by adding them to the index, an intermediate place, and then the index is committed to the repository, which makes a permanent record of the changes in the log.

Log The complete history of the commits made to a repository.

CS 390 Software Engineering - Lecture 4August 29, 2018 3

Page 4: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Git terminology

Term Explanation

Repository The database of the entire history of a project. In git, this is storedinside the working directory in a hidden subdirectory. For a project, a repository is either initialized by the user, in which case it has a completely empty history, or is cloned from an preexisting repository on a server somewhere.

Staged Changes are staged by adding them to the index, an intermediate place. At the time a change is staged, only the changes at the time of the add are staged.

Working directory This is the directory that contains your project. It will have a hidden repository within it.

CS 390 Software Engineering - Lecture 4August 29, 2018 4

Page 5: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Basic git configuration

When a commit is done, git marks the log entry with

information about the user doing the commit

MInimally, the user's name and email address should be

set explicitly.

$ git config --global user.name "John Doe"

$ git config --global user.email [email protected]

This information is stored in ~/.gitconfig or

~/.config/git/config depending on the

distribution

CS 390 Software Engineering - Lecture 4August 29, 2018 5

Page 6: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Git structure

Git stores information in a database. By default it is an

SQLlite database.

Files are compressed and stored as blobs.

Commits are indexes of files and metadata.

Project is a tree of commit indexes. Branches are

pointers to particular commit indexes. The current

branch is indicated by the HEAD.

CS 390 Software Engineering - Lecture 4August 29, 2018 6

Page 7: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Local repository

Any directory can be used as a git repository

Initialize a new git repository

$ git init

Repository information is stored in hidden .git

subdirectory. Generally, will not need to access this

directly.

CS 390 Software Engineering - Lecture 4August 29, 2018 7

Page 8: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Staging changes

A change is anything that is in a working directory that is

not the same as the repository, i.e., the most recent

commit.

▪ Modified files

▪ New files

▪ Deleted files

Changes to be committed must be staged. That is,

information about the change is added to an index.

$ git add <filename>

CS 390 Software Engineering - Lecture 4August 29, 2018 8

Page 9: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Staging changes

When a change is staged, the information reflects the

state of the file at the time of the add. If more

modifications are made to a file, it must be added again

before committing.

When there are many changes, it may be easier to add

everything in the working directory that has changed.

$ git add .

Since not all files need to be tracked (e.g., *.o files), a list

of files that should be ignored may be given in a .gitignore file.

CS 390 Software Engineering - Lecture 4August 29, 2018 9

Page 10: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Committing a project

To see what files have been or need to be staged, look

at the status of the project.

$ git status

Once all of the relevant changes have been added (or added to the .gitignore file), commit the project

$ git commit -m "message"

After a commit, the working directory is said to be clean.

Files that have been committed are also said to be

tracked. Once a file becomes tracked, git expects

changes to it be staged before commits unless it is

removed.

CS 390 Software Engineering - Lecture 4August 29, 2018 10

Page 11: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Commit log

To view the record of commits

$ git log

The default listing is in reverse chronological order and

each entry has an SHA1 checksum, name and email of

the user that did the commit, and the date/time of the

commit.

There are various options that present other information

or other formats.

CS 390 Software Engineering - Lecture 4August 29, 2018 11

Page 12: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Undoing things

Redoing a commit. E.g., forgetting to add a file.

$ git commit –amend

Unstaging a file.

$ git reset HEAD <filename>

Unmodifying a modified file. This is checking out the last

committed version of the file. All of the changes will be

lost, so be careful using it.

$ git checkout -- <filename>

CS 390 Software Engineering - Lecture 4August 29, 2018 12

Page 13: Lecture 4 - Gituenics.evansville.edu/~hwang/f18-courses/cs390/Lecture04-Git.pdf · Commit A commit is a set of staged changes that are added atomically to the repository. Committing

Branching

Branch called master is created by default.

Create a new branch. The branch starts out the same as

the last committed version of current branch.

$ git branch <branch name>

Must switch to a branch by checking it out

$ git checkout <branch name>

Switching back to another branch restores the most

recent committed version of that branch.

CS 390 Software Engineering - Lecture 4August 29, 2018 13