Git-基础玩法

git的安装及使用

安装git及设置

1
2
3
yum install git
git config --global user.name "Est"
git config --global user.email "vanesk@sina.com"

工作区、暂存区、版本库

(PS:更新流程)
本地路径为工作区,.git目录为版本库
版本库中最重要的为stage或者叫index暂存区,自动创建第一个分支master,以及指向master的一个指针HEAD
git01
添加文件
第一步:git add file把文件修改添加至暂存区;
第二步:git commit -m “update file”提交更改,把暂存区内容提交至当前分支;
需要提交的文件修改放在暂存区,然后一次性提交所以文件至分支(如下图所示);
git02
git03

基础命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#创建git库
mkdir /git_data
cd /git_data
git init #初始化git库会在当前目录创建一个.git目录
git status #查看git库状态

#创建测试文件
touch readme
git add readme #从工作区上传文件至暂存区
git commit -m 'upload file readme' #添加注释把文件上传至master下

#更新文件内容
echo "test" >> readme
git diff readme #查看文件变更信息

git add readme #重新更新文件
git commit -m "update file readme" #重新备注更新

#查看日志
git log #日志
git log --pretty=oneline #显示commit_id 备注

#回退
git reset --hard commit_id #回退到某一个版本通过commit_id
git reset --hard HEAD^ #回退到上一个版本

git reflog #查看历史命令,可以方便确定回退到未来哪个版本

#撤销修改
echo "error" >> readme #写错了内容怎么办
git checkout -- readme #回退到修改之前

echo "error" >> readme #写错了内容
git add readme #并且已经更新至暂存区,还没提交怎么办
git reset HEAD readme #把暂存区的修改撤销并更新到工作区
git checkout -- readme #回退到修改之前

#删除文件
git add deletefile
git commit -m "add deletefile" #如果文件不要了怎么办
git rm deletefile #从版本库中删除文件
git commit -m "rm deletefile" #并且更新注释

#删错了怎么办
git checkout -- deletefile #误删后恢复