csc 510-001, (1877)
fall 2024, software engineering
Tim Menzies, timm@ieee.org, com sci, nc state


home :: syllabus :: corpus :: groups :: moodle :: license

Git

What is Git?

Git is a free and open source distributed version control system used for handel everything of projects during software development. It is often used to control source code by programmers collaboratively developing software.

Git data structure

Git has two data structures: a mutable index (also called stage or cache) that caches information about the working directory and the next revision to be committed; and an object database that stores immutable objects. The index serves as a connection point between the object database and the working tree.
In Git terminology, a file is called a blob, and it’s just a bunch of bytes. A directory is called a tree, and it maps names to blobs or trees (so directories can contain other directories).

Main Concepts

Repository

A repository (or repo) is a storage space for files, directories, historical records, commits, and heads. It can be local (on your computer) or remote (on a server like GitHub).

Some components of the repository:

Commit

A commit is a snapshot of a set of changes. For example, if you added 5 files, and removed 2 others, these changes will be contained in a commit. Each commit has a unique ID.

Branch

A branch is a parallel version of your project. By default, Git creates a main (or master) branch. Branches allow you to develop features independently from the main codebase.

Stages of Git

Merge

Merging is the process of combining changes from different branches. It integrates the work from one branch into another.

Clone

Cloning is copying a repository from a remote server to your local machine.

Pull and Push

Basic Commands

#Create an empty Git repository.
git init

# Set & Print Some Basic Config Variables (Global)
$ git config --global user.email "MyEmail@ncsu.com"
$ git config --global user.name "My Name"

# Quickly check available commands
$ git help

#To intentionally untrack file(s) & folder(s) from git. Based on different languages, 
echo "temp/" >> .gitignore
echo "private_key" >> .gitignore

There are different styles of gitignore file. reference: https://github.com/github/gitignore. For example, Python developers might consider using https://github.com/github/gitignore/blob/main/Python.gitignore.

# Clone a repository
git clone <repository-url>

# Check the status of your files. Will display the branch, untracked files, changes and other differences
git status

# list existing branches & remotes
git branch -a
# create a new branch
git branch <myNewBranch>
# delete a branch
git branch -d <myBranch>
# rename a branch
git branch -m <myBranchName> <myNewBranchName>

# Add files to staging area
git add <file>

# Commit your changes
git commit -m "Commit message"

# By default, git push will push and merge changes from the current branch to its remote-tracking branch
git push

# By default, git pull will update your current branch
git pull

# Create a new branch
git checkout -b <branch-name>
# Switching Branches
git checkout <branch-name>
# Restoring Files to a Previous State
git checkout <branch-name> -- <file-name>
# Checking Out a Specific Commit
git checkout <commit-hash>


# Merge a branch into the current branch
git merge <branch-name>

Special situation

Stash

You’ve been doing some work in your git repo, but you want to pull from the remote. Since you have uncommitted changes to some files, you are not able to run git pull. Instead, you can run git stash to save your changes onto a stack!

git stash

git pull

git status #checking if everything is okay

git stash list

git stash pop
#or
git stash apply

Fix merge conflix

When we merge current branch to main branch, some conflicts might happen, like:

Auto-merging filename.txt
CONFLICT (content): Merge conflict in filename.txt
Automatic merge failed; fix conflicts and then commit the result.

This means that Git has identified conflicting changes in “filename” that need to be resolved.
Open the files with conflicts in your text editor or IDE. Conflicts sections shows your current branch, named “HEAD”, and the branch you try to merge, named “branch-name”.

<<<<<<< HEAD
This is a different line on main branch.
=======
This is the modified line on current branch.
>>>>>>> feature

Step:

Reverse

In Git, “reverse” typically refers to undoing changes or reverting a commit. There are several ways to reverse changes in Git:

Some materials

YouTube video: Learn git in 15 mins
Interacting git learning website: Learn Git Branch

In class exercises

Try git comments in Learn Git Branch website.

Homework

Note that for the homework, we will be looking for evidence that you completed these tasks in your commit log.

  1. Initialize a new Git repository:
    • Create a new directory called git-homework.
    • Initialize a Git repository in this directory.
  2. Create a new project and add it to the repository:
    • Create a file called README.md and write a brief description about the repository.
    • Add LICENSE.md, .gitignore, INSTALL.md, CONTRIBUTING.md, CODE-OF-CONDUCT.md into your repo. (reference: https://txt.github.io/se24fall/process.html#standard-files; https://choosealicense.com/licenses/)
    • Create a simple webpage(.html file) discribing your team(including team number, all team members’ names, and emails).
    • Stage and commit with an appropriate commit message.
  3. Modify the file(.html file) and commit the changes:
    • Add links to emails.
    • Commit and push changes with descriptive message.
    • View the commit history and check differences between commits.
  4. Create a new branch:
    • Create a new branch called “feature”.
  5. Switch to the new branch and make change:
    • switch to the feature branch.
    • Add a footer to the website.
    • Commit and push the changes with an appropriate message.
  6. Simulate a merge conflict:
    • Switch back to the main branch.
    • Make a change to the same part (where the footer was added in feature) but with different content. For example, add a different footer message.
    • Commit and push the changes.
  7. Merge and resolve the conflict:
    • Merge the feature branch into the main branch.
    • Resolve the conflict to include both changes or create a consolidated footer message.
    • After resolving the conflict, stage the changes and complete the merge.
  8. Revert a commit:
    • Make a new commit by adding a line to the project(.html file).
    • Then undo the most recent commit.
  9. Stash Changes:
    • Create a new branch named “temp”, and make some changes but do not commit them.
    • Use git stash to save the changes.
    • Switch to the main branch and make another commit.
    • Switch back to your “temp” branch and apply the stashed changes using git stash apply.
  10. Clean Up the Repository:
    • Delete all the branches.
  11. Put the screen snap of the webpage on the README.md file.
    Submitting: create an pdf include your repo link and one screen snap with your terminal comments and return information of step 9. Scale: Each step is worth 0.3 points. Totally 3 points.