1. Overview of GIT
1.1 overview and advantages of GIT
(1) git is actually a version control tool (a brief overview)
-
Collaborative modification
a. Multiple people modify the same file on the server side in parallel without mutual influence
-
Data backup
a. It can not only save the current state of directories and files, but also save the historical state of each submitted
-
version management
a. When saving the file information of each version, do not save duplicate data, so as to save storage space and improve operation efficiency. In this regard, SVN adopts the way of incremental management, while git adopts the way of file system snapshot
-
Permission control
a. Control the permissions of the personnel involved in development in the team
b. Review the code contributed by team developers - unique to git -
Historical records
a. View the modification time and content log information of the modifier
-
Branch Management
a. Allow the development team to promote tasks in multiple production lines at the same time in the process of work, so as to further improve efficiency.
(2) Git's advantages
1.Most operations are done locally without networking 2.Integrity guarantee 3.Add data as much as possible instead of deleting the latter to modify data 4.Branch operation is very fast and smooth 5.And linux Full command compatibility
1.2 structure of GIT
(1) git structure
1.Workspace: writing code (using git add Add to staging area) 2.Staging area: temporary storage (use) git commit -m "Description (add to local library) 3.Local libraries: historical versions
PS: finally, the local database data can be pushed to the remote warehouse
1.3 installation of GIT
1.https://git-scm.com/ Go to the official website to download git
PS: Download according to your own system
2. After downloading, you can install it directly. Select all and then go to the next step
2. Operation of GIT command
2.1 simple operation of GIT command
(1) Local library initialization
1.Command: git init
PS: enter the command and press enter to see more than one git's hidden folder. If you input the command correctly but can't see this folder, it means that you haven't opened the display hidden file (the opening operation is as follows in win10 environment)
(2) Set signature
1.Commands: project level/Warehouse level: valid only in the current local library range git config user.name user name git config user.email mailbox 2.Command: system user level: the range of users who log in to the current operating system git config --global user.name user name git config --global user.email mailbox
PS: priority: in case of both, the principle of proximity is the project level
(3) View current change status
1.Command: git status
PS: first add a file (share. Txt) in the folder for demonstration, as shown in the figure
(4) Add files to staging area
1.Command: git add file name For example: git add shallow.txt 2.Command: git add . PS:This command adds all files to the staging area
PS: picture demonstration of command 1
PS: picture demonstration of Command 2
(5) Remove files added to the staging area
1.Command: git rm --cached File path
(6) Save the staging area file to the local library
1.Command: git commit 2.Command: git commit -m "Description of this submission"
PS: picture demonstration of command 1
1. After you input: git commit command will enter this interface (this interface requires you to fill in the submission instructions here)
2. Description submission method: press i to enter the description input status
PS: after compiling, press "ESC" key, then enter: wq and press enter
PS: demonstration of Command 2
It depends on how you use the specific interface. It depends on how you use it
(7) View history of submissions
1.Command: git log 2.Command: git log --pretty=oneline 3.Command: git log --oneline 4.Command: git reflog
PS: I won't do all the demonstrations, just the first one
PS: you can return the specified version according to this index value in the future
(8) Version forward and backward
1) Commands: index based 1.git reset --hard Index value 1) Commands: Based on^(This can only go back step by step, and one^The symbol represents a step) 1.git reset --hard HEAD^ 3)Commands: Based on~Numerical value(~5 (5 steps backward) 1.git reset --hard HEAD~numerical value
PS: take the first demo. Let's add more submissions first
PS: after running this fallback command, it returns to the specified version
(9) Comparison of three parameters of reset command
1. --soft 1)Move only in local library HEAD Pointer 2)Command: git reset --soft Index value 2. --mixed 1)Move in local library HEAD Pointer 2)Reset staging area 3)Command: git reset --mixed Index value 3. --hard 1)Move in local library HEAD Pointer 2)Reset staging area 3)Reset workspace 4)Command: git reset --hard Index value
PS: this can be understood in combination with the fallback version, so there is no demonstration
(10) Delete and retrieve files
1. Command: rm file name
PS: it can be seen from this. To delete a file, you also need to execute git add and git commit
PS: because we also go to git commit after deletion, we can go back to the version before deletion according to this idea, which is equivalent to retrieving the file
(11) Compare file differences
1.Command: git diff File name (compare with staging area)
PS: Green * * + * * are added contents, and red * * - * * are deleted contents
PS: supplementary: Command: git diff HEAD index value file name (compare with a version of the local library)
3. Branch of GIT
3.1 branches and benefits of branches
(1) What is a branch
1. In the process of version control, multiple lines are used to advance multiple tasks at the same time
(2) Benefits of branching
1. At the same time, promote the function development in parallel to improve the development efficiency 2. During the development of each branch, if the development of one branch fails, it will not affect other branches. The failed branch can be deleted and restarted.
3.2 commands related to branch operation
(1) Create branch
1) Command: git branch Branch name
(2) View branch
1) Command: git branch -v(View all branches)
(3) Switch branch
1) Command: git checkout Branch name
(4) Merge branches (completed in two steps)
1) Command 1: git checkout Main branch name (Switch to main branch) 2) Command 2: git merge Branch name to merge
(5) Resolution of conflicts arising from merging branches
PS; Cause of the problem: the files changed by multiple branches are the same, and the changed location is also the same.
Let's simulate it. For example, I change the main branch to shallow02 Txt file, and then submit it. Then I switch to the share branch to change the share 02 The first line of TXT file is submitted again. At this time, the problem appears, as follows
PS: conflict resolution method
a.At this time, because the two branches change to the same file and the same location. In other words, we need to find another person to modify this place and communicate with him to confirm what this is like b.Then execute the submission again
PS: look again now
4. Basic principle of GIT
4.1 hash of GIT
(1) Hash
1.git The bottom layer adopts SHA-1 algorithm 2.Hash is a series of encryption algorithms. Although different hash algorithms have different encryption strength, they have the following common points: 1)No matter how much data is input, the length of the encryption result obtained by inputting the same hash algorithm is fixed 2)The hash algorithm is determined, the input data is determined, and the output data can be guaranteed to remain unchanged 3)The hash algorithm determines that if the input data changes, the output data must change, and usually changes greatly 4)Hash algorithm is irreversible 3.Hash algorithms can be used to validate files
4.2 git version saving mechanism
(1) File management mechanism of centralized version control tool
1) 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.
(2) git file management mechanism
1) Git Think of data as a set of snapshots of a small file system. Every time an update is submitted Git Will take a snapshot of all the current files and save the index of this snapshot. For efficiency, if the file is not modified, Git Instead of re storing the file, only one link to the previously stored file is retained. therefore Git The working mode of can be called snapshot flow.
5.git and GitHub
5.1 alias GitHub's address
(1) Alias the address of github remote warehouse to facilitate future push and other operations
1. Command: git remote add alias gitHub Remote warehouse address
PS: Demo
5.2 push code to remote library
(1) Using alias to push code to remote warehouse
1.Command: git push Alias branch name
5.3 pull remote library code
(1) Clone remote warehouse code
1. Command: git clone gitHub Remote library address
PS: 1. When this command is used to pull (clone) the code, it will create the origin remote library alias and initialize the local library by default. 2. After pulling the code for the first time, you can use git pull to update the code
git pull is equivalent to git fetch and git merge
6.SSH password free login
6.1 setting SSH public key
(1) Find the account number of the current github warehouse, that is, the email number
(2) Find any folder, open the git command window and enter the command:
1) Command: ssh-keygen -t rsa -C Email account
PS: then go to the folder to find the key file:
PS: open XXXX Pub copy
Then take your name casually, confirm and verify your password
PS: copy the SSH address after the above operation
PS: This SSH address has an alias, which is also convenient for follow-up
7.Eclipse operation git
7.1 initialize the project as a local library
(1) Let's see the operation demonstration directly
PS: find your project
7.2 eclipse staging area interface
(1) Look at the picture to demonstrate
PS: then click Commit to submit;
7.3 configuration file
PS: first build a Java Gitignore file, add the file to be ignored (roughly the same, according to your own requirements)
PS; Then go Add the path of this file (java.gitignore) to the gitconfig file (this file is usually in your c disk user, such as mine: c / user / share / gitconfig)
7.4 some basic operations of git in eclipse
(1) Add files to staging area
PS: after the correct mention, the documents will have a green "+"
(2) Commit staging area to local library
PS: remember to write a log. In addition, instead of going to add, we can directly drag the file to be submitted to the temporary storage area in the Commit interface and then Commit
(3) Push to remote library
PS: you can also click Next to add some log information
Note: because it interacts with github, the network is slow and sometimes an error will be reported. Therefore, if it fails, it can be operated directly
(4) Clone
PS: right click blank and select import
(5) Create a branch (click other... To switch branches)