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.
Outline Git terminology Git configuration Git structure Local repository 2
Git Terminology Term Change Clean Commit Log Explanation Whenever a file in the working directory is altered such that it is different 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. 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. 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. The complete history of the commits made to a repository. 3
Git terminology Term Repository Staged Working directory Explanation The database of the entire history of a project. In git, this is stored inside 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. 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. This is the directory that contains your project. It will have a hidden repository within it. 4
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 johndoe@example.com This information is stored in ~/.gitconfig or ~/.config/git/config depending on the distribution 5
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. 6
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. 7
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> 8
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. 9
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. 10
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. 11
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> 12
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. 13