Linux 2023 Fall 3 Version Control System Git
Linux 2023 Fall 3 Version Control System Git
• 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
GIT
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
• 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
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.
Unmodified Modified
➢ git reset HEAD (Uncommitted) (before “add”)
➢ git reset master
➢ git reset <current SHA>
Untracked Untracked
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
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