Home  >  Article  >  Development Tools  >  Thirty minutes to get you started with Git (summary)

Thirty minutes to get you started with Git (summary)

WBOY
WBOYforward
2022-02-25 17:13:032869browse

This article brings you relevant knowledge about getting started with git, including environment configuration, basic theory, project construction, file operations and other related issues. I hope it will be helpful to everyone.

Thirty minutes to get you started with Git (summary)

Recommended study: "Git Getting Started Tutorial"

Version Control

What It is version control (version iteration, new version! Version manager)

Version control (Revision control) is a method used to manage our files, directories or projects during the development process. Modification history, easy to view change history, backup software engineering technology to restore previous versions.

  • Realize cross-regional multi-person collaborative development
  • Track and record the history of one or more files
  • Organize and protect your source code and documents
  • Statistical workload
  • Parallel development, improve development efficiency
  • Track and record the entire software development process
  • Reduce the burden on developers, save time, and reduce thinking Error

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

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

Common version control tools

The mainstream version controllers are as follows:

  • Git
  • SVN( Subversion)
  • CVS (Concurrent Visual System)
  • VSS (Micorosoft Visual SourceSafe)
  • TFS (Team Foundation Server)
  • Visual Studio Online

Version control products (Perforce, Rational ClearCase, RCS (GNU Revision Control System), Serena Dimention, SVK, BitKeeper, Monotone, Bazaar, Mercurial, SourceGear Vault), the most influential and widely used ones now are Git and SVN.

1. Local version control

Record every update of the file. You can make a snapshot of each version, or record patch files, suitable for personal use, such as RCS.

2. Centralized version control SVN

All version data is saved on the server, and collaborative developers can update or upload their own modifications synchronously from the server.

All version data is stored on the server. The user's local only has the version that he has synchronized before. If he is not connected to the Internet, the user cannot see the historical version, nor can he switch version verification issues or work in different branches. . Moreover, all data is stored on a single server. There is a high risk that this server will be damaged and all data will be lost. Of course, it can be backed up regularly. Representative products: SVN, CVS, Vss.

3. Distributed version control GIT

Each branch has all the code.

All version information warehouses are synchronized to each local user, so that all version history can be viewed locally and submitted locally offline, 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, as long as there is no problem with one user's device, all data can be restored, but this increases the occupation of local storage space.

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

4. The main difference between Git and SVN

SVN is a centralized version control system. The version library is centralized on the central server, and when working, They all have their own computers, so they must first get the latest version from the central server, and then work. After completing the work, they need to push last night's work to the central server. A centralized version control system must be connected to the Internet to work, and requires high network bandwidth.

GIT is a distributed version control system without a central server. Each computer is a complete version library. There is no need to connect to the Internet when working, because the versions are all on the computer. The method of collaboration is this: For example, if you change file A on your computer and someone else also changes file A on your computer, then you two only need to push your modifications to each other, and you can see each other. has been modified. 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 environment configuration

Open the Git official website https://git-scm.com and download the version of git corresponding to the operating system.

If everything is slow to download, you can find a mirror!

The official website download is too slow, you can use Taobao mirror download: http://npm.taobao.org/mirrors/git-for-windows/

Start Git

Git Bash: Unix and Linux-style command line, most used and recommended

Git CMD: Windows-style command line

Git GUI: Git with graphical interface, not Recommended for beginners, try to be familiar with common commands

Basic Linux command learning

cd: change directory

cd..Return to the previous page A directory, directly cd to enter the default directory

pwd: Display the current directory path

ls(ll): List all files in the current directory, but the contents listed by ll are followed by For details

touch: Create a new file such as touch index.js and a new index.js file will be created in the current directory

rm: Delete a file

mkdir: Create new A directory is a new folder.

rm-r: delete a folder, rm-r src deletes the src directory

mv moves the file

reset re-initializes the terminal and clears the screen

clear clear screen

historyView command history

help

exit exit

#Indicates comment

Git configuration

All configuration files are actually saved locally

Set user name and email address:

git config --global user.name "Name"

git config --global user.email 22222@qq.com

git config --system --list Query the configuration configured by the system

git config --global --list Query the global configuration

Basic theory of Git

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

  • Workspace: The workspace is where you usually store project code
  • Index/Stage: Temporary storage area: used to temporarily store your changes. In fact, it is just a file to save information about to be submitted to the file list
  • Repository: Warehouse area (or local warehouse), which is to store data safely The location contains the data you submitted to all versions. HEAD points to the latest version put into the warehouse.
  • Remote: Remote warehouse, a server that hosts code, can be simply considered as a computer in your project team for remote data exchange.

To be precise, the three local areas should be the versions pointed to by HEAD in the git warehouse:

  • Directory: Use Git management A directory, that is, a warehouse, contains our workspace and Git management space.
  • WorkSpace: Directories and files that require version control through Git. These directories and files constitute the workspace.
  • .git: Directory that stores Git management information. It is automatically created when the warehouse is initialized.
  • Index/Stage: Staging area, or the update area to be submitted. Before submitting it to the repo, we can put all updates in the staging area.
  • Local Repo: local repository, a repository stored locally; HEAD will only be the current development branch (branch).
  • Stash: Hidden, it is a work status saving stack, used to save/restore the temporary status in WorkSpace.

Workflow

The workflow of git is generally as follows:

1. Add and modify files in the working directory;

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

3. Submit the files in the staging area to the Git warehouse.

Therefore, files managed by git have three states: modified, staged, and committed.

Git project construction

Creating a working directory and common instructions

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

For daily use, just remember the 6 commands below:

Build a local warehouse:

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

1. To create a brand 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, about the version All the information etc. are in this directory.

Clone the remote repository

1. Another way is to clone the remote directory, because it will completely mirror the pants on the remote server to the local one!

#可镂一个项目和它的整个代码历史(版本信息)
$ git clone [url]

2. Go to gitee or github to clone a test

Git file operation

File 4 states

Version control It is the version control of files. To modify and submit files, you must first know the current status of the files. Otherwise, you may submit files that you do not want to submit yet, or files that you want to submit may not be submitted.

  • Untracked: Untracked, this file is in the folder, but it is not added to the git library, does not participate in version control, and changes to staged through git add.
  • Unmodify: The file has been stored in the database and has not been modified. That is, the file snapshot content in the repository is exactly the same as that in the folder. This type of file has two places. If it is modified, it becomes Modified. . If you use git rm to remove the repository, it will become an Untracked file
  • Modified: The file has been modified, just modified, and no other operations have been performed. This file also has two places. You can enter the temporary storage through git add. In the staged state, using git checkout will discard the modifications and return to the unmodify state. This git checkout will take out the file from the library and overwrite the current modifications!
  • Staged: Temporary state. Execute git commit to synchronize the modifications to the library. At this time, the files in the library and the local files become consistent again, and the files are in the Unmodify state. Execute git reset HEAD filename to cancel the temporary storage, and the file status changes to Modified.

Check the file status

It is said that the file has four states. You can check the file status through the following command:

#查看执行文件状态
git status [filename]
#查看所有文件状态
git status
#添加所有文件到暂存区
git add .
#提交暂存区中的内容到本地仓库 -m提交信息
git commit -m "注释内容"

Ignore files

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

Create ".gitignore" in the main directory " file, this file has the following rules:

  1. Ignore empty lines in the file or lines starting with a pound sign (#) will be ignored.
  2. Linux wildcards can be used. For example: asterisk (*) represents any number of characters, hello (?) represents one character, square brackets ([abc]) represent an optional character range, braces ({string1, string2}) represent optional strings, etc. .
  3. If there is an exclamation mark (!) at the beginning of the name, it indicates an exception rule and will not be ignored.
  4. If the beginning of the name is a path separator (/), it means that the file to be ignored is in this directory, and the files in the subdirectory are not ignored.
  5. If the last end of the name is a path separator (/), it means that the subdirectory of the name in this directory is to be ignored, not the file (the default file and subsequent directories are ignored).
#为注释
*.txt        #忽略所有 .txt结尾的文件,这样的话上传就不会被选中
!lib.txt    #但lib.txt除外
/temp        #进忽略项目根目录下的TODO文件,不包括其他目录temp
build/       #忽略build/目录下的所有文件
doc/*.txt    #忽略doc/notes.txt 但不包括 doc/server/arch.txt

.gitignore file content

#java
*.class
*.log
*.lock

#Package Files #
*.jar
*.war
*.ear
target/

# idea
.idea/
*.iml

*velocity.log*

### STS ###
.apt_generated
.factorypath
.springBeans

### IntelliJ IDEA ###
*.iml
*.ipr
*.iws
.idea
.classpath
.project
.settings/
bin/

*.log
tmp/

#rebel
*rebel.xml*

Use code cloud

  1. Register and log in to code cloud, complete personal information
  2. Set up the machine Baoding SSH public key enables password-free login!
# 进入 C:\Userss\Administrator\.ssh 目录
# 生成公钥
ssh-keygen -t rsa

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

4. Use Code Cloud to create your own warehouse

Integrate Git in IDEA

1. Create a new project and bind git

Integrate the remote git Just copy the file directory to the project or create it in the git directory with the same name (git file is a remotely cloned git file)

The idea will appear after refreshing

2. Modify the file and use IDEA to operate git

  • and add it to the temporary storage area git add .
  • commit submission git commit
  • push to the remote warehouse git push

3. Submit test

Git branch

Common instructions in git branch

#列出所有本地分支
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]

Recommended learning: "Git Tutorial"

The above is the detailed content of Thirty minutes to get you started with Git (summary). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete