Write in front
Recently, I found that many friends are not very familiar with the use of some basic tools at work, such as: Git, a distributed code management warehouse, many of them are not very familiar or not very familiar. Even some friends have never heard of Git, so they just use SVN. As everyone knows, today's major Internet giants and emerging Internet dark horse companies basically use Git, and basically abandoned the use of SVN. why? Let's look down together.
The difference between Git and SVN
Different storage methods
Git stores the content in a metadata format similar to a k/v database, while SVN is file-based (the new version of SVN has been changed to metadata storage)
Here, we give a simple Git usage example.
cd .git/objects/df/
git cat-file -p df70460b4b4aece5915caf5c68d12f560a9de56e
echo 'version1' > text.txt
git hash-object -w text.txt
Different ways of use
To push files locally to remote services, SVN only needs commint and Git needs three steps: add, commint, and push.
For example, we use the following figure to simulate the use of SVN.
We can use the following figure to simulate the use of Git.
Version management mode is different
Git is a distributed version management system, while SVN is a remote centralized management system.
For example, we can use the following figure to represent the centralized management of SVN.
We can use the following figure to represent the distributed management of Git.
Summary of Git core commands
Git client installation
Official client download: https://git-scm.com/downloads
Download other clients: https://tortoisegit.org/download/
Use of Git commands
(1) Clone to local based on remote warehouse
git clone <remote_url>
(2) The current directory is initialized as a git local warehouse
git init <directory>
(3) Create project based on mvn template
mvn archetype:generate
Add locally
(1) Add the specified file to the temporary storage area
git add <fileName>
(2) Add the specified directory to the temporary storage area
git add <directory>
(3) Add all
git add -A
(4) Remove the specified directory and subdirectories from the temporary storage area
git rm --cached target -r
(5) Add the configuration file .gitignore
Submit locally
(1) Submit to local warehouse
git commit file -m '提交的注释信息'
(2) Quickly submit to local warehouse
git commit -am '快捷添加与提交'
Branch management
(1) View the current branch
git branch [-avv]
(2) Create a new branch based on the current branch
git branch <branch name>
(3) Create a new branch based on submission
git branch <branch name> <commit id>
$ git branch -d {
dev}
(4) Switch branches
git checkout <branch name>
(5) Merging branches
git merge <merge target>
(6) Resolve conflicts
If the automatic merge fails due to conflicts, the status is mergeing at this time. Need to manually modify and resubmit (commit)
Remote warehouse management
(1) View remote configuration
git remote [-v]
(2) Add remote address
git remote add origin http:xxx.xxx
(3) Delete the remote address
git remote remove origin
(4) Upload the new branch to the remote
git push --set-upstream origin master
(5) Associate the local branch with the remote
git branch --track --set-upstream-to=origin/test test
Tag management
(1) View current
git tag
(2) Create a branch
git tag <tag name> <branch name>
(3) Delete branch
git tag -d <tag name>
Log management
(1) View all commit logs under the current branch
git log
(2) View all commit logs under the current branch
git log {
branch}
(3) Single line display log
git log --oneline
(4) Compare the difference between the two versions
git log master..experiment
(5) Display the submitted merged network in a graph
git log --pretty=format:'%h %s' --graph
Git underlying principles
GIT storage object (hashMap)
Git is a content-addressable file system. Its core part is a simple key-value data store. You can insert any content into the database and it will return a hash key to retrieve the value. .
(1) Insert data in the Git key-value library
echo 'binghe' | git hash-object -w --stdin 79362d07cf264f8078b489a47132afbc73f87b9a
(2) Obtain the specified content based on the key
git cat-file -p 79362d07cf264f8078b489a47132afbc73f87b9a
Based on this function, Git saves the content of each file version in the database, and when the version is to be rolled back, one of the keys is used to retrieve and replace the period.
Git version writing and rollback process
(1) Find all git objects
find .git/objects/ -type f
(2) Write version 1
echo 'version1' > README.MF; git hash-object -w README.MF;
(3) Write version 2
echo 'version2' > README.MF; git hash-object -w README.MF;
(4) Write version 3
echo 'version3' > README.MF; git hash-object -w README.MF;
(5) Roll back the specified version
git cat-file -p c11e96db44f7f3bc4c608aa7d7cd9ba4ab25066e > README.MF
So our usual git add is actually to insert the modified content into the key-value library. When we execute git add README.MF, it is equivalent to execute git hash-object -w README.MF to write the file to the database.
We have solved the storage problem, but it can only store content and does not store the file name. If you want to roll back, how do you know which content corresponds to which file? Next we will look at the tree object, which solves the problem of file name storage.
Git tree object
The tree object solves the file name problem. Its purpose is to organize multiple file names together. It contains multiple file names and their corresponding keys and references to other tree objects, which can be understood as files in the operating system. Folder, a folder contains multiple files and multiple other folders.
Each branch is associated with a tree object, which stores all file names and corresponding keys under the current branch. You can view it with the following command
git cat-file -p master^{
tree}
Git commit object
A submission is a snapshot of the current version. The snapshot is saved by submitting the object. The stored content is: a top-level tree object, the object that was submitted last time, the submitter's username and email address, and the submission timestamp. submit comments.
$ git cat-file -p b2395925b5f1c12bf8cb9602f05fc8d580311836
tree 002adb8152f7cd49f400a0480ef2d4c09b060c07
parent 8be903f5e1046b851117a21cdc3c80bdcaf97570
author binghe <[email protected]> 1532959457 +0800
committer binghe <[email protected]> 1532959457 +0800
In summary, we can infer that a total of three objects are generated from the process of modifying a file to submitting:
- A content object: the content of the file is stored
- A tree object: the key of the file name and content object is stored
- A submission object: stores the key of the tree object and submit comments.
Git references
When we execute git branch {branchName}, a branch is created. The essence is to create a reference file based on the specified commit in git and save it under .git\refs\heads\.
(1) Create a branch
git branch dev
cat.git\refs\heads\dev
There are three types of references in Git:
- Branch reference
- Remote branch reference
- Label reference
(2) Query and compare two versions
git log master..experiment
(3) Version submission history network
git log --pretty=format:'%h %s' --graph
(4) View the branch tree
git cat-file -p master^{
tree}
Well, let's talk about it today! Don't forget to like it, give it to someone who is watching and forward it, so that more people can see, learn together and make progress together! !