[root@foundation40 ~]# mkdir demo
[root@foundation40 ~]# cd demo/
[root@foundation40 demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@foundation40 demo]# echo westos > readme.md
[root@foundation40 demo]# git status #查看状态
[root@foundation40 demo]# git status -s
?? readme.md
在状态报告中可以看到新建的 README 文件出现在 Untracked files 下面。 未跟踪的文件意味着 Git 在之前的快照(提交)中没有这些文件;Git 不会自动将之纳入跟踪范围,除非你明明白白地告诉它“我需要跟踪该文件”, 这样的处理让你不必担心将生成的二进制文件或其它不想被跟踪的文件包含进来。 不过现在的例子中,我们确实想要跟踪管理 README 这个文件。
跟踪新文件
[root@foundation40 demo]# git add readme.md
[root@foundation40 demo]# git status
[root@foundation40 demo]# git status -s
A readme.md
只要在 Changes to be committed 这行下面的,就说明是已暂存状态。
如果此时提交,那么该文件此时此刻的版本将被留存在历史记录中。 你可能会想起之前我们使用 git init 后就运行了 git add (files) 命令,开始跟踪当前目录下的文件。git add命令使用文件或目录的路径作为参数;如果参数是目录的路径,该命令将递归地跟踪该目录下的所有文件。
提交到版本库
将文件提交到版本库中以后,就看不到状态了
[root@foundation40 demo]# git commit -m "add readme.md"
[root@foundation40 demo]# git status -s
暂存已修改文件
新添加的未跟踪文件前面有 ?? 标记,新添加到暂存区中的文件前面有 A 标记,修改过的文件前面有 M 标记。 你可能注意到了 M有两个可以出现的位置,出现在右边的 M 表示该文件被修改了但是还没放入暂存区,出现在靠左边的 M 表示该文件被修改了并放入了暂存区。
例如, README 文件在工作区被修改了但是还没有将修改后的文件放入暂存区,lib/simplegit.rb文件被修改了并将修改后的文件放入了暂存区。 而 Rakefile在工作区被修改并提交到暂存区后又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录。
[root@foundation40 demo]# vim test.txt
test
[root@foundation40 demo]# vim readme.md
westos
westos
[root@foundation40 demo]# git status -s
[root@foundation40 demo]# vim readme.md
[root@foundation40 demo]# git status -s
MM readme.md
#工作区被修改并提交到暂存区后又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录
?? test.txt
[root@foundation40 demo]# git add readme.md
[root@foundation40 demo]# git add test.txt
[root@foundation40 demo]# git status -s
M readme.md
A test.txt
[root@foundation40 demo]# git commit -m "add test.txt"
[root@foundation40 demo]# git status -s
忽略文件
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。
Git 忽略所有以 .o 或 .a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的.
Git 忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副本
[root@foundation40 demo]# touch .file1
[root@foundation40 demo]# git status -s
?? .file1
[root@foundation40 demo]# cd .git/
[root@foundation40 .git]# ls
[root@foundation40 .git]# vim .gitignore
.* #忽略所有文件
[root@foundation40 .git]# cd ..
[root@foundation40 demo]# mv .git/.gitignore .
[root@foundation40 demo]# ls
readme.md test.txt
[root@foundation40 demo]# l.
. .. .file1 .git .gitignore
[root@foundation40 demo]# git status -s #看不到任何文件的状态
版本回退
[root@foundation40 demo]# ls
[root@foundation40 demo]# git log --pretty=oneline
a14a12fee73e661ad019794daf826a025cd2ec2b add test.txt
1ea93d8a3fbb97919e7da884f552603bd96a2e3e add readme.md
[root@foundation40 demo]# git reset --hard HEAD^
HEAD is now at 1ea93d8 add readme.md
[root@foundation40 demo]# ls
readme.md
[root@foundation40 demo]# cat readme.md
恢复
[root@foundation40 demo]# git log --pretty=oneline
[root@foundation40 demo]# git reflog
[root@foundation40 demo]# git reflog
[root@foundation40 demo]# cat readme.md
[root@foundation40 demo]# cat test.txt
撤销修改
[root@foundation40 demo]# vim test.txt
[root@foundation40 demo]# cat test.txt
test
hello world
[root@foundation40 demo]# git status
[root@foundation40 demo]# git checkout -- test.txt
[root@foundation40 demo]# cat test.txt
test
删除文件
[root@foundation40 demo]# git rm test.txt
rm 'test.txt'
[root@foundation40 demo]# git status
[root@foundation40 demo]# git status -s
[root@foundation40 demo]# git commit -m "deleted test.txt" #提交删除test.txt文件,从版本库里彻底删除
[root@foundation40 demo]# git status -s
[root@foundation40 demo]# ls
进入Git页面
添加远程库 让两个仓库进行远程同步
[root@foundation40 demo]# git remote add origin git@github.com/dmf1997/demo.git
#写自己的GitHub账户名,否则连接不到自己的仓库
[root@foundation40 demo]# git push -u origin master
#将本地库的所有内容推送到远程库上. 远程库的名字就是origin
生成密钥
[root@foundation40 ~]# ssh-keygen
[root@foundation40 ~]# cd .ssh/
[root@foundation40 .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@foundation40 .ssh]# cat id_rsa.pub #查看公钥
添加的钥匙是公钥
添加钥匙完成
测试:
测试能否将编写的文件推送到远程库上
[root@foundation40 .ssh]# cd
[root@foundation40 ~]# cd demo/
[root@foundation40 demo]# ls
readme.md
[root@foundation40 demo]# cat readme.md
westos
westos
westos
在网页上查看推送到库里的文件
测试能否将远程库的文件或内容更新到本地库
[root@foundation40 demo]# vim test.txt
[root@foundation40 demo]# cat test.txt
test
[root@foundation40 demo]# git add test.txt
[root@foundation40 demo]# git commit -m "add test.txt" #提交
[root@foundation40 demo]# git push -u origin master #推送到远程库
在远程库里修改文件内容
[root@foundation40 demo]# git pull origin #将远程库更改的内容推送到本地
[root@foundation40 demo]# ls
readme.md test.txt
[root@foundation40 demo]# cat test.txt