Git latest tutorial 3 - Git project construction + Git file operation

Table of contents

1. Create a working directory and common instructions

Two local warehouse construction

1 Create a new warehouse 

2 Clone the remote repository

Three Git file operations

1. Four states of the file

2. View file status

3. Ignore files


1. Create a working directory and common instructions

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 following 6 commands:

Two 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.

1 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.

2 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!

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

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

At this time, since we have not set up ssh, a window will pop up asking us to enter the user name and password. as follows:

Here we can retest after setting up ssh! 

Three Git file operations

1. 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 do not want to submit now, or the file you want to submit has not been submitted.

  • 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. The status becomes Staged through git add .

  • 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 , and discard the modified state when using git checkout , 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

2. 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 提交信息

Case operation:

 

 Use the git add . command to put the file in the temporary storage area:

 Submit the content in the temporary storage area to the local warehouse:

3. Ignore files

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

 For example: the configuration of the idea project is as follows:

Guess you like

Origin blog.csdn.net/weixin_46474921/article/details/127103077