preface
Git is an open source distributed version control system for agile and efficient handling of any project, large or small. It is actually a Git key-value file storage system.
Learn the key
Basic use of Git
Git operation terms
Git common workflow details
Git checkout
Git key-value file system
Build git Commit tree manually
1. Basic use of Git
1.1 Git Initialization
To use Git, you need to create a repository. You can either use an existing directory as your Git repository or create an empty directory using the current directory as your Git repository using the following command:
git init
Copy the code
To use the specified directory as your Git repository, run the following command:
git init path
Copy the code
1.2 Git Local User Information Operation
1.2.1 Viewing Git Local User Information
Git config user.email git config user.email git config user.emailCopy the code
1.2.1 Modifying Git Local User Information
// Change the git user name git config user.name New user name // Change the git mailbox git config user.email new mailboxCopy the code
1.3 Git Global User Information Operation
1.3.1 Viewing Git Global User Information
Git config --global user.email git config --global userCopy the code
1.3.2 Modifying Git Global User Information
Git config --global user.email Git config --global user.email Git config --global user.email git config --global userCopy the code
1.4 Adding files to git Repository
You can use the add command file is added to the index/suspend area and before the file is added to the index/suspend area, will first determine whether index/suspend the zone file index of this file is included in the key information, if have the key to the file information, won’t do any operation (even if git binary file, the file system, this file does not exist If the index file does not contain the key information of the file, the binary data of the file will be added to the git file system (i.e. the objects folder, if the binary data of the file does not exist in the objects folder), using the following command:
git add filename
Copy the code
Before using this command, take a look at the file data stored in Git, as shown below:
You can see that after git initialization, there is no data in the index/staging area and no files have been added to the Git file system before any files have been added.
Use the command above to add the file, as shown below:
Can be found that this command will add files to the index/suspend area (namely the increase of the index file, the file does not store the file content, only some of the key storing files), and add the file to the git file system, you can use the following command to view the index hexadecimal data in the file:
// Hexdump index file path // Calculate the hash value of the git hash-object file pathCopy the code
The data in the red box is the hash value of the file added to the index/hold area, as shown below:
1.5 Deleting files from git repository
You can delete files using the rm command, as shown below:
git rm filename
Copy the code
The specific usage is shown in the figure below:
Using rm alone will generate an error. According to the comment, you need to pass in parameters to use rm command, as shown below:
--cached
: This parameter means to delete files only from index/hold, but not from working directory. The full command is as follows:
Git rm --cached file pathCopy the code
The command above will only delete the files in the pause area, but the files in the working directory will still be there, as shown below:
If you look at the hexadecimal data in the index file, you can see that the index/delay area removes the information from the file, as shown below:
-f
: Force the file to be deleted (the index/hold area and the file in the working directory will be deleted)
Git rm -f Specifies the path to the git fileCopy the code
The command above will only delete files in the pause area and files in the working directory, as shown below:
Look at the hexadecimal data in the index/hold file index at this point, as shown below:
Note: The delete operation does not clear the deleted file ingit
Binary file of the file system
1.6 git add .
The command
Git git git git git git git git git git git git git Check whether the file is already added to the Git file system (i.e. the Objects folder). If not, add it to the Git file system first.
Git add is the directory where the. Git file is located.Copy the code
Before using git add., create two TXT files and record the current git status.
Take a look at the hexadecimal information in the index file, as shown below:
Take a look at what is stored in the Git file system, as shown below:
Execute the git add. command as shown below:
First look at the hash values of all TXT files, as shown below:
Take a look at what has been added to the Git file system, as shown below:
Finally, take a look at what has been added to the index/hold file index, as shown below:
Modify the contents of the file_1.txt file as shown below:
The hash value of the modified file_1. TXT file is as follows:
Performed againgit add .
Command, as shown in the following figure:
Take a look at what has been added to the Git file system, as shown below:
Finally, take a look at what has been added to the index/hold file index, as shown below:
What you can see is that the index/cache file index stores the last git add execution. Key information about the file after the git add command (if you have modified the same file several times and executed git add) Git add is stored in the git file system every time you execute git add. The binary data of the new file generated in the operation.
1.7 Git commit command
This command is used to commit the file information recorded in the index \ deferred file index to the local repository:
Git commit git commit git commit
Git commit -m 'Enter some commit information here'Copy the code
Git git git git git git git git git git git git
Record the contents of the Git file system as shown below:
Execute git commit as shown in the following figure:
Now take a look at what has been added to the Git file system, as shown below:
Finally, take a look at what has been added to the index/hold file index, as shown below:
Then add the contents of file_3.txt to the index area and see what happens to the data, as shown below:
Run git add. And check the hash value of the file_3. TXT file, as shown in the following figure:
See what’s added to the Git file system, as shown below:
Take a look at the new additions to the index/pause file index, as shown below:
Commit the code again using the git commit command, as shown below:
See what’s added to the Git file system, as shown below:
Take a look at the new additions to the index/pause file index, as shown below:
1.8 Viewing the Current Branchgit commit
The historical record
If you want to check the git commit history of your current branch, you can use the following command:
git log --oneline --decorate --graph --stat
Copy the code
The effect is shown in the picture below:
1.9 Roll back the submitted code
The code to roll back a commit uses the git reset command (you can also specify a rollback to a previously committed version), where HEAD is used to point to the branch’s last committed object, as shown below:
Roll back only the current HEAD
The complete command used is as follows:
Git reset --soft HEAD~1Copy the code
Examples are as follows:
View the commit record after rollback
With this rollback, the last commit record is gone, but the contents of the index file remain unchanged, as shown below:
TXT file. The file_3. TXT file has been added to the index file but has not been committed to the local repository. The file still exists in the working directory, as shown in the following figure:
Submit the code again, as shown below:
To roll back index, use the following command:
Git reset HEAD. //Copy the code
Execute the command, as shown below:
Git reset HEAD reverses all operations performed on the index file since the last commit
To roll back the working directory, use the following command:
git checkout -- .
Copy the code
Execute the command, as shown below:
1.9.2 Rolling back the current HEAD and INDEX (Index/delay zone)
The complete command is as follows:
Git reset --mixed HEAD~1Copy the code
Examples are as follows:
With this rollback, the last commit record is gone and the contents of the index file are changed, as shown in the following figure:
TXT file. The file_3. TXT file has not been added to the index file or submitted to the local repository. The file still exists in the working directory, as shown in the following figure:
Execute the git add. command and submit the code as shown below:
1.9.3 Rolling Back the CurrentHEAD
,index
(index/hold area) and working directory
This operation is irreversible, so use with caution!! Because the file information will be deleted from the working directory!! The complete command is as follows:
git reset --hard HEAD~1
Copy the code
Before doing this, take a look at git commit records, as shown in the following figure:
See what’s added to the Git file system, as shown below:
Take a look at the data in the index file, as shown below:
The content of the file_3. TXT file is as follows:
Git reset –hard HEAD~1
If you look at the commit record, the last commit record is deleted, as shown in the following figure:
After the rollback, the data stored in the Git file system does not change, as shown in the following figure:
Take a look at the data in the index file, as shown below:
The content of the file_3. TXT file is as follows:
1.11 Using Branch
- use
branch
To create a branch, run the following command:
Git branch Specifies the branch nameCopy the code
An example is shown below:
- use
branch
To view branch information, run the following command:
Git branch --listCopy the code
An example is shown below:
- use
branch
To delete the branch, run the following command:
Git branch -d Specifies the branch nameCopy the code
An example is shown below:
1.10 Use of checkout
- Use checkout to switch branches as follows:
Git checkout branch nameCopy the code
An example is shown below:
- Use checkout to create a new branch and switch immediately, as follows:
Git checkout -b new branch nameCopy the code
An example is shown below:
- Use Checkout to re-store the workspace file as follows:
// Re-store the workspace file git checkout --Copy the code
2. Storage principle of Git
Git actually has no concept of a folder. It saves files by the path where they were saved. To verify this idea, create a folder and perform the following operations:
If you need to submit an empty folder, you can create a hidden file in the empty folder and name it.keep or.gitkeep. Then the submission will be successful, as shown below:
2.1 Nature of the git add. Command
First create the file shown below in the path shown below:
Initialize git and view the files in.git, as shown below:
After the git add. command is executed, the objects folder produces the following files:
In fact, the generated folder name + file name in the folder is the hash value of the files in the working directory, and the contents stored in these files are the binary data of the corresponding files in the working directory. You can print the contents of the files using the following command:
Note: Whenever the contents of the file (not the path) are modified and executedgit add .
The command generates a new file in objects because the hash value of the file has been changed
Git cat-file -p git cat-file -p git cat-file -pCopy the code
The print result is as follows:
That is, Git splits the 40-bit HASH that stores objects into two parts:
-
The first two bits are folders
-
The last 38 bits are the file name of the object
The main reasons why Git designs the directory structure this way, rather than directly using a 40-bit hash file name are as follows:
-
Some file systems have limits on the number of files in a directory. For example, FAT32 limits the maximum number of files in a single directory to 65535.
-
Some file system search files are linear search. The more files in a directory, the slower the access is.
In addition to generating the files in the Objects folder, git add. The action also generates an index file that represents the contents of the Git stage. This file is a binary file that holds all information about the file being staged, such as inode information, hash values, etc. You can use the following command to view the binary data in the index file:
Hexdump index File pathCopy the code
The actual use is shown in the figure below:
If you delete the corresponding files generated in the Objects file and the index file, you get git add. This is what the git add. command actually does.
Git add. Git add.
- Computes the hash value of the file and writes
git
For the file system database (that is, in the objects folder), use the following command:
Git hash-objct -w Specifies the file pathCopy the code
Examples are as follows:
You can also write files to your Git database using the following shell commands
for i in {1.. 3}; \ do \ echo "$(git hash-object -w file_${i}.text)"; \ doneCopy the code
- To add the files from the working directory to the cache/staging area (that is, the index file), use the following command:
// --add adds a specified file. // --cacheinfo Adds specified information in a specified format. // 100644 mode git update-index --add -- cacheInfo 100644 File hash nameCopy the code
Mode has several types as shown in the figure below
An example is shown below:
- To view the file information stored in the hold area, use the following command
git ls-files -s
Copy the code
An example is shown below:
2.2 Nature of the Git commit command
Assume that the project file structure looks like the following figure, and that all files are already using Git Add. Add to the hold area.
The file stored in index/cache file index is shown below:
Git file system stores files as shown below:
Git commit consists of two commands, as follows:
git write-tree
: This command is based on the current situationgit
index
The file path in the file generates some corresponding tree structure files (the contents of these files are the key information of the files contained in them), and stores these tree structure files ingit
In the file system, see the following operations for details:
git write-tree
Copy the code
After git write-tree is executed, four new binary files are generated for objects, as shown below:
To check the contents of a git file based on the hash value of the file stored in the git file system, run the following command:
Git cat-file specifies the hash value of the file. Git cat-file specifies the hash value of the fileCopy the code
Concatenate the hash values of these files and print the contents of these files using commands, as shown below:
That’s what the git write-tree command does.
git commit-tree
: Submit the tree structure information generated above.
First, take a look at the Git commit record, as shown below:
If no record is currently committed, use the following command to commit:
Git commit - tree tree hash value - m "submit information" - the hash value of the parent node p/and/or use echo "submit information" | git commit tree tree hash value - p - the hash value of the parent nodeCopy the code
Use as shown below:
Check the current git status, as shown below:
Git commit-tree does not commit successfully because the git commit-tree command only generates the ID of a commit object. The commit object has not yet been written to the git file system. You need to use the following command to write the commit object to a branch:
Git update-ref refs/heads/ commit Specifies the object IDCopy the code
Use as shown below:
The above command will produce the following data:
Through the command view bb3a7ee7d65f44bf8a5bf3a923dc953d3a525838 the contents of the file, as shown in the figure below:
2.3 Manual Constructiongit commit tree
Git write-tree creates a new tree with the git write-tree command. Git write-tree creates a new tree with the git write-tree command.
// Create a parent tree Git read-tree --prefix= new tree name/sub-tree Hash value // Build the current tree (parent tree) git write-treeCopy the code
The following is an example:
- Build a tree
temp_project
New trees to makeproject
As a child tree
Execute git write-tree to save the binary data for the new tree to the Git file system, as shown below:
- Build a tree
temp_files2
New trees to makefiles2
As a child tree
Execute git write-tree to save the binary data for the new tree to the Git file system, as shown below:
You can also restore the built tree from Git to your working directory using the following command:
Git checkout -- build tree name /Copy the code
The following is an example:
- Restore temp_project tree
- Restore temp_files2 tree
- Resubmit the code
Check git status, as shown below:
performgit add .
The command
Check git status again, as shown below:
Submit the code as shown below: