Git learning and Git use

Organized based on the video: A complete set of tutorials from getting started to mastering Git in Silicon Valley (covering GitHub\Gitee Code Cloud\GitLab)
The article is archived at: https://www.yuque.com/u27599042/qy17ty


Introduction to Git

What is Git

  • Git is a free, open source distributed version control system
  • Git provides convenience for multi-person collaboration project code management

version control

  • Version control is to record modification records of files and record changes in file content so that you can check the revision status of a specific version in the future and facilitate viewing and switching of historical versions of files.

Centralized version control

  • There is only a single server in the centralized version control tool for centralized management of file versions. All historical versions of all files are saved in this server. Collaboration workers connect to the server through the client to take out the latest files for modification. File modifications The merges are all performed on this server
  • Advantages: Administrators can easily control each developer's permissions and manage a centralized version control system.
  • Disadvantages: If the server goes down, no one will be able to submit updates, and they will not be able to work together.

distributed version control

  • In distributed version control, there is a code hosting center. The code hosting center (remote library) is responsible for the hosting and management of all files. The remote library is a code library in distributed version control. All developers will Clone the remote library locally. There will be a code library the same as the remote library locally. Each developer will modify the files in his local code library. After the modification is completed, his local library will be pushed to the remote library. In the remote library Library for file hosting and management
  • Distributed version control system versus centralized version control system:
    • Development can also be carried out even when the server is disconnected, because each developer's version control is performed locally. After the modification is completed, it only needs to be pushed and merged with the remote library.
    • Each client also saves the entire complete project, including historical records, which is more secure. When a failure occurs, it can be restored using the local warehouse of other clients afterwards.

Git working mechanism

picture.png

Code hosting center

  • The code hosting center is a remote code warehouse based on the network server. Generally, we simply call it the remote library.
  • Classification of code hosting center (remote library):
    • LAN code hosting center
      • GitLab
    • Internet Code Hosting Center
      • GitHub (extranet)
      • Gitee code cloud (domestic website)

Git download and installation

Git download

Git installation


Git commands

You can use linux commands directly in Git's Bash command line

set user signature

  • The function of user signature is to distinguish the identities of different operators who modified the file.
  • The user's signature information can be seen in the submission information of each file submission history version to confirm who made this submission.
  • After the Git installation is completed, you only need to set the user signature once, and the user signature is required . If the user signature is not set, an error will be reported when submitting the code.
  • Note that the user signature has nothing to do with the account you will use to log in to GitHub (or other code hosting centers) in the future. The purpose of setting the user signature is to confirm who made this submission.
# 设置用户名
git config --global user.name 用户名
# 设置用户邮箱
# 邮箱可以不用真实存在,Git 不会验证邮箱是否真实存在,格式正确即可。
git config --global user.email 邮箱
  • image.png

View user signature

  • Enter the local Windows user home directory to view the user signature:C:/ => 用户 => 当前登录Windows的用户对应的文件夹 => .gitconfig
  • image.png
  • image.png

Initialize local library

  • Initialize the local library, let Git obtain the management rights of the project, and allow Git to manage the project
git init
  • image.png
  • Note that the generated .gitdirectory is hidden by default
    • Windows view hidden directories
    • 5052a833f0b84b16a9e0a461474e8b87.png

View local library status

git status
  • a94652b39bf24295b0c66f72c5988f09.png
  • master is the main branch. By default, there is only one master branch (master/main)
  • Create a new file and view it again
  • 699f1721bc7d4b709dd35112dc9941ca.png

Rename local branch

git branch -m 旧分支名 新分支名
  • image.png

Add local files to the staging area

Add the specified file to the staging area

# 将某个指定文件添加暂存区
git add 文件名

# 将多个指定文件添加暂存区
git add 文件名1 文件名2 文件名3 ...
  • 34e41b63b65e479ba46fe8b628be1567.png
  • Check the status of files after they are added to the staging area
  • a6a1062df609463cbf8fc122b91259d6.png

Add the specified directory and the files under it to the staging area

git add 目录
# 提交当前目录及其下所有被删除、被修改和新增的文件到数据暂存区
git add .
  • Currently, a txt.txt file is created in the a directory, a txt.txt file is also created in the a/b directory, and a txt.txt file is also created in the a/b/c directory.
  • image.png

Add all files to staging area

git add *

  • Submit all modified files and new files to the data staging area, excluding deleted files
  • Add all currently existing files to the staging area
git add *
  • image.png
  • image.png

git add -u & git add --update

  • Submit all deleted files and modified files to the data staging area, excluding newly created files
git add -u
git add --update
  • image.png

git add -A & git add --all

  • Submit all deleted, modified and newly added files to the data staging area
git add -A
git add --all
  • image.png

Add a certain type of file to the staging area

# 添加当前目录下所有 html 文件到暂存区
git add *.html
  • image.png

Submit local files to local library

Submit all contents of the temporary storage area

git commit
  • image.png

Submit the specified file

  • Tracked files can be specified for submission, including workspace and staging area
git commit 文件名
git commit 文件名1 文件名2 ...
  • image.png

Submit the file and specify the submission description information for this time.

  • The above commands will enter the vi command interface and enter the submission description information for this time.
# 提交文件的同时指定本次的提交描述信息
git commit 文件名... -m <本次提交描述信息>
  • image.png

Commit all tracked file changes

git commit -am <本次提交描述信息>

# 相当于执行
# 将已跟踪的被删除和修改的文件提交到暂存区
git add -u
# 将暂存区中的文件提交到本地库
git commit -m <message>
  • image.png
  • image.png

Delete Files

rm

  • The command in Linux to delete the workspace file will not submit the deletion operation to the temporary storage area.
  • The file version record in the local library is still that the file has not been deleted. This deletion operation needs to be added to the temporary storage area and submitted. The current historical version of the file is recorded in the local library as deleted.
rm 文件名
  • image.png

git rm file name [–cache | --force | -f]

  • Delete the workspace file and submit the deletion operation to the temporary storage area
  • If the file currently to be deleted has been modified and no modification operation has been submitted, an error will be reported when executing this command to delete the file.
  • image.png
  • If you need to delete this file at this time, you can submit the modification operation first and then delete the file.
  • Or perform forced deletion of the file. git rm -f 文件名Or git rm -force 文件名, if forced deletion is performed, the modification operations that have not been submitted before will not form the historical version of the file. The modification operations will be overwritten and replaced by the deletion operation. After the forced deletion, the workspace file will be deleted.
  • You can also execute git rm --cache 文件名this to prevent the file from being tracked by Git. The workspace file still exists, and then you can delete the file.
git rm -f 文件名
git rm --force 文件名

# 从缓存中删除该文件
# 使文件不被 Git 追踪
git rm --cache 文件名
  • image.png
  • image.png
  • image.png
  • image.png

View version information

go reflog

  • View condensed version information
git reflog
  • image.png

git log

  • View detailed version information
git log
  • image.png

Version shuttle

  • Reset to specified version
git reset 版本号
  • The state of the first commit, subsequent instances will shuttle back to this version
  • image.png

–hard

  • Will shuttle to the specified version and clear the staging area and workspace
  • Status before shuttle:
  • image.png
  • image.png
  • Since 5.txt is not tracked, that is, it is not managed by Git, it will not be affected in any way, but the workspace and staging area managed by Git will be cleared by Git.
    • The submitted 2.txt 3.txt in the workspace and the 4.txt submitted to the staging area are all cleared.
  • image.png
  • image.png

–soft

  • Will shuttle to the specified version, but no changes will be made to the workspace or staging area. All changes between HEAD (pointer to the current version) and the commit version shuttled to will be submitted to the staging area. , the staging area and work area are not cleared. That is, the difference between the two versions of the shuttle will be submitted to the staging area.
  • Status before shuttle:
  • image.png
  • image.png
  • Status after shuttle:
  • image.png
  • image.png

–mixed

  • This option is the default
  • Will shuttle to the specified version, the workspace will not be changed, so all changes from HEAD (pointer to the current version) to the commit version shuttled to will be saved in the workspace, that is, the staging area will not be cleared , is marked as changed, and the contents of the temporary storage area will be cleared.
  • Status before shuttle:
  • image.png
  • image.png
  • Status after shuttle:
  • image.png
  • image.png

Git branch

Branch overview

  • In the process of project management and version control, in order to enable the project to continue to provide services to users, and at the same time project development can promote multiple tasks, we can create a separate branch for each task, and the main branch is used to continue to provide services to users. , other branches are used for the development of project tasks. Using branches means that the development implementation of each task can be separated from the main development line. When developing each branch, it will not affect the operation of the main line branch.
  • A branch can be understood as a separate copy of the main branch. (The bottom layer of the branch is actually a reference to the pointer)
  • c903967cb77444409925b977bdfcc123.png

Benefits of branching

  1. Promote the development of multiple functions in parallel at the same time to improve development efficiency.
  2. During the development process of each branch, if the development of one branch fails, it will not have any impact on other branches. Just delete the failed branch and start again.

View branches

git branch -v
  • Using this command, you can view all current branches, where the * sign represents the current branch.
  • c268705a0bb84ee2b76967220fe74374.png

Create a branch

git branch 分支名
  • The newly created branch will copy the contents of the master branch to form a copy of the master branch.
  • 32b2553b15574a97b49058e99f2a73cd.png

switch branch

git checkout 分支名
  • b61fd6cb5d0e414685c30fd22d66c16f.png
  • 2eda32a06d47474096c54e1ef08542f0.png

merge branches

git merge 分支名
  • This command is used to merge the specified branch into the current branch
  • If you want to merge other branches into the main branch, you need to execute the merge branch command in the main branch

Normal merge

  • The hot-fix branch has modified the code, but the master branch has not modified the code. At this time, merging the hot-fix branch into the matser branch is a normal merge.
  • Normal merge, that is, the modifications in the branch to be merged into the current branch do not conflict with the modifications in the current branch.
  • 2e1c1a3e9ffd45f48da5382bcc9d0513.png

conflict merge

  • When merging branches, the two branches have two completely different sets of modifications at the same location in the same file. That is, conflicts are sent when merging branches. Git cannot decide for us which branch to use for merging the modifications. It must be decided manually. Which branch should the changes be merged into.
  • Modify and commit in the master branch:
  • d80bfae49e9c4abfbd1884741e3e338c.png
  • Modify and submit the hot-fix branch:
  • df70383da40c4b90a17c34e79750a142.png
  • Merge branch:
  • 9e0a2a151c2a4c018cd593ea34758b90.png
  • Manually merge branches:
  • b1c297b5ce2b4b2bb703866154fb569c.png
  • Manually merge files, manually open and edit files, manually merge file modifications, and manually select which branch's modifications need to be retained in conflicting parts.
  • 088cf98d6cce4d08a7d19ce7b573bdec.png
  • c0a241f3c59b4f9a835ca033f4231b02.png
  • After saving the modifications, you need to add the manually conflict-merged files to the staging area, and then submit the modifications:
    • Note: You cannot bring the file name when using the git commit command at this time, because both branches have modified the same file. If you bring the file name, an error will be reported. Git does not know which branch to submit the modifications.
    • Merge the contents of other branches into the current branch. The contents of other branches will not be modified. They are still the contents before the merger. Only the current branch is modified (merge the contents of other branches into the current branch).
  • 5f194ce6195548d4900b77d743eebd7a.png
  • 39cee86692284ccabc025eb9ce32a131.png

View the current branch in the .git file

image.png
image.png

View the version of the current branch in the .git file

  • View based on the directory in the HEAD file
  • image.png
  • image.png
  • image.png

Git remote warehouse operation (GitHub)

GitHub creates a remote repository


The warehouse is created:

Remote warehouse operations

Command name effect
git remote -v View all current remote address aliases
git remote add alias remote address Give an alias
git push alias branch Push content on the local branch to the remote repository
git clone remote address Clone the contents of the remote repository to the local
git pull remote library address alias remote branch name Pull down the latest content of the branch from the remote warehouse and merge it directly with the current local branch.

View all current local remote warehouse address aliases

git remote -v

Create an alias for the remote warehouse address

After creating an alias for the remote warehouse, you can use the alias directly for subsequent operations of pulling code from the remote warehouse and pushing code to the remote warehouse.

git remote add 别名 远程地址

Push the local library to the remote warehouse

When pushing a local warehouse to a remote warehouse, you need to specify the link to the remote warehouse or a local alias, indicate which remote warehouse you want to push to, and the branch of the remote warehouse you want to push to.

git push 别名或远程仓库链接 分支





Pull the remote library to the local library

When pulling the remote library code to the local, you need to specify the remote warehouse to be pulled and which branch of the remote warehouse to be pulled.

git pull 远程库地址别名或链接 远程分支名

Pull the remote library to the local library:

Clone the remote warehouse to the local

When cloning a remote warehouse to a local location, you do not need to log in to an account. You can just use the address of the remote warehouse to clone to a local location.

git clone 远程地址

Clone the remote warehouse demo1 to the local E://git-clone


Clone will do the following operations:
1. Pull the code
2. Initialize the local warehouse
3. Automatically create an alias for the cloned remote warehouse (default is origin)

Invite to join the team





Then send the invitation letter to the invited person. After logging in to the github account, the invited person opens the invitation link and agrees to the invitation.

After agreeing, join the collaboration

The invitee can then push the code to the remote repository.

Windows Credential Manager


GitHub credentials saved in Windows credentials, used when Git pushes and pulls from the local warehouse to the remote warehouse

ssh password-free login

Enter the home directory of the current user on the computer's Windows system and delete the .ssh directory.

Open the Git bash command line window and run the command to regenerate the .ssh key directory

# 使用如下命令生成 ssh 密钥
# -t 指明使用的加密算法为 rsa
# -C 指明当前生成的密钥是针对于哪个Github账号
ssh-keygen -t rsa -C 创建Github账号使用的邮箱

image.png


View and copy the public key, and pair the GitHub account with the current host using the public key configured on the Github account and the matching private key you currently have.




When the current computer client uses SSH connection to push and pull things to the remote warehouse, there is no need to log in.


File status in github repository

Pull the remote repository to local


Guess you like

Origin blog.csdn.net/m0_53022813/article/details/132449343