0
点赞
收藏
分享

微信扫一扫

2022-01-13 Git初级使用

幸福的无所谓 2022-01-13 阅读 22
gitgithubssh

文章目录


此文直接从下载和安装Git开始描述。
如果想知道Git是什么?有什么用?请看别的博文。

安装

我的描述肯定没有官方文档详尽!
官网文档:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

帮助文档

下面的语句会在浏览器打开本地一个详细说明的文件。

git help <verb> # git help --config
git <verb> --help # git config --help
man git-<verb> # 不知道为什么,没有使用成功

下面的语句仅在命令窗口显示正常的帮助文档。

git <verb> -h # git config -h

配置Git

官网文档:https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup

Git的配置文件

Git的配置文件可能被存在三个地方:

明显的,如果三个配置文件中存在同样的变量,则有优先级:Level 3 > Level 2 > Level 1
可通过下方的代码查看配置文件位置,以及其中的变量:

git config --list --show-origin

我因为还没有创建repository,所以运行结果有两个位置:D盘中git的安装位置,C盘中具体用户的位置。
在这里插入图片描述

配置用户身份

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

其他配置:

## 编辑器设置
git config --global core.editor emacs
# windows上必须用完整的地址
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

如果不小心写错了,直接运行下面的命令,在vim里修改即可。

git config --global --edit

查看配置的变量

下面的命令和"git config --list --show-origin"效果类似,只是不显示所在文件的信息,而仅显示文件中的变量信息。

git config --list

显示的结果可能会出现同名的变量,是因为来自不同的文件,根据前面的优先级,只有最后出现的变量才会起作用。如果变量很多,可以通过下面这条语句来确定。

git config --show-origin rerere.autoUpdate

查看某个确切的变量

git config user.name

创建一个Git Repository

添加文件以版本控制

cd到某个空文件夹之后,运行下面的语句:

# 创建.git文件夹,用以保存repository的文件信息。
git init

把需要版本控制的文件放入文件夹,运行git add命令添加文件

git add *.*    #所有文件
git add *.cpp  #所有cpp文件
git add LICENSE    #添加LICENSE
git add file_name 

#提交并附带备注信息
git commit -m 'Initial project version'

从网上克隆一个repository

从网上克隆一个库到当前文件夹

git clone https://github.com/libgit2/libgit2

从网上克隆一个库到当前文件夹,并从新给这个库命名

git clone https://github.com/libgit2/libgit2 mylibgit

记录repository的变化

官方文档:https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

git status

用add命令添加added.text文件以版本控制之后,运行git status的结果如下图所示。
可以看见有版本控制的文件是绿色的,未添加版本控制的文件是红色的。
在这里插入图片描述
修改added.text文件之后运行git status的结果:
在这里插入图片描述

忽略文件的编写

在最开始使用git init的文件夹中,新建.gitignore文件。文件的编写规则如下:

# ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in any directory named build
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf

剩下的内容暂时参考下方另一位博主的文章即可,后面有别的内容再添加。
https://blog.csdn.net/Lakers2015/article/details/114659213

举报

相关推荐

Git基础(13):IDEA中使用Git详解

Git01

2022/4/13

2022/1/13

2022/02/13

VS2022 Git功能的使用

2022/01/30

2022/01/29

0 条评论