[Shangsi Valley] Git and GitHub foundation complete tutorial - note 2

stay [Shangsi Valley] Git and GitHub foundation complete tutorial - note 1 There is a brief introduction to Git, as well as commands such as adding add, submitting commit, withdrawing reset, etc.

Let's recall the command of the last article, with good Txt as an example.

catalogue

0 recall the content of the previous article

4.3. 5 forward and backward (three modes)

4.3. 6 Comparison of three parameters of reset command

4.3. 7 delete and retrieve files

4.3. 8 compare file differences

Next, 4 Git command line operation

4.4 branch management

4.4. 2 what is a branch?

4.4. 3 branch operation

5 Git Fundamentals

5.1 introduction to hash algorithm

5.2 Git version saving mechanism

5.2. 1 file management mechanism of centralized version control tool

5.2. 2. Git file management mechanism

5.2. 3 details of GIT file management mechanism

5.3 Git branch management mechanism

5.3. 1 branch creation (i.e. creating a new pointer)

5.3. 2 branch switching (HEAD pointing)

0 recall the content of the previous article

4.3. 5 forward and backward (three modes)

git actually has a pointer when managing historical versions. The name of the pointer is "HEAD", which is easy to move. The essence of forward and backward is the movement of the pointer.

    

The first method: Operation Based on index value [recommended]

Now the pointer points to the record closest to us. Note: selecting the code in git means copying and pasting directly. Code format:

git reset --hard [Local index value] //Just take the first few digits of the index value

Actual operation code:

git reflog //View all history records, including pointer conditions

cat good.txt //See now good Txt file

git reset --hard a6ace91 //"Crossing"

git reset --hard 42e7e84 //Then "cross"

cat good.txt //See now good Txt file

git reset --hard 9a9ebe0 //Go forward "through"

cat good.txt //See now good Txt file

The second: use the ^ (XOR) symbol, and you can only go back.

Code format:

git reset --hard HEAD^^

Actual operation code:

$git log --oneline //View the history of the current version

$git reset --hard HEAD^ //Step back

$tail -n 3 good.txt //Save layout and display only good Last 3 lines of TXT file

$git log --oneline //View the history of the current version

$git reset --hard HEAD^^^ //Step back three steps

$tail -n 3 good.txt //View the current version, showing only the last 3 lines

The third kind: Using ~ (wavy line) symbol, you can only go back

git reset --hard HEAD~n  //Note: n steps backward

Actual operation code:

$git log --oneline //View the history of the current version

$git reset --hard HEAD^^^ //Step back three steps

//Also step back 3 steps

$git reset --hard HEAD~3  //Indicates 3 steps backward

Summary: the last two methods need to be counted in the history. git reflog can view all history records.

4.3. 6 Comparison of three parameters of reset command

(see the command help document: git help reset)

git help reset //The document is not online. Download it in advance

--soft parameters

Does not touch the index file or the working tree at all. (do not touch staging area and work area)

Only move the} HEAD pointer in the local library (green prompt when viewing the status: the local library and staging area are not synchronized)

--mixed parameter

Move the HEAD pointer in the local library

Reset staging area

(when viewing the status, red prompt: the local library and staging area are backward, and it feels like the workspace is moving forward)

--hard parameter

Move the HEAD pointer in the local library, and the three areas move together: reset staging area and workspace.

4.3. 7 delete and retrieve files

Actual operation code:

$vim aaa.txt //Create a new AAA Txt file

$git add aaa.txt

$git commit -m "new aaa.txt" aaa.txt

$git status

//On branch master.

//nothing to commit,working tree clean

$rm aaa.txt

$ll //View files in the current directory

$git status

//Tip: git add or git checkout

//deleted : aaa.txt

$git add aaa.txt

$git status

//Tip: git reset

//deleted: aaa.txt

$git commit -m "delete aaa.txt" aaa.txt

$git status

//On branch master.

//nothing to commit,working tree clean

//git will only add versions during operation, and will not delete historical versions.

$git reflog //View the history and locate the current version

$git reset --hard 567f4e1 //"Traverse" to delete previous version

$ll //View the files in the current directory and delete the AAA I got it back



$vim apple.txt //Create a new apple Txt file

$ll //There are two files, apple Txt and good txt

$git add apple.txt

$git commit -m "new apple.txt" apple.txt

$git reflog //View the history and locate the current version

$rm apple.txt

$ll //There is a file, good txt

$git add apple.txt

$git status

//Tip: git reset

//deleted: aaa.txt

$git reset --hard HEAD

//HEAD is now at ac36f52 new apple.txt

$ll //There are two files, apple Txt and good txt

Summary of deleting file retrieval method

The premise is that the status when the file exists before deletion is submitted to the local library.

Actual operation code:

//Entered file directory
rm aaa.txt  //Delete local file
git add aaa.txt   //Then submit to the staging area
git commit -m "delete aaa" aaa.txt   //Then submit to the local warehouse (deletion completed)

Retrieval operation:

git reset --hard [Pointer position] 

The delete operation has been submitted to the local library: the pointer position points to the history (back to the previous version that was not deleted)

The deletion operation has not been submitted to the local library: the pointer position uses HEAD(git reset --hard # HEAD)

4.3. 8 compare file differences

Compare file differences:

git diff [file name]      //Compare the files in the workspace with the staging area

git diff [Historical version in local library]  [file name]   //Compare the files in the workspace with the local library history, and compare multiple files without file names

Actual operation code:

$ll //There are two files, apple Txt and good txt

$git status

//On branch master.

//nothing to commit,working tree clean

$vim apple.txt //Modify apple Txt file

$git diff apple.txt //As long as there are modifications, use this command to compare and display red - (delete line) and green + (add line).

$git add apple.txt //Add this file to the cache

$git diff apple.txt //There is no difference in the display

$git diff HEAD apple.txt //Use the pointer to point to the local library, and compare the workspace with the local library

$git diff HEAD^ apple.txt //The workspace is compared to the previous version (history of the local library)

$git diff HEAD //Compare all files in the current workspace without a file name

Next, 4 Git command line operation

4.4 branch management

4.4. 2 what is a branch?

(1) During version control, multiple lines are used to advance multiple tasks at the same time.

After the version library is initialized, it has a master branch.

Suppose you want to develop some new functions instead of developing on the master trunk, so you open a new branch. New branch feature_blue is copied from the master. The content is the same when it was first created. Want to develop another function and create a new branch feature_ game. The two branches are developed separately. The development is completed, there is no problem with the test, feature_blue branches can be merged into the master. There may be bug s in the master, often hot_fix branch, generally hot repair (items running on the server do not stop), and then merge to the master branch after emergency repair. When the feature_game branch is developed and merged to the master, the master is a new master.

(2) Benefits of branching?

  • At the same time, promote the development of multiple functions in parallel to improve the development efficiency.
  • Each branch is independent and does not affect each other in the development process. If the development of one branch fails, it will not have any impact on other branches. The failed branch can be deleted and restarted. It's convenient for us to try and make mistakes. In fact, SVN also has branches, but it is inconvenient to use, and the management efficiency of branch files is low.

4.4. 3 branch operation

Actual operation code:

$git status

//On branch master.

//Your branch is up-to-date with 'origin/master'.

//nothing to commit,working tree clean

$git branch -v //View all branches now, only the master

$git branch hot_fix //Create a new branch

$git branch -v //View all branches now, with hot_fix and master (currently here)

$git checkout hot_fix//Switch branch

//Switched to branch 'hot_fix'

$git branch -v //View all branches now, with hot_fix (currently here) and master

$vim apple.txt //In hot_ File operation under fix branch

$git add apple.txt

$git commit -m "test branch hot_fix" apple.txt

//To merge code into the master, the current branch must be the master (standing on the accepted branch)

$git checkout master

$git merge hot_fix

//Updating...

$cat apple.txt //See that the content has been merged

summary

o1 create branch: git Branch [branch name] / * example: git branch hot_fix

o2 view branch: git branch -v

o3 switch branch: git checkout [branch name] / * git checkout hot_fix

o4 merge branch: git merge

//Step 1: switch to the branch to be modified (merged and new content added)

git checkout [Merged branch name master]  /*Switch branch

//Step 2: execute the merge command (merge branch instructions)

git merge [Branch name with new content]

//hot_fix a865afb hot_fix commit apple / * query branch effect after merging

//* master  a865afb hot_fix commit apple

o5 conflict resolution

Cause of conflict: when two branches modify the same location of the same file, but the modified contents are different.

$git branch -v //The two branches are consistent

hot_fix 235ac97 test branch hot_fix

master 235ac97 test branch hot_fix

$vim good.txt #Modify file

$git add good.txt

$git commit -m "test conflict" good.txt

$git branch -v //The two branches are inconsistent

//hot_fix 235ac97 test branch hot_fix

//master d715d90 test conflict

$git checkout hot_fix

$vim good.txt //In hot_ Under the fix branch, also modify good Txt, change the same line, line 9, and modify different contents

$git add good.txt

$git commit -m "test conflict hot_fix" good.txt

$git branch -v //The two branches are inconsistent

//hot_fix 4e38aa4 test conflict hot_fix

//master d715d90 test conflict

$git merge master //Merge the master directly

//Auto-merging good.txt

//CONFLICT(content):Merge conflict in good.txt

//Automatic merge failed;fix conflicts and then commit the result.

//A conflict in SVN will produce several additional files

$vim good.txt //Open the file and have a look

Performance of conflict: there is current branch content and another branch content

Conflict resolution:

Step 1: edit the file and delete special symbols

Step 2: modify the file to a satisfactory level, save and exit

$git status

//You have unmerged paths.

//(use "git merge --abort" to abort the merge)

//Unmerged path:

//(use "git add ..."to mark resolution)

//both modified: good.txt

Step 3: git add [file name]

$git add good.txt

Step 4: git commit -m "log information"

$git commit -m "resolve conflict" good.txt //An error will be reported after the file name

//fatal:cannot do a partial commit during a merge.

$git commit -m "resolve conflict"

//Note: the commit must not contain a specific file name at this time

5 Git Fundamentals

5.1 introduction to hash algorithm

Hash is a series of encryption algorithms (for hash, plaintext is not necessarily text, but audio, video and picture files). Although different hash algorithms have different encryption strength, they have the following common points:

① No matter how large the amount of input data is, if the same hash algorithm is input, the length of the encryption result is fixed. For example, md5 (hexadecimal number of 32), SHA1, CRC32.

② The hash algorithm is determined, the input data is determined, and the output data can be guaranteed to remain unchanged (no matter when the same data is executed, no matter how many times it is executed, the results are the same).

③ The hash algorithm determines that if the input data changes (even slight changes), the output data must change, and usually changes greatly.

④ Hash algorithm is irreversible (plaintext cannot be deduced from ciphertext)

The bottom layer of Git adopts SHA-1 algorithm.

Due to feature ③, hash algorithm can be used to verify files. The principle is shown in the following figure: (comparison of hash values before and after transmission)

Git relies on this mechanism to fundamentally ensure data integrity. (letters ignore case)

5.2 Git version saving mechanism

5.2. 1 file management mechanism of centralized version control tool

Store information as a list of file changes. Such systems regard the information they store as a set of basic files and the differences that each file gradually accumulates over time. For example, SVN.

Save only the modified part each time, which can save memory space.

5.2. 2. Git file management mechanism

Git sees data as a set of snapshots of a small file system. Each time an update is submitted, GIT will take a snapshot of all the current files and save the index of the snapshot. For efficiency, if the file is not modified, GIT will no longer re store the file, but only keep a link to the previously stored file. Therefore, GIT's working method can be called snapshot flow.

5.2. 3 details of GIT file management mechanism

(1) Git's "commit object" (hash value for each file)

All hash values constitute a tree object, which contains the hash value and the corresponding file. It also has its own hash value.

The submission object includes the tree object, which also has its own hash value.

$git log //You can see that each commit has a hash value

The management files in bitcoin and git have great similarities. Bitcoin each does a hash, and then two do a hash.

(2) The chain formed by the submission object and its parent object

Each version and snapshot will have a parent pointing to its parent node. The connection between versions through this parent-child relationship. The "submission object" also includes author information and committer information.

5.3 Git branch management mechanism

5.3. 1 branch creation (i.e. creating a new pointer)

In terms of data structure, this is also a list structure. "root commit" is submitted for the first time. The following master and testing can be regarded as pointers, which point to a certain version. Creating pointers is much more efficient than copying files.

5.3. 2 branch switching (HEAD pointing)

Switch to the testing branch and the HEAD pointer moves. As shown below:

The content was submitted when HEAD pointed to testing. The master stays where it is, and the testing branch has taken a step forward. As shown below:

Switch back to the master and re point the HEAD pointer to the master, as shown in the following figure:

   

When the HEAD points to the master, the data is submitted, and the master moves back one step. As shown below:

Conclusion: branching is a very important content in Git. The next article introduces our account management.

Note: take study notes. If you make mistakes, please correct them! It's not easy to write an article. Please contact me for reprint.

Keywords: Linux git github Network Protocol p2p

Added by coolfool on Thu, 16 Dec 2021 00:26:10 +0200