Quick start of Git & GitHub
Article Directory
- Quick start of Git & GitHub
-
- 1. The concept of Git and version control
- Second, the installation process of Git
- Three, Git's local structure and initializing the local warehouse
- Fourth, the introduction of the code hosting center
- Five, Git commonly used commands
- Six, introduction and use of branches
- Seven, team internal collaboration process simulation
- 8. Cross-team collaboration process simulation
- Nine, SSH password-free login
1. The concept of Git and version control
-
Git is a free, open source distributed version control tool
-
Version control is a tool to record changes in the content of files in order to view or modify specific historical versions
-
Classification of version control tools:
(1) Centralized version control tool
There is a single centralized management server that saves revisions of all files. Staff need to connect to this server to view or modify historical versions. The disadvantage is that if the server fails, all staff cannot submit updates or view, Revision history version, common such as SVN
(2) Distributed version control tool
Each host can save file revisions like a server, and allow other hosts to view and revise historical versions from its own host
-
The centralized version control tool stores the differences of each version of the file, and the rollback version must be a version of a version.
-
The distributed version control tool stores the indexes of all historical versions (less disk space is required), and the rollback version can be rolled back directly through the index
Second, the installation process of Git
-
Go to the official website to download https://git-scm.com/
-
All the next steps in the installation process (use the default)
-
Right-click on the desktop or disk directory and click Git Bash Here to open the Git terminal
Three, Git's local structure and initializing the local warehouse
- Git's local structure
- Initialize the local warehouse
(1) Create a folder GitResp (custom name) on the disk as the local structure of Git
(2) Open the Git terminal
① The commands in Git are the same as Linux
i. View Git version
ii. Clear the screen
iii. Set signature (user name and email)
② Initialization of the local warehouse
i. Enter the folder of the created local warehouse, right click to open the terminal
ii. A .git folder (hidden) will be created under this folder
③ A detailed introduction to the local structure of Git
Fourth, the introduction of the code hosting center
The classification of the interaction between the local library and the remote library:
1. Collaboration within the team
2. Cross-team collaboration
Classification of hosting centers:
(1) In a local area network environment: GitLab server can be built
(2) In the Internet environment: GitHub or Gitee (Code Cloud) can be used
Five, Git commonly used commands
- add and commit commands
Used to add files to the cache and submit to the local library
(1) Create a file Demo.txt in the workspace
(2) Add files in the work area to the temporary storage area
(3) Submit the contents of the temporary storage area to the local library
-m "xxx" Add comments to files submitted to the local library
(4) Note
The red box means this file is not in the workspace
i. Files that are not placed in the workspace are not managed by git
ii. Even the files in the workspace are not managed by git, and can be managed only through the add and commit commands
- status command
View the status information
(1) Create a file Demo2.txt and check the status
(2) Execute the add command on Demo2.txt and add it to the temporary storage area to check the status
(3) Execute the commit command on Demo2.txt and submit it to the local library to check the status
i. 红框表示暂存区无文件,已经全部提交到本地库
ii. 对文件内容修改之后需要重新执行add、commit命令将其提交到本地库
- log command
(1) git log
Display the log information from the nearest to the farthest
Blank space: next page
b: previous page
q: exit
(2) git log --pretty=oneline
(3) git log --oneline
(4) git reflog
HEAD@{X} means X steps are required to roll back to this version
- reset command
Forward or rewind historical version
Note: Select it in the terminal to copy, right-click and select paste to paste
- hard\mixed\soft parameters
git reset --hard [index number]: When the version of the local library is modified, the version of the temporary storage area and the work area are modified accordingly
git reset --mixed [index number]: When the version of the local library is modified, the temporary storage area is modified accordingly, but the work area is not modified
git reset --soft [index number]: When the version of the local library is modified, neither the temporary storage area nor the work area is modified
- Delete files/recover deleted files
(1) Create a Test2.txt file and execute the add and commit commands on it
(2) Delete the Test2.txt file in the workspace
(3) Synchronize the delete operation to the temporary storage area and local library
(4) View log
It was found that the file was not physically deleted in the local library, but a deleted record was added, and the deleted file can be retrieved according to the index number
(5) Retrieve deleted files through index
- diff command
Compare the difference between the content of a certain file workspace and temporary storage area
(1) Create a file Test3.txt with content aaaa, and execute the add and commit commands on it
(2) Change the content of the Test3.txt file in the workspace, add bbb, and the content becomes aaaabbb, resulting in inconsistent content in the workspace and temporary storage area
(3) Execute the git diff Test3.txt command on this file
(4) git diff means to compare the differences between all files in the work area and the temporary storage area
(5) git diff [index number] / [HEAD] [file name] means to compare a version of a file in the work area and the temporary storage area
Six, introduction and use of branches
1. Introduction to the branch
In the version control process, multiple branches are used to advance multiple tasks at the same time, as shown in the figure:
Benefits of branching:
(1) Multiple branches can be developed concurrently without affecting each other, improving development efficiency
(2) If the function development of a branch fails, just delete this branch directly, it will not affect any other branches
2. Operation branch
(1) View, create, and switch branches
i. Create a file Test4.txt in the workspace, the content is abc, and execute the add and commit commands on it
ii. View branches (currently there is only one master branch master)
iii. Create a branch
iv. Switch branches
The orange box in the above figure shows that the new branch will inherit the file version before the main branch, not a blank branch
(2) Solve the conflict problem of the branch
i. Enter the branch01 branch, add the following content to the Test4.txt file, and execute the add and commit commands
ii. Switch the branch to master, check the branch situation, and find that the content of the master branch has not changed with branch01
At this time, open the Test4.txt file and find that there are only three characters abc, that is, there is no new content
iii. Add content to Test4.txt on the premise of the master branch, and execute the add and commit commands
At this time, the contents of the same file in the main branch and branch01 branch conflict
iv. At this time, you need to merge the branch01 branch to the main branch
v. Execute git merge branch01 after switching to the master branch
vi. Now view the contents of the Test4.txt file
vii. At this time, you need to delete and modify this file, decide what to leave, and then execute the add command
viii. Execute the commit command on this file to resolve conflicts
Seven, team internal collaboration process simulation
-
Staff A creates a local library and executes add and commit commands on the Demo.txt file in the workspace
-
A Create a remote library on GitHub
(1) Create a remote library
(2) Enter information
(3) Completion status
(4) The address of the remote library is longer, you can alias this address locally
i. Make an alias
ii. View alias
- Push the contents of the local library to the remote library (you need to log in to the GitHub account in the pop-up window)
After the push is successful, check the remote library on GitHub:
- Staff B clones the remote library created by A to the local
The cloning operation can help us complete:
(1) Initialize the local library
(2) Fully clone the contents of the remote library to the local
(3) Automatically create the alias of the remote library (origin)
- B created the Demo3.txt file and needs to push it to the remote library. If you push it directly, an error will occur
- Log in to A's account on GitHub and invite B to join the team
- Log in to employee B’s account on GitHub, enter the invitation link in the address bar of the browser to join the team
-
B Push the Demo3.txt file to the remote library again, success
-
A needs to download the modified content of the remote library to the local
Note: There is a safer way (fetch + merge)
(1) Execute the fetch command on the remote library
After the grab command is executed, only the contents of the remote library are downloaded to the local, but the contents of the workspace are not updated
(2) Before officially updating the content of the workspace, you can go to the remote library to confirm whether the content is correct
(3) After confirming that it is correct, perform the merge operation (the branch should be switched back before the merge)
- How to resolve conflicts during collaborative development
(1) A pushes the file Test.txt to the remote library, B pulls from the remote library to the local, and B modifies Test.txt and pushes to the remote library
(2) After A modifies Test.txt and pushes to the remote library, conflicts occur
(3) In case of conflict, you should pull to the local first, delete and modify the file according to the previous method, and then push to the remote library
Note: After the conflict is resolved, the file name cannot be added when the commit command is executed
8. Cross-team collaboration process simulation
- Employee A of company A copies the address of his remote library
-
Staff B of company B logs in to his GitHub account and enters A’s remote library address in the address bar of the browser
-
B clicks the fork in the figure to copy A's remote library to his own remote library
- Appears after clicking
-
B can clone the content in the remote library to the local, and push to the remote library after modification
-
B Log in to your GitHub, enter your remote library, click
- A Log in to your GitHub account for review
Nine, SSH password-free login
-
Enter the command to enter the home directory of the computer user
-
Execute the command to generate a .ssh directory
After entering the command, press Enter three times to use the default value
-
There are two files in the created .ssh directory
-
Open the file ending in .pub and copy the contents
-
Log in to your GitHub account
- Alias the SSH remote address
The advantage of using SSH: no need to authenticate every time