命令名称 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m “日志信息” 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
设置用户签名
基本语法:
git config --global user.name 用户名 | 设置用户签名
git config --global user.email 邮箱 | 设置用户签名
配置完成查看:
cat ~/.gitconfig
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git首次安装必须设置一下用户签名,否则无法提交代码。
这里设置用户签名和将来登录GitHub的账号没有任何关系。
初始化本地库
基本语法:
git init
查看.git文件
ll -a
查看本地库状态
基本语法:
git status
输出:
on branch master 表示当前本地库在master分支中
no commits yet 目前没有提交任何东西
nothing to commit (create/copy files and use "git add" to track) 没有什么需要提交
新建hello.txt文件后查看状态显示:
on branch master
no commits yet
untrack files:
(use "git add <file>...") to include in what will be committed 表示git没有追踪此文件
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
添加暂存区
基本语法:
git add hello.txt
使用git status
显示:
on branch master
no commit yet
changes to be commited:
(use "git rm --cache <file>..." to unstage)
new file: hello.txt 绿色说明当前已经追踪到文件
文件当前只是存在于暂存区中,暂存区文件是可以删除的,如果不想将hello.txt保存历史版本,可以将其删除
git rm --cached hello.txt
提交本地库
将暂存区文件提交到本地库
基本语法:
git commit -m "日志信息" 文件名
输出文件版本号
git commit -m "my first commit" hello.txt
查看状态:
git status
on branch status
nothing to commit, working tree clean
查看日志:
修改文件
vim hello.txt
# 这里随意修改文件内容
查看文件状态:
git status
红色表示修改还没被添加到暂存区中。
git add hello.txt
git status
提交本地库:
git commit -m "second commit" hello.txt
git是按行来维护文件的。
git status
git reflog
可以看出当前有两个版本
指针指向第二个版本
查看历史版本
基本语法:
git reflog 查看版本信息
git log 查看版本详细信息
版本穿梭
基本语法:
git reflog 首先查看当前历史记录
git reset --hard 版本号
git reflog 切换完成后再查看版本历史记录
Git切换版本,底层其实就是移动HEAD指针