Home > Article > Development Tools > In-depth analysis of the basic working principles of Git
This article brings you relevant knowledge about Git, which mainly introduces the basic working principles of git, entry-level tutorials, and helps novices quickly get started with Git by playing with Git local warehouses. I hope everyone has to help.
## Recommended study: "Git Learning Tutorial"
Workspace The workspace (Workspace) is the directory where the local code is located, and it is also the directory where .git/ (local warehouse) is stored.
Staging area The staging area (Index/Stage) is the cache space of the workspace and the local warehouse, which records the information about to be submitted to the local warehouse. (Version library) file modification information, the index file in the .git/ directory is the temporary storage area.
Local warehouse The local warehouse (Repository) is also called the local library or version library, which stores all local versions (commit submission records). The local warehouse The files are in the .git/ directory.
Remote warehouse Remote warehouse (Remote) On the network, GitHub, Gitee and GitLab can create remote warehouses. Like local warehouses, remote warehouses Different code versions are also stored, but these versions can come from multiple local repositories.
---------------------- --------------------------------The picture below comes from the Internet----------------- ----------------------------------------
Enter the workspace (code storage directory), enter git init, git will create a new .git/ directory in the workspace:
In a newly created .git/ directory, there are three text files:
[Note] Never manually change the files in .git/, as it may damage the structure of the local warehouse and cause adverse consequences.
3. User information configuration1. Introduction to user name and email configurationThe first thing after initializing the local warehouse is to add user configuration information to the local warehouse, including user Name and email address,The username and email address here have no direct relationship with the account of the hosting platform (such as gitee). Its only function is to let other users or the hosting platform know the uploader information of the code. The email address is messed up. Filling it out will not prevent you from being able to upload the code.
For example, I uploaded the code using "Zhang San" and "Li Si" below. The email address is not real, but the upload can still be successful: (There was an accident in the commit information)
Li Four’s email address is obviously not real.
Since the email addresses of "Zhang San" and "Li Si" are not bound to gitee accounts, when you click on their avatars, the user information will not be displayed. If the gitee account submission is filled in the user configuration Email, you can check the uploader's account information on gitee.
The submission email address of the gitee user can be set and viewed in gitee->Personal Homepage->Personal Settings->Mailbox Management:
I have talked about so many things to note about user names and email addresses. In fact, user information configuration is very simple:
git config --global user.name "your name"git config --global user.email "your email"
user.name followed by user name, fill in whatever you want.
user.email is followed by the user’s email address, fill it in arbitrarily.
–global is used to configure global properties
When submitting code to the local warehouse, git will first retrieve the .git/config file of the local warehouse. If there is no user information, the global configuration file will be used. (In line with the principle of proximity).
The global configuration file is stored in the system user directory/username/.gitconfig, which only contains the user attribute.
When –global is not added, only the user configuration of the local warehouse is set. The local warehouse user configuration information is stored in: .git/config
The picture above is mine Only the user name of the local configuration is set. At this time, if the modification is submitted to the local warehouse, the user name in the submission record (below) uses the local configuration. Because the user mailbox is not configured locally, the mailbox still uses the mailbox specified in the global configuration.
After configuring the user information, we can start to consider submitting the code, but sometimes, we don’t want to think about the entire work All files in the area are submitted to the local warehouse (version library). The existence of the temporary storage area (index/stage) solves this problem for us. We can add the code file to the temporary storage area first. If we feel that it needs to be changed, we can delete the file from the temporary storage area until we feel that the file is selected. When you're almost done, proceed to the next step (submit to the local warehouse).
Before managing the staging area, we also need to know several statuses of the workspace files:
---------------------------------------- ----------------The picture below comes from the Internet--------------------------------- --------------------------
The git status command can be used to view the current status of the workspace files Status:
#查看特定文件的状态 git status [filename]#查看所有文件状态 git status #精简的方式显示文件状态 git status -s
First check the status of all files in the workspace and find that there is no file:
现在创建3个文件,再次使用git status(-s表示精简显示),3个文件的状态为Untracked,??为精简显示下Untracked的标志,意思是新创的文件没有被本地仓库(版本库)跟踪。
git add [文件…] 可以添加一个或多个文件到暂存区,使文件状态变为Staged,A表示该文件被add到暂存区。
也可以使用 git add . 或 git add -A 将工作区所有文件添加到暂存库(除了.gitignore里声明的文件,本文暂不介绍)。
既然可以向暂存区添加文件,那么反向操作必然也不能少,git rm --cached [文件…] 命令可以将暂存区的文件移除,使其恢复到Untracked状态。
如果已经存入暂存区,但在文件提交到本地仓库前,我们对其进行了修改,那么它的状态将变为Modified。
对于Modified状态的文件,我们可以使用git add将修改后的版本加入到暂存区,也可以使用git checkout -- [file...]
将工作区的该文件恢复到暂存区的版本。
git add a.c重新添加a.c到暂存区:
下图为使用git checkout -- a.c
从暂存区恢复a.c文件,下图中我没有加 “ --”,它的作用是让checkout不检测任何其他选项参数,目的是防止该命令把a.c当做一个分支(checkout 还有一个作用是切换分支)。
对Modified状态下的文件使用 git diff 可以得出文件修改的详细记录,git diff和diff命令虽然作用都是对比文件,但git diff的作用是对比不同的状态下的同一文件,而diff用来对比两个不同的文件。
文件添加到暂存区的目的就是将其提交到本地仓库(版本库),提交命令为git commit -m “message”
我们可以在commit 后面添加文件,这样能指定提交的文件:
通过git log 可以查看提交记录,HEAD为本地仓库当前分支,指向主分支master:
直接使用 git commit -m “message” 可以将整个暂存区都提交到本地仓库:
#以一行的形式显示所有提交版本: git log --pretty=oneline
#一行显示,只显示哈希值的前7位: git log --oneline
#显示历史提交版本与当前版本的间隔数: git reflog
代码文件提交到本地仓库后,还需要推送到远程仓库进行托管。
我以码云为例,远程仓库的创建可以通过以下三步实现:
git remote add
如果要用https的方式上传代码,需要添加远程仓库https地址;用ssh上传代码,则url填远程仓库ssh地址。
First copy the warehouse address from the code cloud warehouse homepage:
I named the local alias of the remote warehouse origin:
The remote warehouse can Set multiple ones, as long as the local aliases do not conflict.
The git remote -v command can check the remote warehouse address, or you can check it through git config -l
git remote remove
First confirm the alias of the remote warehouse. The alias corresponding to the https protocol address of the remote warehouse I currently set is https, and the alias corresponding to the ssh protocol address is ssh. These two addresses are actually It's the same warehouse, but the protocol is different.
git push name can upload the local warehouse. name is the alias of the remote warehouse in the configuration file. Use https to upload. , you need to enter your account and password to complete the upload. The Window system will automatically save the account and password. If you want to modify the user name and password that Window has saved, you can refer to Modifying Gitee Login Credentials.
Sometimes the first upload will be unsuccessful. You can try to use git push -u name master. The function of this command is to use the main branch of the name warehouse as the upstream branch. -u and –set-upstream The effect is the same.
If you want to use ssh to upload, you need to generate an SSH key first and The public key is saved to the SSH public key setting of gitee's personal settings. For the specific process, please refer to: Generate SSH key and implement code upload
Sometimes the first upload will be unsuccessful, you can try to use git push -u name master, the function of this command is to use the main branch of the name warehouse as the upstream branch. -u and –set-upstream have the same effect.
Git cloning can copy the remote warehouse to the local one and automatically initialize the local warehouse at the same time.
Open Git bash in any directory, enter git clone
[Note]: Clone requires a password just like push.
After cloning successfully, enter the warehouse directory, which is exactly the same as the workspace when uploading, and the submission log is also the same.
The function of git fetch is to copy the branch of the remote warehouse to the local warehouse and save the latest version in the FETCH_HEAD branch. After obtaining the remote warehouse branch, you need to manually merge it into the current branch.
Command format: git fetch [
git merge
git pull is similar to git fetch, but git pull will automatically merge the branch of the remote warehouse into the current local branch.
Recommended learning: "Git Video Tutorial"
The above is the detailed content of In-depth analysis of the basic working principles of Git. For more information, please follow other related articles on the PHP Chinese website!