Follow the right people and do the right thing. Get Git in an hour

Table of contents

Version control (to understand)

 GIT history (not important)

GIT environment configuration (not important)

Git basic theory (important)

 Git project construction

Create a working directory and common commands

​Edit local warehouse build

Create a new warehouse

Clone the remote repository

Git file manipulation

The four states of the file

View file status

ignore file

Use code cloud Gitee

Integrating Git in IDEA

Description: GIT branch


 

 

 

 

 

5 Basic Instructions to Solve Initialization and Backup Code

 

 

 

 

 

 

 In addition, Git has other functions. For example, when multiple people are developing, it is necessary to set user permissions and create different branches to avoid content conflicts caused by multiple people editing. For another example, when a code conflict cannot be submitted, you can use code rollback, temporary storage and other instructions to deal with it according to the specific situation. There are more pits here. Specific Git instructions, Baidu has

 


 

Simple Git, easy to learn!

Before learning git, we need to understand a concept - version control!

Version control (to understand)

Version control ( Revision control) is a software engineering technology used to manage our modification history of files, directories, or projectsduring the development process

  • Realize multi-person collaborative development across regions

  • Track and record the history of one or more files

  • Organize and protect your source code and documentation

  • Statistical workload

  • Parallel development, improve development efficiency

  • Track and record the entire software development process

  • Reduces developer burden, saves time, and reduces human error

Simply put, it is a technology used to manage multi-person collaborative development projects.

Without version control or version control itself lacks correct process management, many problems will be introduced in the software development process, such as consistency of software code, redundancy of software content, transactional nature of software process, concurrency in software development process security, software source code security, and software integration.

Common version control tools

What we learn must be the most popular at the moment!

If you know Git, SVN is easy to learn, and even many commands are similar.

The mainstream version controllers are as follows:

  • Git

  • SVN(Subversion)

  • other

Version Control Classification

1. Local version control

Suitable for personal use, each update of the record file, you can make a record file for each version, such as RCS.

2. Centralized version control SVN

All version data are stored on the server, and co-developers can synchronize updates from the server or upload their own modifications

All version data is stored on the server, and the user's local only has the previously synchronized version. If the user is not connected to the Internet, the user cannot see the historical version, and cannot switch version verification issues, or work in different branches. Moreover, all data is stored on a single server, and there is a great risk that this server will be damaged, so that all data will be lost. Of course, it can be backed up regularly. Representative product: SVN

3. Distributed version control Git

Everyone owns all the code! Disadvantages: Potential safety hazards, employees who have all the codes may run away!

All version information warehouses are synchronized to each local user, so that all version history can be viewed locally, offline and local submission can be made, and only need to be pushed to the corresponding server or other users when connected to the Internet. Since each user saves all version data, all data can be restored as long as there is no problem with the user's device, but this increases the occupation of local storage space.

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

The main difference between Git and SVN (important)

SVN is a centralized version control system. The version library is centralized on the central server. When working, you use your own computer, so you must first get the latest version from the central server, and then work. After completing the work, you need Push the finished work to the central server. The centralized version control system must be connected to the Internet to work, and has high requirements for network bandwidth.

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 connect to the Internet when you work, because the versions are all on your own computer. The method of collaboration is as follows: for example, you change file A on your computer, and other people also change file A on your computer. At this time, the two of you only need to push your changes to each other, and you can see each other. To 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 history (not important)

GIT environment configuration (not important)

Configuring environment variables is only for global use. After configuring environment variables, you can use Git anywhere. It doesn't matter whether the environment variable is configured or not, because it has been automatically configured during installation.

Git basic theory (important)

Git theory is the core of Git. After learning the theory, you can learn some Git operations .

three areas

Git has three local working areas: working directory (Working Directory), staging area (Stage/Index), resource library (Repository or Git Directory). If the remote git warehouse (Remote Directory) is added, it can be divided into four work areas. The conversion relationship of files between these four areas is as follows:

  • Workspace: The workspace is where you usually store the project code

  • Index / Stage: Temporary storage area , used to temporarily store your changes, in fact it is just a file, save the information about to be submitted to the file list

  • Repository: The warehouse area (or local warehouse ) is the place where data is safely stored. It contains the data you have submitted to all versions. Where HEAD points to the latest version put into the warehouse

  • Remote: The remote warehouse , the server hosting the code, can be simply considered as a computer in your project team for remote data exchange such as: GitHub

We only need to manage the workspace and the remote warehouse, and the other two only need to be operated with instructions.

The three local areas should be exactly the version pointed to by HEAD in the git repository:

  • Directory: A directory managed by Git, that is, a warehouse, including our workspace and Git's management space.

  • WorkSpace: The directories and files that need to be version controlled by Git, these directories and files make up the workspace.

  • .git: The directory for storing Git management information, which is automatically created when the warehouse is initialized.

  • Index/Stage: The staging area, or the update area to be submitted, we can put all the updates in the staging area before submitting them into the repo.

  • Local Repo: Local warehouse, a repository stored locally; HEAD will only be the current development branch (branch).

  • Stash: Hidden, it is a working state saving stack, which is used to save/restore the temporary state in WorkSpace.

work process

The workflow of git generally looks like this:

1. Add and modify files in the working directory;

2. Put the files that need version management into the temporary storage area;

3. Submit the files in the temporary storage area to the git warehouse.

Therefore, the files managed by git have three states: modified (modified), staged (staged), and submitted (committed)

 Git project construction

Create a working directory and common commands

The working directory (WorkSpace) is generally the folder you want Git to help you manage. It can be the directory of your project or an empty directory. It is recommended not to have Chinese.

For daily use, just remember the 6 commands below! ! !

Build a local warehouse

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 .

Create a new warehouse

1. To create a new warehouse, you need to use the root directory of the project managed by GIT to execute:


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

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

Clone the remote repository

1. Another way is to clone the remote directory, because the warehouse on the remote server is completely mirrored to the local!

(Clone is to copy the contents of the remote warehouse to the local)

2. Go to gitee or github to clone a test!

Git file manipulation

The four states of the file

Version control is the version control of the file. To modify and submit the file, you must first know the current state of the file. Otherwise, you may submit a file that you don’t want to submit now, or the file you want to submit has not been submitted.

It doesn't matter if you don't understand the following paragraph for the time being, after a while, you will understand it after practicing a few times.

Some simple knowledge is not for you to learn in one go!

  • Untracked: Untracked, this file is in the folder, but it has not been added to the git library, and does not participate in version control. After the status becomes Staged (temporary storage) through git add, this file becomes tracked.

  • Unmodify: The file has been put into the library and has not been modified, that is, the content of the file snapshot in the version library is exactly the same as that in the folder. There are two places for this type of file. If it is modified, it becomes Modified. If you use git rm to move out Repository, it becomes an Untracked file

  • Modified: The file has been modified, only modified, and no other operations have been performed. This file also has two destinations. It can enter the temporary staged state through git add. Use git checkout to discard the modification and return to the unmodify state. This git checkout That is, take the file out of the library and overwrite the current modification!

  • Staged: Temporary storage state. Execute git commit to synchronize the modification to the library. At this time, the files in the library and the local files become consistent again, and the file is in the Unmodify state. Execute git reset HEAD filename to cancel the temporary storage, and the file status is Modified

View file status

The above said that the file has 4 states, and the state of the file can be viewed through the following command:


#查看指定文件状态
git status [filename]

#查看所有文件状态,即查看有没有被提交进去
git status

# git add .                  添加所有文件到暂存区
# git commit -m "消息内容"    提交暂存区中的内容到本地仓库 -m 提交信息

ignore file

Sometimes we don't want to put certain files into version control, such as database files, temporary files, design files, etc.

Create a ".gitignore" file in the main directory. This file has the following rules:

  1. Empty lines in the ignore file or lines beginning with a pound sign (#) will be ignored.

  2. Linux wildcards can be used. For example: an asterisk (*) represents any number of characters, a question mark (?) represents a character, square brackets ([abc]) represent an optional range of characters, and braces ({string1,string2,...}) represent optional string etc.

  3. If there is an exclamation point (!) at the beginning of the name, it indicates an exception to the rule and will not be ignored.

  4. If the name is preceded by a path separator (/), it means that the files to be ignored are in this directory, and the files in subdirectories are not ignored.

  5. If there is a path separator (/) at the end of the name, it means that the subdirectory of this name under this directory is to be ignored, not the file (the default file or directory is ignored).


#为注释
*.txt        #忽略所有 .txt结尾的文件,这样的话上传就不会被选中!
!lib.txt     #但lib.txt除外
/temp        #仅忽略项目根目录下的TODO文件,不包括其它目录temp
build/       #忽略build/目录下的所有文件
doc/*.txt    #会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

Use code cloud Gitee

GitHub has a wall and is relatively slow. In China, we generally use gitee. Sometimes the company builds its own gitlab server

This can actually be used as an important information for everyone looking for a job in the future!

1. Register with the login code cloud and improve your personal information

2. Set the local machine to bind the SSH public key to realize password-free login! (Password-free login, this step is very important, Code Cloud is a remote warehouse, we usually work in a local warehouse!)

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

 3. Add the public key information public key to the code cloud account!

 4. Use Code Cloud to create your own warehouse!

License: Whether open source can be reproduced at will, open source but not for commercial use, cannot be reproduced, ... restrictions!

Clone locally!

Integrating Git in IDEA

1. Create a new project and bind git.

Pay attention to the changes in the idea

2. Modify the file and use IDEA to operate git.

  • Add to staging area

  • commit submit

  • push to remote warehouse

3. Submit the test

These are all single-person operations!

The way of learning is the most important! Learn to learn! Most of the time in my class, I am teaching everyone to learn a concept and thought (learning style)

There is no skill in the right way, and the skill can still be sought. Skills have no way, stop at skills!

The real teaching, teach a man how to fish!

Description: GIT branch

Branching is relatively difficult in GIT. Branching is the parallel universe in science fiction movies. If the two parallel universes do not interfere with each other, it will have no effect on you now. However, at some point, the two parallel universes merged, and we had something to deal with!

Common instructions in git branches:


# 列出所有本地分支
git branch

# 列出所有远程分支
git branch -r

# 新建一个分支,但依然停留在当前分支
git branch [branch-name]

# 新建一个分支,并切换到该分支
git checkout -b [branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

Operation in IDEA

If the same file is modified when merging branches, it will cause conflicts: the solution is that we can modify the conflicting files and resubmit! Choose to keep his code or yours!

The master main branch should be very stable and used to release new versions. In general, it is not allowed to work on it. In general, work is done on the newly created dev branch. It can be merged into the main branch master.

Homework practice: Find a small partner and build a remote warehouse together to practice Git!

1. Don't think that Git is difficult, and you will learn how to use it when you practice more at work!

2. There is also a lot of Git learning. After reading my Git tutorial, you can think more and summarize it in your own blog!

Supporting video link: 10. Registration and use of Code Cloud_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/Arvin_ing/article/details/127464826