Git entry operation and use (detailed)

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Git Simple Notes


1. What is Git?

Git is an open source distributed version control system, which is currently the most advanced and popular version control system in the world. Versioning of very small to very large projects can be handled quickly and efficiently.

  • Features: The larger and more complex the project, the more collaborative developers, the more it can reflect the high performance and high availability of Git!

2. Use Git

1. Download and install Git in Windows

Before you can start using Git to manage versions of your projects, you need to install it on your computer. You can use a browser to visit the following website, and choose to download the corresponding Git installation package according to your operating system:
https://git-scm.com/downloads

2. Configure user information

After installing Git, the first thing to do is to set up your username and email address. Because when version management is performed on the project through Git, Git needs to use these basic information to record who has operated on the project (Note: If the --global option is used, the command only needs to be run once to take effect permanently ) code is as follows (example):

    // 在网站根目录右键-- Git Bash Here
    // 如果是第一次利用git提交,请配置好全局选项
     git config --global user.name "用户名"
     git config --global user.email "你的邮箱地址"

3. Check configuration information

In addition to using Notepad to view the global configuration information (C:/Users/username folder/.gitconfig file), you can also run the following terminal command to quickly view the global configuration information of Git:

	// 查看所有的全局配置项
	git config --list --global
	// 查看指定的全局配置项
	git config user.name
	git config user.email

4. Obtain help information

You can use the git help (verb) command to open the help manual in a browser without networking, for example:

	// 要想打开git config 命令的帮助手册
	git help config

If you don't want to see the full manual, you can get a more concise "help" output with the -h option:

	// 想要获取git config命令的快速参考
	git config -h 

5. Two ways to get Git repository

  • Convert an unversioned local directory into a Git repository
  • Clone an existing Git warehouse from other servers
    Both of the above two methods can get a usable Git warehouse on your computer

6. Initialize the warehouse

  • In the project directory, open "Git Bash" by right mouse button
  • Execute the git init command to convert the current directory into a Git warehouse
  • The git init command will create a hidden directory named .git (if you can’t find hidden projects that can be opened in the viewing directory, this .git directory is the Git warehouse of the current project, which contains the initial necessary files, which are Git An essential part of a warehouse.

7. 4 states of files in the workspace

Each file in the workspace may have four states, which are divided into two categories:

Untracked
Files not managed by Git
Files already managed by Git
Unmodified (Unmodified) : The content of the file in the workspace is consistent with the content of the file in the Git repository
Modified : The content of the file in the workspace is inconsistent with the content of the file in the Git repository
Staged : The modified files in the workspace have been placed in the temporary storage area, and the modified files are ready to be saved in the Git warehouse

Check the status of the file: git status

以精简的方式显示文件状态:
git status -s
git status --short
#未跟踪文件前面有红色的??标记 例:?? index.html

insert image description here


8. Track new files

Put the local file in the temporary storage area, and use the command git add to start tracking a file. So, to track index.html and other files, just run the following command:

git add index.html

At this time, run the git status command again, and you will see that the index.html file is under the Changes to be committed line, indicating that it has been tracked and is in the temporary storage state: display the status of the
insert image description here
file in a simplified way (newly added to the temporary storage files in the zone are marked with a green A in front of them):
insert image description here


9. Submit an update

Put the local file into the local warehouse, and now there is an index.html file in the temporary storage area waiting to be submitted to the Git warehouse for storage. You can execute the git commit command to submit, where the -m option is followed by this submission message, which is used to further describe the content of the submission:

git commit -m '新建了index.html文件"

After the submission is successful, the following information will be displayed:
insert image description here
After the submission is successful, check the status of the file again and get the following prompt:
insert image description here
(All files are in an unmodified state, and no files need to be submitted)


10. Make changes to submitted files

  • Modified file
    Currently, the index.html file has been tracked by Git, and the contents of the index.html file in the workspace and the Git repository are consistent. After we modify the content of index.html in the workspace, and run the git status and git
    status -s commands again, we will see the following content:

insert image description here
insert image description here

The file index.html appears under the line Changes not staged for commit, indicating that the content of the file has changed, but it has not been put into the temporary storage area. (Note: There is a red M mark in front of the modified files that have not been put into the temporary storage area)

  • Temporarily save the modified file
    At present, the index.html file in the workspace has been modified. If you want to temporarily save this modification, you need to run the gitadd command again. This command is a multifunctional command, which mainly has the following three functions:
    1 .You can use it to start tracking new files
    2. Put the tracked and modified files into the temporary storage area
    3. Mark the conflicting files as resolved
    insert image description here
  • Undo the modification of the file (use the git checkout – index.html command to undo the modification of the index.html file)
    Undo the modification of the file means: restore the modification of the corresponding file in the workspace to the one saved in the Git warehouse version of. The result of the operation: all modifications will be lost and cannot be recovered! The risk is relatively high, please operate with caution! The
    essence of the undo operation: use the files saved in the Git warehouse to overwrite the specified files in the workspace.

11. Add multiple files to the temporary storage area at one time

If there are many files to be temporarily stored, you can use the following command to add all the newly added and modified files to the temporary storage area at once:
insert image description here


12. Unstaged files

If you need to remove the corresponding file from the temporary storage area, you can use the following command:

git reset HEAD 要移除的文件名称  // 移除单个文件
git reset HEAD .   // 移除所有文件

insert image description here


13. Skip using the staging area

The standard workflow of Git is workspace → temporary storage area → Git warehouse, but sometimes it is a bit cumbersome to do so. At this time, you can skip the temporary storage area and directly submit the changes in the workspace to the Git warehouse. At this time, Git works The process is simplified to workspace → Git repository.
Git provides a way to skip the use of the temporary storage area. As long as you add the -a option to git commit when submitting, Git will automatically store all tracked files and submit them together, thus skipping git add steps:

git commit -a -m "描述消息"

insert image description here


14. Remove files

There are two ways to remove files from the Git repository:
① Remove the corresponding files from the Git repository and the workspace at the same time
② Only remove the specified files from the Git repository, but keep the corresponding files in the workspace

# 从 Git 仓库和工作区中同时移除 index.js 文件
git rm -f index. js
# 只从 Git 仓库中移除 index.css, 但保留工作区中的 index.css 文件
git rm --cached index.css

15. Ignore files

  • Generally, we always have some files that don't need to be managed by Git, and we don't want them to always appear in the list of untracked files. In this case, we can create a configuration file called .gitignore that lists matching patterns for files to ignore. The format specification of the file .gitignore is as follows:
    ① Comments starting with #
    ② Directories ending with 1 ③ Preventing recursion starting
    with /
    ④ Negation starting with !
    Simplified regular expression)
  • glob pattern The
    so-called glob pattern refers to a simplified regular expression:
    ① Asterisk * matches zero or more arbitrary characters
    ② [abc] matches any character listed in square brackets (this case matches an a or matches a b or match a c)
    ③ question mark? Only match
    any character All numbers from 0 to 9)
    ⑤Two asterisks ✳✳ means match any intermediate directory (such as a/✳✳/z can match a/z, a/b/z or a/b/C/z, etc.)
  • Examples of gitignore files
# 忽略所有的 .a 文件
 * .a
# 但跟踪所有的 lib.a, 即便你在前面忽略了 .a 文件
!lib.a
# 只忽略当前目录下的 T0DO 文件,而不忽略 subdir/T0DO
/TODO
# 忽略任何目录下名为 build 的文件夹
build/ 
# 忽略doc/notes. txt, 但不忽略 doc/server /arch. txt
doc/*. txt
# 忽略 doc/ 目录及其所有子目录下的.pdf文件
doc/**/*.pdf
  • Implementation steps
    ① Create a new .gitignore ignore file in the project root directory
    ② Fill in your own ignore rules and the files you want to ignore in the .gitignore ignore file
    insert image description here
    insert image description here

16. View commit history

If you want to review the commit history of your project, you can use git log , a simple and effective command.

# 按时间先后顺序列出所有的提交历史,最近的提交排在最上面
git log  // 回车继续翻看下面所有历史,输入 q 退出
# 只展示最新的两条提交历史,数字可以按需进行填写
git log -2
# 在一行上展示最近两条提交历史的信息
git log -2 --pretty=one line
# 在一行上展示最近两条提交历史的信息,并自定义输出的格式
# %h:提交的简写哈希值 %an:作者名字 %ar:作者修订日期,按多久以前的方式显示 %s:提交说明
git log -2 --pretty=format:"%h | %an | %ar | %s"

17. Roll back to the specified version

# 在一行上展示所有的提交历史
git log --pretty=oneline
# 使用git reset --hard命令,根据指定的提交ID回退到指定版本
git reset --hard <CommitID>

insert image description here

# 在旧版本中使用 git reflog --pretty=oneline 命令,查看命令操作的历史
git reflog --pretty=oneline
# 再次根据最新的提交 ID, 跳转到最新的版本
git reset --hard <Commit ID>

insert image description here


18. Summary

  • Command to initialize a Git repository
    • git init
  • Commands to view file status
    • git status or git status -s
  • A command to add files to the temporary storage area at one time
    • git add .
  • The command to submit the files in the temporary storage area to the Git warehouse
    • git commit -m "commit message"

3. Summary

The above is what I will talk about today. This article only briefly introduces the basic operation and use of Git.

  • I wish: peace and success

Guess you like

Origin blog.csdn.net/m0_61874034/article/details/125087768