Quick start of Git and GitHub, a must-see for beginners. Download and install Git. Common instructions for branch use. SSH password-free login. Team internal collaboration. Cross-team collaboration

Quick start of Git & GitHub

1. The concept of Git and version control

  1. Git is a free, open source distributed version control tool

  2. Version control is a tool to record changes in the content of files in order to view or modify specific historical versions

  3. 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

  1. 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.

  2. 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

  1. Go to the official website to download https://git-scm.com/

  2. All the next steps in the installation process (use the default)

  3. Right-click on the desktop or disk directory and click Git Bash Here to open the Git terminal

Insert picture description here

Three, Git's local structure and initializing the local warehouse

  1. Git's local structure

Insert picture description here

  1. 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

Insert picture description here

ii. Clear the screen

Insert picture description here

iii. Set signature (user name and email)

Insert picture description here

② Initialization of the local warehouse

i. Enter the folder of the created local warehouse, right click to open the terminal

Insert picture description here

ii. A .git folder (hidden) will be created under this folder

③ A detailed introduction to the local structure of Git

Insert picture description here

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

Insert picture description here

2. Cross-team collaboration

Insert picture description here

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

  1. 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

Insert picture description here

(3) Submit the contents of the temporary storage area to the local library

-m "xxx" Add comments to files submitted to the local library

Insert picture description here

(4) Note

The red box means this file is not in the workspace

Insert picture description here

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

  1. status command

View the status information

(1) Create a file Demo2.txt and check the status

Insert picture description here

(2) Execute the add command on Demo2.txt and add it to the temporary storage area to check the status

Insert picture description here

(3) Execute the commit command on Demo2.txt and submit it to the local library to check the status

Insert picture description here

		i. 红框表示暂存区无文件,已经全部提交到本地库
		ii. 对文件内容修改之后需要重新执行add、commit命令将其提交到本地库
  1. log command

(1) git log

Display the log information from the nearest to the farthest

Insert picture description here

Blank space: next page
b: previous page
q: exit

(2) git log --pretty=oneline

Insert picture description here

(3) git log --oneline

Insert picture description here

(4) git reflog

HEAD@{X} means X steps are required to roll back to this version

Insert picture description here

  1. reset command

Forward or rewind historical version

Note: Select it in the terminal to copy, right-click and select paste to paste

Insert picture description here

  1. 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

  1. 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

Insert picture description here

(3) Synchronize the delete operation to the temporary storage area and local library

Insert picture description here

(4) View log

Insert picture description here

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

Insert picture description here

  1. 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

Insert picture description here

(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:

Insert picture description here

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)
Insert picture description here

iii. Create a branch

Insert picture description here

iv. Switch branches

Insert picture description here

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

Insert picture description here

ii. Switch the branch to master, check the branch situation, and find that the content of the master branch has not changed with branch01

Insert picture description here

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

Insert picture description here

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

Insert picture description here

vi. Now view the contents of the Test4.txt file

Insert picture description here

vii. At this time, you need to delete and modify this file, decide what to leave, and then execute the add command

Insert picture description here

viii. Execute the commit command on this file to resolve conflicts

Insert picture description here

Seven, team internal collaboration process simulation

  1. Staff A creates a local library and executes add and commit commands on the Demo.txt file in the workspace

  2. A Create a remote library on GitHub

(1) Create a remote library

Insert picture description here

(2) Enter information

Insert picture description here

(3) Completion status

Insert picture description here

(4) The address of the remote library is longer, you can alias this address locally

i. Make an alias

Insert picture description here

ii. View alias

Insert picture description here

  1. 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)

Insert picture description here

After the push is successful, check the remote library on GitHub:

Insert picture description here

  1. Staff B clones the remote library created by A to the local

Insert picture description here

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)

  1. B created the Demo3.txt file and needs to push it to the remote library. If you push it directly, an error will occur

Insert picture description here

  1. Log in to A's account on GitHub and invite B to join the team

Insert picture description here
Insert picture description here

Insert picture description here

  1. Log in to employee B’s account on GitHub, enter the invitation link in the address bar of the browser to join the team

Insert picture description here

  1. B Push the Demo3.txt file to the remote library again, success

  2. A needs to download the modified content of the remote library to the local

Insert picture description here

Note: There is a safer way (fetch + merge)

(1) Execute the fetch command on the remote library

Insert picture description here

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

Insert picture description here

(3) After confirming that it is correct, perform the merge operation (the branch should be switched back before the merge)

Insert picture description here
Insert picture description here

  1. 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

Insert picture description here

(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

  1. Employee A of company A copies the address of his remote library

Insert picture description here

  1. 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

  2. B clicks the fork in the figure to copy A's remote library to his own remote library

Insert picture description here

  1. Appears after clicking

Insert picture description here

  1. B can clone the content in the remote library to the local, and push to the remote library after modification

  2. B Log in to your GitHub, enter your remote library, click

Insert picture description here
Insert picture description here
Insert picture description here

  1. A Log in to your GitHub account for review

Insert picture description here

Insert picture description here

Nine, SSH password-free login

  1. Enter the command to enter the home directory of the computer user
    Insert picture description here

  2. Execute the command to generate a .ssh directory

Insert picture description here

After entering the command, press Enter three times to use the default value

  1. There are two files in the created .ssh directory
    Insert picture description here

  2. Open the file ending in .pub and copy the contents

  3. Log in to your GitHub account

Insert picture description here

Insert picture description here
Insert picture description here

  1. Alias ​​the SSH remote address

Insert picture description here

The advantage of using SSH: no need to authenticate every time

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/110451882