1、安装git,配置用户信息
// for Linux os
sudo apt-get install git
// for Mac os
brew install git
// for Windows os
Git 官网下载默认安装
git config --global user.name "Your name"
git config --global user.email "email@example.com"
2、创建版本库repository(在一个空文件夹)
git init
3、添加文件到版本库
// add file to repository
git add readme.txt
// commit to repositiory
git commit -m "wrote a readme file"
其中,在对readme文件进行过修改之后。使用git status命令可见确实存在未提交的改动。
可以通过git diff 命令查看修改
知道修改之后再进行提交 git add readme.txt,此时允许git status查看当前仓库的状态
之后进行提交git commit -m "add distributed"
4、版本回退
git log命令查看版本更新日志,可以看到两次更新操作日志中
版本回退我们当然需要我们需要回退到的版本。git中HEAD表示当前版本,也就是最新提交的4ead84,然后上一个版本就是HEAD^,上上个是HEAD^^,往上数100个是HEAD~100。
回退版本git reset --hard HEAD^(在windows cmd 中^是转义符号,mac中cat可以查看文本,在windows中可以通过type命令查看)
其中git的暂存区和工作区的概念需要后续进行了解
5、远程仓库(使用GitHub作为例子)
- 创建SSH Key,在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有
id_rsa
和id_rsa.pub
这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key: -
登陆GitHub,打开“Account settings”,“SSH Keys”页面:然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴
id_rsa.pub
文件的内容:
6、添加远程库
7、git创建并切换分支
git checkout -b feature1