CS 61B Intro to Gitlet
gitlet preview
prev: [[Git-intro]]
next:
number system
decimal number system 十进制
binary number system 二进制
hexadecimal number system 十六进制
hexadecimal is what you usually see in CS.
command line compilation
Hello.java –> javac –> Hello.class –> java –> running
public static void main(String[] args)(String[] args) refer to the command line argumentsGit we are using today are written in C, which is compiled directly into a binary and does’t need the complier. So for c programs, we just call git status instead of java git status.
Git
Why version control?
maintaining multiple copies is useful for the project.
git preview: things haven’t covered
- Maps.
- Hashing.
- File I/O.
- Graphs.
How it works
every time commit, store a copy of the entire repo in a secret folder in your computer, called .git.
Naive approach 1: each commit is stored in a subdirectory with copies of each file.
Inefficient:
- several identical files.
![[屏幕截图 2024-04-13 173120.png]]
approach 2: Store only files that change
![[屏幕截图 2024-04-13 173329.png]]
Inefficient: To figure out which files to copy, we have to walk through all of the commits to determine which file we gonna use.
Appoarch 3: 2 but version data structure
Each commit is a “map” or “dictionary”. For each filename, it maps that filename to a version number.
Example: V4 in Python might be represented by {“Hello.java”: 2, “Friend.java”: 4, “Egg.java”: 3}
Advantage: do not need to think which one to select.
However, if the A.java in V5 is the same as that in V1, how to increase efficiency?
Hashing
examine the difference between files
Another question: if 2 programmers working on the same project:
- both start at V3
- A changes Horse.java and commit
- B change Fish.java and commit
- who is V4?
Suggest B first, A’ s computer would have no idea that this commit have been made.
Approach 4: use time & date as the version number
Rather than using an escalating integer version number, we could use the current time and date.
- Possible concern: Two programmers make commits (or files) at the same time.
Approach 5: Use a “Hash” as the Version Number
Git use “git-SHA1 hash” of the file as its version number.
Also, git use the git-SHA1 hash to store the data.
drawback: maybe the same hash number for some files. (the chance is very low!)
A benefit of Hashing: security (detecting hash)
Serializable
see in lab6
used for enable classes to write/read objects from files
Branching
merging
first:
1 | git log --graph --oneline --all --decorate |
then:
1 | git checkout -b WithSwiss aa45fbd |
Can switch back to the master branch with checkout.
1 | git checkout master |
Can also attempt to merge branches.
1 | git merge WithSwiss |
if the 2 branches did modification to the same file, then the merge conflict happens, because git didn’t know which file to follow.
![[屏幕截图 2024-04-13 205131.png]]
