csc 510-001, (1877)
fall 2024, software engineering
Tim Menzies, timm@ieee.org, com sci, nc state
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 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).
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:
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.
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.
Merging is the process of combining changes from different branches. It integrates the work from one branch into another.
Cloning is copying a repository from a remote server to your local machine.
#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>
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
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:
In Git, “reverse” typically refers to undoing changes or reverting a commit. There are several ways to reverse changes in Git:
Revert a Commit: Creates a new commit that
undoes the changes made by a previous commit without rewriting history.
This is useful when you want to keep the history intact and record the
fact that a commit was reverted.
sh git revert <commit-hash>
But if you have merged a branch into your main branch, but later realized that you need to undo this merge.
#find the commit hash of the merge that you want to reverse
git log --oneline
#use "-m" option to specify the parent of the merge you want to keep. The "-m 1" option typically specifies that you want to keep the changes from the first parent (usually the main branch before the merge).
git revert -m 1 <merge-commit-hash>
Reset a Commit: Moves the HEAD
pointer and potentially the branch pointer to a previous commit,
effectively “erasing” commits from the history. This can be destructive
as it changes the commit history, making it as though certain commits
never happened.
The git reset command is used to undo commits by
moving the HEAD and the current branch to a specified commit. There are
three modes of reset:
git reset [--soft | --mixed | --hard] <commit-hash>
Important: Using –hard will lose all changes after the specified commit, and they cannot be recovered easily.
Undo Changes in the Working Directory: Reverts
changes made to files in the working directory that haven’t been staged
or committed yet.
If you want to undo changes to a file that
haven’t been staged or committed yet, you can use the “git checkout” (in
older versions) or “git restore” (in newer versions) command.
sh git restore <file> #or for older version git checkout -- <file>
Unstage Changes: If you’ve added changes to the
staging area (using “git add”) and want to unstage them without losing
changes in the working directory:
sh git reset HEAD <file>
Reflog to Recover Lost Commits: If you
accidentally use “git reset –hard” or otherwise lose commits, you can
often recover them using the Git reflog, which tracks all movements of
HEAD. sh git reflog
Find the commit
hash of the desired state, and then reset or checkout to that
commit.
YouTube video: Learn git in 15
mins
Interacting git learning website: Learn Git Branch
Try git comments in Learn Git Branch website.
Note that for the homework, we will be looking for evidence that you completed these tasks in your commit log.