0% found this document useful (0 votes)
25 views33 pages

Linux 2023 Fall 3 Version Control System Git

Uploaded by

pmaio0211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views33 pages

Linux 2023 Fall 3 Version Control System Git

Uploaded by

pmaio0211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Chia-Heng Tu

Dept. of Computer Science and Information


Engineering
National Cheng Kung University
Fall 2023
Outline
• Version Control System
• Git
• GitHub
• GitHub flow

September 19, 2023 2


VERSION CONTROL SYSTEM

September 19, 2023 3


What is Version Control System (VCS)?
• VCS records changes to a file or a set of files
over time
– E.g., documents, computer programs, large web sites,
and other collections of information

• It allows you to
– revert the selected files back to a previous state,
– revert the entire project back to a previous state,
– compare changes over time, and
– see who last modified something that might be
causing a problem, and who introduced an issue and
when, and more
By Revision_controlled_project_visualization.svg: *Subversion_project_visualization.svg: Traced by User:Stannered, original by en:User:Sami Keroladerivative work: Moxfyre
(talk)derivative work: Echion2 (talk) - Revision_controlled_project_visualization.svg, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=9562807
September 19, 2023 4
Why VCS?
• If we use different filenames
to control file versions, the
names make everyone confused
about relation between versions
• Things get more complex when
it is a collaborative work

https://peerj.com/preprints/3159/?utm_content=bufferc4f11&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer
September 19, 2023 5
Version Control Systems
• RCS (Revision Control System), 1982
• CVS (Concurrent Versions System), 1990
• SVN (Apache Subversion), 2000
• Git, 2005 Centralized version control

Centralized version control


These systems have:
•a single server that contains all
the versioned files, and
•several clients that check out
files from that central place
•For many years, this has been
the standard for version control

https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control September 19, 2023 6


Git overview: distributed version control
Snapshot-based version control
File states
Git commands (config, init, add, commit, checkout, branch, reset)

GIT

https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control September 19, 2023 7


Git
• Created by Linus Torvalds in 2005 for Distributed version control
development of the Linux kernel
– with other kernel developers contributing
to its initial development
• It is free and open source:
https://git.kernel.org/pub/scm/git/git.git/

• Written in C for speed and portability


• Distributed and Decentralized
– Independent of network access or a
central server
– git log is 100x faster than svn log because
the latter must contact a remote server
https://git.wiki.kernel.org/index.php/GitFaq#Why_the_.27Git.27_name.3F September 19, 2023 8
Git (Cont.)
• Strong support for non-linear development
– branching and merging
• Efficient handling of large projects
– fetching version history from a locally stored repository
faster than from the remote server

September 19, 2023 9


Snapshot-based Version Control
• Git considers the file contents as
a stream of snapshots Modified,
– A snapshot of a file is created when the file is modified new store

– To be efficient, if a file is not modified, it uses a link to the Not changed,


previous version of file that has already been stored link to previous

• As a result, each version of the project contains snapshots


of the monitored files

September 19, 2023 10


Delta-based Version Control
• The version control systems, such as CVS & Subversion,
keep the changes (i.e., deltas) made to each monitored file
over time
– By recording only the changes made to a file, it saves disk spaces
– Recovering the file is done by applying the series of changes
made to the base version

Further information for snapshot and delta based version control September 19, 2023 11
Check if a File Has Been Changed
• Git uses the checksum of a file to tell if the file has been
modified since the checksum has been calculated
– The checksum is calculated by computing the SHA-1 hash value
of the contents of the file or the directory structure
– The SHA-1 hash value is a 40-character string composed of
hexadecimal characters
git hash-object takes some data, stores
it in your .git/objects directory (the
object database), and gives you back
the unique key that now refers to that
data object
The first two characters of the hashing
string is used as the folder name of the
created object

zlib-compressed contents

To check the original


content
Courtesy of https://git-scm.com/book/en/v2/Git-Internals-Git-Objects; image source September 19, 2023 12
The States of a File
• Untracked / Modified
- Untracked means that the file is new to the Git project and is not monitored
by the versioning system
- Modified means that the file has been seen before (tracked) and has been
changed, but is not ready to be snapshotted by Git (not staged)
The typical flow of manipulating a file:
• Staged Edit/Stage/Commit
- When a file becomes staged, it's taken
into the staging area
- This is where Git is able to take a
snapshot of it and store its current
state to the local repository
- This area is also known as the Index

• Committed
- Git has officially taken a snapshot of
the files in the staging area and stored
a unique index in the Git directory
Courtesy of https://code.snipcademy.com/tutorials/git/fundamentals/three-states-areas
https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things September 19, 2023 13
Commands of File States
• List the states of project files with • To intentionally ignore the
git status untracked files with .gitignore
– It lists the current branch
– It simply shows you what's been • A .gitignore file specifies
going on with git add and git intentionally untracked files
commit
that Git should ignore
# On branch master
# Changes to be committed: – Files already tracked by Git are
# (use "git reset HEAD <file>..." to unstage)
# not affected
#modified: hello.py
# – The example contents of the file
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed) ### C ###
# (use "git checkout -- <file>..." to discard changes in working # Prerequisites
directory) *.d
#
#modified: main.py # Object files
#
*.o
# Untracked files:
# (use "git add <file>..." to include in what will be committed) #
*.ko Prevent compiled
#hello.pyc *.obj Python modules from
*.elf appearing in git status
*.pyc
Courtesy of https://git-scm.com/docs/gitignore
https://www.atlassian.com/git/tutorials/inspecting-a-repository September 19, 2023 14
Useful Commands
• A convenience function git config, which is used to set
Git configuration values
– E.g., email, username, editor and merge tool
• To set the Git configuration on different levels
- local: Setting is applied to the context repository git config gets
invoked in; i.e., in the repo's .git directory: .git/config
- global: Setting is applied to an operating system user in a file that is
located in a user's home directory, e.g., ~ /.gitconfig
- system: Setting is applied across an entire machine, and the file is at the
system root path, e.g., $(prefix)/etc/gitconfig
– An example to set the user email: git config --global
user.email [email protected]"
Courtesy of https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config September 19, 2023 15
Useful Commands (Cont.)
• git init
– Create an empty Git repository or reinitialize an existing one
• git add
– Add file contents to the staging area
Untracked
git add Unmodified
Modified
• git commit
– Record staged changes to the repository
commit
- HEAD points to current branch
hash

git commit

September 19, 2023 16


Useful Commands (Cont.)
• git branch
• Branch:
– You spawn a new branch to encapsulate your changes; e.g., add a
new feature or fix a bug—no matter how big or how small
– Git branches are effectively pointers to snapshots of your changes

•The diagram visualizes a repository with


two isolated lines of development, one
for a little feature, and one for a longer-
running feature
•By developing them in branches, it’s not
only possible to work on both of them in
parallel, but it also keeps the main
master branch free from questionable
code

Courtesy of https://www.atlassian.com/git/tutorials/using-branches September 19, 2023 17


Useful Commands (Cont.)
• git branch • Example:
– List all of the branches in your – git branch <new-branch-name>
repository
– This is synonymous with
git branch –list git branch …
$> git branch
master another_branch
• git branch -a feature_inprogress_branch - It's important to understand that branches are
just pointers to commits
– List all remote branches - When you create a branch, all Git needs to do
is creating a new pointer, it doesn’t change
• git branch -m <branch> the repository in any other way
– Rename the current branch to – git branch crazy-experiment
<branch> – This only creates the new branch
• git branch -d <branch> – git checkout crazy-experiment
– Delete the specified branch – To start adding commits to it, you need
to select it with git checkout, and
– This is a “safe” operation in that Git
prevents you from deleting the branch – then use the standard git add and git
if it has unmerged changes commit commands
– Check -D option by yourself

Courtesy of https://www.atlassian.com/git/tutorials/using-branches September 19, 2023 18


A Good Commit Message Matters
• Communicate context about the code An uninformative example

change to fellow developers (and indeed to


their future selves)
– A diff will tell you what changed, but only the
commit message can properly tell you why
• The seven rules
1. Separate subject from body with a blank line An example for your reference
2. Limit the subject line to 50 characters $ git log
commit 42e769bdf4894310333942ffc5a15151222a87be

3. Capitalize the subject line Author: Kevin Flynn <[email protected]>


Date: Fri Jan 01 00:00:00 1982 -0200

4. Do not end the subject line with a period Derezz the master control program

5. Use the imperative mood in the subject line MCP turned out to be evil and had become intent on
world domination. This commit throws Tron's disc into

6. Wrap the body at 72 characters MCP (causing its deresolution) and turns it back into a
chess game.

7. Use the body to explain what and why vs. how


https://chris.beams.io/posts/git-commit/ September 19, 2023 19
Scenario 1
Modify the Most Recent Commit
• git commit --amend
– A convenient way to modify the most recent commit
– It lets you combine staged changes with the previous commit
instead of creating an entirely new commit
– It can also be used to rewrite to the last commit message

Courtesy of https://www.atlassian.com/git/tutorials/rewriting-history September 19, 2023 20


Scenario 2
Undo Things (One Step Backward) Untracked
• Undo the stage command to untrack some files (before “add”)

Unmodified Modified
➢ git reset HEAD (Uncommitted) (before “add”)
➢ git reset master
➢ git reset <current SHA>

• Uncommit the last commit without recovering the files


➢ git reset HEAD^ Untracked (before “commit”)
➢ git reset master^ Unmodified
(Committed) Modified (before “commit”)
➢ git reset <last SHA>

➢ The above three commands are identical


Check this page for more of Git Reset. September 19, 2023 21
Scenario 3
Reset the Changes
• Reset to last (specific) commit
– I.e., it destroys the modifications of the current commit
– git reset --hard HEAD^
– git reset --hard master^
– git reset --hard <SHA>

Untracked Untracked

Modified Unmodified: as in a7c5b5d

➢ The above three commands are identical September 19, 2023 22


Scenario 4
New Branches
• git checkout <branch name>
– switch to the specific branch, <branch name>
– The git branch command creates a new branch off master using git
branch new_branch
– Once created, you use git checkout new_branch to switch to that branch

• git checkout –b <branch name>


– create the new branch and immediately switch to it

• git checkout -b <new-branch> <existing-branch>


– By default git checkout -b will base the new-branch off the current HEAD
– An optional additional branch parameter can be passed to git checkout
– In the above example, <existing-branch> is passed which then bases new-
branch off of the existing-branch, instead of the current HEAD
Courtesy of https://www.atlassian.com/git/tutorials/using-branches/git-checkout September 19, 2023 23
Scenario 4
New Branches (Branch Evolving Example)

checkout –b cat commit

checkout master commit

• At some point, you will need to


merge the branches
• Check how it works by yourself

September 19, 2023 24


GITHUB

September 19, 2023 25


Famous Git Repository Services
• GitHub (not opensource, belong to Microsoft)
• Bitbucket
• GitLab (opensource)

Courtesy of https://blog.bitrise.io/state-of-app-development-2016 September 19, 2023 26


Host Your Project on GitHub

Courtesy of
https://peerj.com/preprints/3159/?utm_content=bufferc4f11&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer
September 19, 2023 27
Sync Repository with GitHub
GitHub GitHub
Download the
Deviate from
latest content of
origin/master
origin/master
fetch

pull
(fetch + merge)

GitHub

GitHub

push
Merge the deviated Push the local data to
versions to form a the remote repo to
new version locally synchronize the repos
September 19, 2023 28
GitHub Flow
• GitHub flow is a lightweight, branch-based workflow
that supports teams and projects

• A typical flow
– Create a branch from master for new features
– After added the features with some commits, you push them
to GitHub as same branch
– Open a pull request for adding the new branch from the
master branch

Courtesy of https://guides.github.com/introduction/flow/ September 19, 2023 29


GitHub Flow (Cont.)

Create a Open a Pull Deploy Merge


branch Request (Usually by the
maintainer)

Add Discuss
commits and
code review
Courtesy of https://guides.github.com/introduction/flow/ September 19, 2023 30
Pull Request
• Usually provided by the Git repository service
• Web UI

September 19, 2023 31


Pull Request (Cont.)
• Two workflows
1. Pull Request from a forked repository
– A fork is a copy of a repository used in the fork and pull model
– Anyone can fork an existing repository and push changes to their personal
fork without needing access to the source repository
– The changes can be pulled into the source repository by the project
maintainer
2. Pull Request from a branch within a repository
– Used in the shared repository model, collaborators are granted push access
to a single shared repository and topic branches are created when changes
need to be made
– Pull requests are useful in this model as they initiate code review and general
discussion about a set of changes before the changes are merged into the main
development branch
– This model is more prevalent with small teams and organizations
collaborating on private projects
Courtesy of https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-collaborative-development-models
September 19, 2023 32
References
• Git user manual
• Visualizing Git Concepts with D3
• 為你自己學 Git – 高見龍 (線上電子書)
• A simple Git manual
• Git GUI tools
– TortoiseGit on Windows
– Git Desktop

September 19, 2023 34

You might also like