Distributed Version Control System - Git Details

Posted by J@ystick_FI on Wed, 13 Nov 2019 20:02:00 +0100

Preface:

The biggest difference between distributed and centralized is that developers can submit code locally, and each developer clones a complete git repository on the local machine.

The following is a classic git development process:

The git features are as follows:

  • Clone a complete git repository (including code and version information) from the server to a single machine;
  • Create branches and modify code on your own machine for different development purposes;
  • Submit code on branches you create on a single machine;
  • Merge branches on a single machine;
  • Get the latest version of the code fetch off the server and merge with your main branch;
  • Generate patches and send them to the main developer;

That's all about git. Next, I'll show you the basic operations of git.

Blog Outline:

  • 1. Install git
  • 2. Creation and introduction of git Library
  • 3. Basic operation of git Library
  • 4. Operation to Revoke Modifications
  • 5. Associate local git libraries with github
  • 6. Download from github to the local git version Library

git can be installed on Windows, mac, Linux and other operating systems. Here you will write down how to install it on Linux and its basic operations.

1. Install git

Very simple, just one command, as follows:

[root@git ~]# yum -y install git

2. Creation and introduction of git Library

#It is best to use an empty directory as the git Library
[root@git ~]# mkdir git
[root@git ~]# cd git
[root@git git]# git init   #Initialize to git Library in empty directory
//Initialize empty Git version library at/root/git/.git/
[root@git git]# ls -a      #After successful initialization, a hidden directory of.git is generated
.  ..  .git
#The generated hidden directory is used to track and manage the version library. It is not recommended to modify the files in the directory at will.
#If it's messed up, it destroys the git library.

There are three important concepts in the git version library: workspace, staging area, and version library.

  • Workspace: This is the directory you can see in your system.
  • Staging area: It is usually stored in an index file in the.git directory, so it is also called an index.
  • Version library: There is a.Git hidden directory in the workspace. This is not a workspace, but a version library for git.

The following diagram shows the relationship between a workspace, a staging area in a version library, and a version library:

In the figure above, the workspace is on the left, the version library is on the right, the area labeled "index" in the version library is the staging area, and the directory tree represented by the master branch is labeled "master".

When the "git add" command is executed on a workspace modified (or added) file, the directory tree of the temporary area is updated, and the contents of the workspace modified (or added) file are written to a new object in the object library, and the ID of the object is recorded in the file index of the temporary area.

When a git commit is performed, the directory tree of the staging area is written to the version library (object library), and the master branch is updated accordingly.That is, the directory tree that the master points to is the directory tree of the staging area at the time of submission.

When the "git reset HEAD" command is executed, the directory tree of the staging area is overridden and replaced by the directory tree pointed to by the master branch, but the workspace is not affected.

When the "git rm--cached <file>" command is executed, files are deleted directly from the staging area, and the workspace is unchanged.

When the command "git checkout." or "git checkout -- <file>" is executed, the files in the workspace are replaced with all or the specified files in the staging area.This operation is dangerous and will clear changes in the workspace that have not been added to the staging area.

When the command "git checkout HEAD." or "git checkout HEAD <file>" is executed, all or part of the files in the staging and workspace are replaced with those in the master branch that HEAD points to.This command is also dangerous because it clears uncommitted changes in the workspace as well as uncommitted changes in the staging area.

3. Basic operation of git Library

#You need to report your home first, declare your name and mailbox
[root@git git]# git config --global user.name "ljz"
[root@git git]# git config --global user.email "ljz@ljz.com"
[root@git git]# echo "aaaa" > git.txt      #Create a file for testing
[root@git git]# git add git.txt       #Add Test File to Staging Area
[root@git git]# Git commit-m "first submission"     
#Submit files from the staging area to the version library, and be sure to use the'-m'option to note the submission instructions
[master(Root Submission) eecbb4d] First submission
 1 file changed, 1 insertion(+)
 create mode 100644 git.txt
[root@git git]# echo "bbbb" >> git.txt      #Modify test file content
[root@git git]# git add git.txt       #Add to Staging Area
[root@git git]# git status        #View git status
# At branch master
# Changes to be submitted:
#   (Use "git reset HEAD <file>..." to evacuate the staging area)
#
#   Modify:      git.txt      #You can see the hint that git.txt has been modified
#New Test Files
[root@git git]# echo "Second Test File" > git2.txt
[root@git git]# echo "Third Test File" > git3.txt
[root@git git]# ls           #Confirm New Test File
git2.txt  git3.txt  git.txt
[root@git git]# git add git2.txt git3.txt         #Submit multiple files at once
[root@git git]# git status   #View git status
# At branch master
# Changes to be submitted:
#   (Use "git reset HEAD <file>..." to evacuate the staging area)
#
#   Modify: git.txt
#   New file: git2.txt
#   New file: git3.txt
#Above is a hint that git has been modified and two new files have been added
[root@git git]# git commit -m "Submit multiple versions"    #Submit multiple versions of a staging area
[master 86c5044] Submit multiple versions
 3 files changed, 3 insertions(+)
 create mode 100644 git2.txt
 create mode 100644 git3.txt
[root@git git]# git log --pretty=oneline         #View submission records, one line for one price increase
86c50445d84591741812e0bd9088a1c67bf5e9ec Submit multiple versions
eecbb4d9ff025681b4abe4ec2bdd90eeb0b66fd6 First submission

#So far, all the files under the git library have been submitted, so now I'll delete all the local files to see what the git status is
[root@git git]# rm git*       #Delete all test files in the current directory
[root@git git]# git status        #View git status
# At branch master
# Changes that have not yet been provisioned for submission:
#   (Use "git add/rm <file>..." to update the submission)
#   (Use "git checkout -- <file>..." to discard workspace changes)
#
#   Delete: git.txt
#   Delete: git2.txt
#   Delete: git3.txt
#The above prompt deleted three files, but the following is modified but not submitted
//Modifications have not yet been added to the submission (using "git add" and/or "git commit-a")

#So, what do I want to do now to restore the deleted files?Just do the following:
[root@git git]# git reflog --pretty=oneline         #View submission records
86c5044 HEAD@{0}: commit: Submit multiple versions
eecbb4d HEAD@{1}: commit (initial): First submission
[root@git git]# ls       #Make sure there are no test files in the current directory
[root@git git]# git reset --hard 86c5044      #Version rollback to specified post
HEAD Now at 86 c5044 Submit multiple versions
[root@git git]# ls      #Look again, the deleted file is back
git2.txt  git3.txt  git.txt

#So, now I want to revert to my first submission?

[root@git git]# git reflog --pretty=oneline      #You also need to view its log records
86c5044 HEAD@{0}: commit: Submit multiple versions
eecbb4d HEAD@{1}: commit (initial): First submission
[root@git git]# git reset --hard HEAD@{1}        #Yes, you can specify not only the ID number of the first column but also its HEAD field when the version is rolled back
HEAD Now in eecbb4d First submission
[root@git git]# ls    #Review the current working directory again and revert to the state where there was only one test file initially
git.txt
[root@git git]# git reset --hard 86c5044          #When restoring to the maximum number of test files again
HEAD Now at 86 c5044 Submit multiple versions
[root@git git]# ls
git2.txt  git3.txt  git.txt

4. Operation to Revoke Modifications

As for undoing changes, which are already shown above, how to undo changes from the repository, the following shows how to undo changes from the staging area, workbench

1. Undo modifications from the workbench

[root@git git]# cat git.txt   #Determine the current file content
aaaa
bbbb
[root@git git]# echo "cccc" >> git.txt      #Modify file contents
[root@git git]# cat git.txt       #View modified files
aaaa 
bbbb
cccc
[root@git git]# git checkout -- git.txt            #Undo the workbench
[root@git git]# cat git.txt         #Confirm that newly added content has been revoked
aaaa
bbbb

2. Revoke modifications from the staging area

[root@git git]# echo "abcd" > ljz.txt     #Create a new test file
[root@git git]# git add ljz.txt              #Add to Staging Area
[root@git git]# git status           #View git status
# At branch master 
# Changes to be submitted:
#   (Use "git reset HEAD <file>..." to evacuate the staging area)
#
#   New file:    ljz.txt        #Prompt for newly added files
[root@git git]# git reset HEAD ljz.txt         #Perform undo action
[root@git git]# git status       #Review git status again, prompt for empty submission, and prompt for git add to establish submission
# At branch master
# Untracked files:
#   (Use "git add <file>..." to include content to submit)
#
#   ljz.txt
//The submission is empty, but there are files that have not yet been tracked (use "git add" to establish a trace)

3. Delete the specified version from the version Library

[root@git git]# rm git.txt       #Delete local files
[root@git git]# git rm git.txt        #Use git to execute rm commands
[root@git git]# git commit -m "Delete Files"     #Submit to version library,

So far, this is just the basic operation of the git version library, so?How do we put our git
What about libraries associated with github?Below are the association methods in both cases.

5. Associate local git libraries with github

Scenario 1: There are git libraries locally and the github libraries are empty:

1. First you need to create an empty github library.

It's easy to register your github account and log in. I won't write it here.

Generate a key pair on the host and upload it to github:

[root@git git]# ssh-keygen -t rsa -C "916551516@qq.com"        #After executing this command, press Enter all the way, followed by your own mailbox address
[root@git git]# cat ~/.ssh/id_rsa.pub     #View the generated public key and copy its contents
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5ubfu4/UxpX/1aW3bPLUBihphbC0sUkmM0NusofTEW2rQ6Fy4+tRb2qvDLm6XXXBIzDNcr16qFGE95OFmhGF+M+TjjjvVyMFQu+qLjyfRceiYlLZ6II0ZY5+agQJSYlFYYyvHhJHFo69S07jx5A7Q2doLpW+O0i2MLdY0CyKvlRgE9Onoj+8TM9ZBfJdtoAGQdkH353NeiFJOJi71+KQgvvzpRiRiRjTv4mLGuWdeAAhxG1+rGnyotQktiobHGHKPLpm9w/PT95tuKQ/d8zH4BqsDkWuzIMy5E0vhELpEHFBilx6YuPL2h1N8YSFARxyz4zRPAQoCeATdgA+nD68z 916551516@qq.com

On github, do the following to add a public key:

Enter the password of the github account to verify:

Determine that the addition was successful:

2,

Back to the newly created library:


[root@git git]# ls -a       #The current working directory must be a git Library
.  ..  .git  git2.txt  git3.txt  git.txt  ljz.txt
[root@git git]# git remote add origin git@github.com:lvjianzhao/test01.git  #The first command to execute the prompt
[root@git git]# git push -u origin master        #Submit, because it is the first upload, you need to use the'-u'option
#If, after executing the above command, you are prompted to enter yes, then you can
Counting objects: 8, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (8/8), 563 bytes | 0 bytes/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To git@github.com:lvjianzhao/test01.git
 * [new branch]      master -> master
//Branch master is set to track remote branch master from origin.
#The prompt has been submitted successfully.

So F5 refreshes the page of the library we just created on github and you can see the files in our workbench directory, as follows:

6. Download from github to the local git version Library

The above has demonstrated how to associate a local git version library with a remote github empty library.

So here's how to download github's existing libraries (which have content) locally.

Since the mailbox and ssh keys are already set up for the fifth step, you can omit them here. If you do not configure the mailbox and ssh keys, you can refer to the fifth paragraph to configure them.

The github library you created in step 5 is downloaded locally.

Find the library created by github as follows:

[root@git /]# git clone git@github.com:lvjianzhao/test01.git  
#Execute the command "git clone" followed by the ssh path on the github we copied
//Cloning to'test01'...
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 8 (delta 0), reused 8 (delta 0), pack-reused 0
//Among recipients: 100% (8/8), done.
[root@git /]# cd test01/         #Once cloned successfully, test01 will be generated in the current directory
[root@git test01]# ls        #And content is what we have on github
git2.txt  git3.txt  git.txt
[root@git test01]# echo "clone success..." > succed   #Create a new test file
[root@git test01]# ls      #The following:
git2.txt  git3.txt  git.txt  succed
[root@git test01]# git add succed      #Add newly created files to the staging area
[root@git test01]# git commit -m "clone succes"     #Submit to Version Library
[master e62bdba] clone succes
 1 file changed, 1 insertion(+)
 create mode 100644 succed
 [root@git test01]# git push origin master    #Push local files to github
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 272 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:lvjianzhao/test01.git
   86c5044..e62bdba  master -> master

[root@git test01]# git remote       #View information about remote version Libraries
origin

Back on github, refresh the library's page and you'll see the newly submitted file as follows:

_________

Topics: Linux git github ssh