目录
学习资源来自廖雪峰的Git教程
本文简短记忆学习内容,主要是使用命令,方便以后查看,完整学习请查看廖雪峰Git教程
操作过程在Ubuntu18.04
完成,其他平台没有尝试
安装Git
在Ubuntu18.04安装
$ sudo apt install git
安装完成后,需要进一步的设置,输入命令:
$ git config --global user.name "Your Name" #Your Name自己更改
$ git config --global user.email "[email protected]" #[email protected]处自己更改
创建版本库
创建
输入命令并返回如下内容即完成创建
$ git init
Initialized empty Git repository in <directory path>
添加文件
$ git add <file name>
$ git commit -m <message> # for example: "Add two files."
版本回退
查看历史记录
$ git log
单行显示历史记录
$ git log --pretty=oneling
如果要恢复到上一个版本,使用命令
$ git reset --hard HEAD^
HEAD^
代表上一个版本,HEAD^^
表示上上一个版本,HEAD~100
表示往上100个版本
根据指定版本号恢复
$ git reset --hard <commit id> # For example: 1094a(It's unecessary to input the whole commit id. A few letters is OK.)
查看所有分支的所有操作(包括提交、回退、已删除的提交操作记录等)
可以利用这个命令查看以前版本的信息,进行回退、恢复等操作
显示所有操作
$ git reflog
显示指定行数n
$ git reflog -n
管理修改
查看当前状态
$ git status
对比当前状态与现在工作区中文件的差别
$ git diff HEAD -- <file name>
可以的操作:
第一次修改->git add
->第二次修改->git add
->git commit
撤销修改
工作区内文件修改之后还没有保存到暂存区
$ git checkout -- <file name>
有两种意思:
- 在修改了file的内容之后,还没有
git add
,使用此命令可以恢复到文件修改之前。 - file这次修改之前,已经
git add
了一次或多次,使用此命令之后,file中内容恢复到最后一次git add
的内容
工作区内文件修改之后已经保存到暂存区
$ git reset HEAD <file>
此命令可以将暂存区清空,将暂存区中git add
提交的内容撤销掉,但是文件内容不改变。
如果想要丢弃工作区的内容,再使用git chechout -- <file name>
命令。
工作区内文件修改之后已经提交到版本库
查看上一节内容,版本回退。
删除文件
在使用命令rm <file>
或其他方法删除文件之后,可以有两种方法更改。
在版本库中删除。
$ git rm <file name>
$ git commit -m <message> # 删除之后提交,生成commit
误删除,需要恢复
$ git checkout -- <file name> # 撤销对<file name>的更改