Version control tool--git

What is version control

Revision control helps us in the development process to achieve cross-regional multi-person collaborative development, track and record the historical submission records of the project, host the project, count the workload, record the entire development process of the software, etc.

Common version control tools

  • git
  • svn
  • cvs
  • vss
  • tfs
  • Visual Studio Online

Centralized version control SVN CVS

All version data is stored on the server, and the developer can obtain data from the server or upload the data modified by himself.
Disadvantages:

  • Users cannot work if they are not connected to the Internet
  • All data is stored on a single server, there is a great risk that this server will be damaged, so that all data will be lost.

Insert picture description here

Distributed version control Git

All version information is synchronized to each user locally, so that all version history can be viewed locally, and can be submitted locally offline, just push to the corresponding server or other users when connecting to the Internet, because each user saves it All version data, as long as there is no problem with a user's device, all data can be restored.
Disadvantages:

  • Everyone has all the code! Security risks!
  • Increase the occupation of local storage space.

advantage:

  • It will not be unable to work due to server damage or network problems!

Insert picture description here

The main difference between Git and SVN (emphasis)

SVN is a centralized version control system. The version library is centralized on the central server. When you work, you use your own computer. Therefore, you must first obtain the latest version from the central server, and then work. After the work is completed, you need Push the work done by yourself to the central server. The centralized version control system must be connected to the Internet to work, and requires high network bandwidth; if the server is damaged, all data will be lost.

Git is a distributed version control system. There is no central server. Everyone's computer is a complete version library. You don't need to be connected to the Internet when you work, because the versions are all on your own computer. The method of collaboration is as follows: For example, Zhang San changed file A on the computer, and other people also changed file A on the computer. At this time, the two of you only need to push your changes to each other, and you can each other. See the other party's modification. Git can directly see which code and files have been updated!

Git is currently the most advanced distributed version control system in the world.

git installation and environment configuration

Fool-style installation...

Start git

  • Git Bash: Unix and Linux style command line,使用最多,推荐最多
  • Git CMD: Windows style command line
  • Git GUI: Git with graphical interface, not recommended for beginners, try to be familiar with common commands first

git configuration

View configuration git config -l
Insert picture description hereView System Configuration git config --system --list
Insert picture description hereview the current user (global) configurationgit config --global --list
Insert picture description here

Git-related configuration files

  • Git\etc\gitconfig: gitconfig --system system level in the Git installation directory
  • C:\Users\Administrator\ .gitconfig only applies to the configuration of the currently logged in user --global
    Insert picture description here

Here you can directly edit the configuration file or set it through commands and it will respond here.

Set user name and mailbox (user ID, necessary)

The first thing you need to do after installing Git is to set your username and e-mail address. This is very important because every Git commit will use this information. It is permanently embedded in your submission:

git config --global user.name "coiggahou"              //绿色部分为GitHub用户名
git config --global user.email [email protected]   //这里用GitHub的注册邮箱

How Git works

Insert picture description here

  • Work area : It is the place where the code is usually stored, such as: a project in the idea project, etc.
  • Suspended area : an area for temporarily storing data, in fact a file
  • Repository : The repository is in the .git directory
  • Remote warehouse : the server hosting the code, such as github, gitee, etc.
    Insert picture description here

Local warehouse construction

There are two ways to create a local warehouse: one is to create a brand new warehouse, and the other is to clone a remote warehouse.

#   在当前目录新建一个Git代码库
$ git init

After execution, you can see that there is only an extra .git directory in the project directory, and all the information about the version is in this directory.

Remote warehouse clone

Go to gitee or github to clone a test!

# 克隆一个项目和它的整个代码历史(版本信息)
$ git clone  https://gitee.com/kuangstudy/openclass.git   #url

After execution, you can see that there is an extra directory for the cloned project, and all the information about the version is in this directory.
Insert picture description here

Common git commands

git init              #创建本地仓库
git status            #查看文件状态 
git add .             #将文件提交到暂缓区中 
git commit -m "注释如:first commit"   #文件提交到版本库
git push origin dev      #提交到远程仓库dev分支
git pull origin dev      #将远程仓库dev分支拉取到本地仓库
git remote add origin https://github.com/yourName/repositoryname.git     #连接到远程仓库
git push origin dev -f   #强制推送
git log    #查看提交日志

Insert picture description here

Ignore files

*.txt    #忽略.txt结尾的文件
!lib.txt   #忽略除开lib.txt之外的文件
/temp    #忽略temp 目录上的文件
temp/   #忽略temp目录下的文件
doc/*.txt    #忽略doc目录下的txt结尾的文件


Use Code Cloud

1. Register the login code cloud, improve personal information
2. Set the machine to bind the SSH public key to realize password-free login!

# 进入 C:\Users\Administrator\.ssh 目录
# 生成公钥
ssh-keygen -t rsa

Insert picture description here3. Add the public key information public key to the code cloud account!
Insert picture description here4. Use Code Cloud to create your own warehouse!

Integrate git in idea

1. Set the git.exe installation path in idea
Insert picture description here2. Initialization of the
Insert picture description herelocal library The initialization of the local library is over.
Insert picture description here3. Submit the file to the temporary storage area
Insert picture description here4. Submit the file to the repository
Insert picture description here

Common commands for git branches

git branch       	  #查看本地仓库所有分支
git branch -r   	  #查看远程仓库所有分支
git branch dev        #新建一个分支
git checkout dev      #切换到指定分支
git merge dev         #将指定分支dev与当前分支合并
git checkout -b dev    #新建一个分支并切换到该分支
git branch -d dev      #删除指定分支
git push origin :dev   #删除远程仓库指定分支

There may be conflicts in git pull origin dev?

If the b.txt file of the remote warehouse is modified, and the same file is modified in the local warehouse and submitted to the remote, conflicts will occur.
Insert picture description hereInsert picture description here

So how to resolve the conflict?

After we open b.txt and modify the file again, of
Insert picture description herecourse, we can resubmit it. For the same reason, we still have the same solution when we encounter conflicts during the merge.

What is the difference between git reset --hard and --soft? (Key to master)

You can view the commit record of the current branch through git log. When we want to roll back to a certain version, we can use the git reset command, and then use the git push origin dev -f command to force the current branch to be pushed to the remote warehouse.

  • -Hard By rolling back the head to the specified version, the version before the specified version will be lost, including the work area and the suspension area.

HEAD points to our latest version number. At this time, we suddenly found that there is a problem with version 3 and version 4 and we need to roll back to version 2. Then we can execute git reset --hard version number 2, and the effect will be effective after the execution is over. The schematic diagram is as follows:
Insert picture description hereInsert picture description here

  • --Soft By rolling back the head to the specified version, the version before the specified version will not be lost in the suspended area, and it is often used to merge multiple commit_ids.

After reset, we can see that the latest two versions do not exist, and the current HEAD is at the new version number.
If we regret now and want to reset and go back, then we can view all version numbers (including the version number that was just killed) through git reflog, to ensure that the data saved by git will not be lost, and then reset again to go back. As shown below :
Insert picture description here

What is the difference between git reset and git revert? (grasp)

git reset completes the version rollback by rolling back HEAD. Git revert generates a new commit. The new commit is just the opposite of the content of the commit you want to remove. It is essentially a new commit, that is, HEAD continues to move forward.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44421869/article/details/114133607