The first half of this article is aimed at zero-based beginners, explaining some git installation and principles. The commands will be used in the "summary" section. The bloggers who come to check the commands can jump directly.
Article Directory
git installation and configuration
Windows:
Download the installation package to install, all the way to next. Note that you select 64/32 bit (the following picture shows the 64-bit installation package).
After the download is complete, right-click on any blank space in the folder window to see two options, Git Bash Here and Git GUI Here.
Configure git below: select Git Bash Here in the above options
git --version #查看版本
git config --global user.name 你的id #配置用户ID
git config --global user.email 你的email #配置用户邮箱
The above configuration, which --global
is a global configuration, if this attribute is not added, it will only work under the current path
After configuring under Windows, exit
press Enter to exit this interface
Linux:
apt-get install git
or
yum install git
The method of configuring git is the same as above:
git --version #查看版本
git config --global user.name 你的id #配置用户ID
git config --global user.email 你的email #配置用户邮箱
Use git
Workspace and Repository
Briefly introduce the process of interaction between git local (workspace, repository) and the server:
Assuming that in a certain project, the code and files we wrote locally are stored in the xx folder (directory), then this folder is our workspace.
We will create a hidden folder .git under the workspace. This folder is called the repository. Of course, this repository only exists locally, and some data is recorded in the way of git.
Each commit/push will detect the difference between the current workspace and git, and then record the difference.
Push files from local to cloud
Work area----add----"temporary storage area—commit—"version library—push—"server
First add the content to the local staging area, but not directly write it into the version library.
Because each submission to the repository is a version of the current project , which is a rollback node, it is generally written to a node or a major modification occurs before submitting and recording a version.
Submission of the command will write the newly added content in the temporary storage area to the local repository, and finally push it to the cloud. (See below for specific orders)
Build a repository
Right-click in the workspace-"git bash here (enter the directory directly under linux, enter the following command), enter the command:
git init
A hidden directory, the version library, will be created in the workspace:
Submit changes to the repository
Use the following command to view the files in the current workspace. That is, which files have not been added to the temporary storage area, and which files have not been submitted to the repository.
git status
As follows: Since it was just created, there is no content in the current version library, and there is a menu.html file in our workspace. Use the above command to check the git status, the following shows that untracked files have menu.html:
Use the following command to add the file to the version Library:
git add 文件名 #添加单个文件
git add . #添加所有文件
Check the git status again, showing that the files that have not yet been submitted have menu.html:
Use the following command to submit the changes in the staging area to the repository:
git commit -m "本次提交的版本说明/注释"
Check the git status again, it shows that there is no update available √
Local submission to the staging area summary
git add #将工作区的改动添加入暂存区
git commit -m "本次版本记录/说明/注释" #将工作区的内容提交到版本库
git status #查看当前git状态,对比当前工作区、版本库的区别
server configuration
For cloud storage, you can choose a server you purchase, or a code hosting platform such as github and gitee. The following demonstrates the creation of warehouses and local projects on github to push to the cloud
Since the github server is abroad, the current user experience is relatively poor, and the blogger here is very slow to load, it is recommended to use domestic gitee (gitee is created and connected in exactly the same way as github, and the interface is Chinese, which is also more friendly to novices)
github/gitee create a warehouse and push to the cloud
-
Create a github account
-
New warehouse
Repository
In this step, you need to fill in the warehouse name, the entire warehouse name that meets the project. In the future, push to the cloud will rely on this warehouse name to identify the cloud storage location
One more thing, if you check Add readme file, you need to have a readme.md in the local library
-
Bind warehouse
git remote add 仓库名 仓库链接
-
Link to the cloud branch,
enter the workspace that needs to be pushed to the cloud, and then execute the followinggit push -u 仓库名 分支名
Need to enter the user name and password of the cloud warehouse to verify. That is, the password of the account just registered.
If you encounter a situation that cannot be pushed, you can use the -f parameter to force the push. But use this parameter with caution, because it will directly cover the warehouse content in the cloud! !
git push -uf 仓库名 分支名
The -u parameter is to establish a link between the local workspace and the warehouse branch. There is no need to add it for the second use.
By the way,
push
you may need to enter the git account and password when using it. If you make a mistake at this time, you can use itCtrl+Backspace
to delete it.
Version rollback
Use the following command to view the version of each submission:
git log #查看当前项目版本的更新日志
git log fileName #查看某文件日志
git log . #查看本目录日志
There is one for each record commit
, and the version id is displayed here.
As you can see, the oldest update is displayed at the bottom. You can also add --pretty=oneline
parameters to make the information look more concise:
the fallback command:
git reset --hard HEAD^ #回退到上一版本
git reset --hard HEAD^^ #回退到当前版本的上两个版本
git reset --hard HEAD~20 #回退到当前版本的前20个版本
git reset --hard 版本id #回退到指定版本id的版本
The version id does not need to be written in full, just write a few letters to distinguish which version it is.
The above command rolls back and changes the file in the current workspace, and the version of the file in the current workspace will change to which version of the file information when it is rolled back.
git reflog #查看所有操作日志
The log
difference between this command is that it reflog
looks at the operation history of all version levels of the project since its establishment, including the rollback operation. But log
only the submission status to the current version will be displayed:
As shown in the figure above, after you have rolled back to the second version, the information of the third version git log
will no longer be visible.
Branch management
The role of branches:
The file is being repaired and is preparing to release the next version. A bug has been found, and this bug needs to be changed. But the current file a has been modified, and there is still a part of the update work that has not been completed. It cannot be modified directly in the current state, nor can it be rolled back to the previous unmodified version.
Branch solution is: Modify the production of a previous file, create a branch addFunction
, only in addFunction
the upper branches of a modification.
When a bug is found in time, it can be from master
a new branch dev
in dev
solving branch in a future bug will dev
push back the master
branches. It does not affect addFunction
the development of new features over there at all.
Similarly, addFunction
after the branch is finished development, push it back master
to complete the addition of new features.
Use branch
git branch #查看目前有几个分支
git branch 分支名 #新建分支
git checkout 分支名 #转到指定分支
git checkout -b 分支名 #创建并转到指定分支
The branch with an asterisk in front of it is the current branch. Different colors will be used to distinguish under windows
git merge 分支名 #将指定分支合并到master中去
git branch -d 分支名 #删除分支
Parts that do not need to be uploaded
There may be some very large directories/files in the project that do not need to be uploaded (such as laravel vendor
, after writing the configuration file, composer install
you can install this directory locally ) and you can use the .gitignore
file to record
The specific writing method This blog is written in great detail and I won’t go into details here.
to sum up
The process of pushing files from the local to the cloud:
Work area----add----"temporary storage area—commit—"version library—push—"server
command:
git add . #更改添加到暂存区
git commit -m "本次版本说明" #提交一个版本到版本库
git push #推送到云端
git status #对比查看当前工作区和版本库之间差异
git log #查看当前版本提交日志
git log fileName #查看名为fileName文件的日志
git log . #查看当前目录的日志
git reset --hard HEAD^ #工作区回退到上一个版本
git reset --hard HEAD^^ #工作区回退到当前版本的上两个版本
git reset --hard HEAD~20 #工作区回退到当前版本的前20个版本
git reset --hard 版本id #回退到指定id的版本
git reflog #查看迄今为止所有版本变动
git branch #查看目前有几个分支
git branch 分支名 #新建分支
git checkout 分支名 #转到指定分支
git checkout -b 分支名 #创建并转到指定分支
git merge 分支名a #将指定分支a合并到当前分支中去
git branch -d 分支名 #删除分支