git官网:https://git-scm.com/
代码托管网站GitHub官网:https://github.com/
系统环境:CentOS 6.6
(啰嗦一句:网上好多文章安装git时总喜欢分客户端、服务端。其实安装是一样的)
git安装
[root@localhost ~]# yum -y install git #git安装
[root@localhost ~]# git --version #查看版本
git version 1.7.1
[root@localhost ~]# mkdir /date ;cd /date
[root@localhost date]# git init #初始化一个空目录.git版本库
# git init --bare project.git #指定初始目录-Git的版本库
[root@localhost date]# ls .git/ #查看git目录结构
branches config description HEAD hooks info objects refs
配置用户名和邮箱地址
git config --global user.name "Your Name"
git config --global user.email you@example.com
git提交 git add && git commit -m “”
touch 1.txt
git status #查看状态
git add 1.txt #告诉git,把什么文件提交到仓库中去
#空目录不能提交
git commit -m '版本说明' #提交
git diff 文件名 #(提交前)查看修改内容
查看提交版本
git log #查看提交版本
git log --pretty=oneline #精简输出
5c97f78e3c892b42bb2910c24cb9dfacd1657b57 1.txt
Git的版本号是一个 40 位的 SHA-1 的哈希值
Git版本回滚
Git中:
HEAD -- 表示当前版本 0
HEAD^ -- 上一个版本 1
HEAD^^ -- 再上一个版本 2
HEAD~10 -- 10个版本 10
回到上一个版本:
git reset --hard HEAD^
这时查看上一个版本为最新,要想回到之前最新版本:
git reset --hard c0f9f38c76286be379f2399a59c3680af24a7181
#git log最新的commit id(即,版本号)
如果没存之前最新的版本号,运行git reflog
[root@localhost date]# git reflog #列出head曾经指向过的一系列commit
fd6c722 HEAD@{0}: HEAD^: updating HEAD
1e00363 HEAD@{1}: commit: 3.txt
fd6c722 HEAD@{2}: commit: 2.txt
5c97f78 HEAD@{3}: commit (initial): 1.txt
[root@localhost date]# git reset --hard fd6c722
修改、删除操作
1、取消(撤消)修改
当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout -- filename
当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,
git reset HEAD filename #回到了上面的情况
git checkout -- filename
已经提交了不合适的修改到版本库时,想要撤销本次提交,参考我们前面讲解的版本回退,不过前提是没有推送到远程库
2、删除操作
rm -rf 文件名
git rm 文件名(目录加 -r 参数) #放入暂存区
git commit -m "" #提交
分支管理
查看本地版本库: # git branch (-r:查看远程,-a:查看所有)
创建分支: # git branch develop
切换分支: # git checkout develop
创建并切换分支: # git checkout -b develop
合并某分支到当前分支: # git merge <name>
查看各个分支最后一个提交对象的信息# git branch -v
查看已经合并分支 # git branch --merge
查看尚未合并分支 # git branch --no-merged
一般来说,列表中没有 * 的分支通常都可以用 git branch -d 来删掉。原因很简单,
既然已经把它们所包含的工作整合到了其他分支,删掉也不会损失什么 -D 强制删除
查看Git仓库存放的数据具体有哪些文件,可以通过一些命令
$ git cat-file -p master^{tree} #查看最新版本内容
100644 blob a906cb2a4a904a152e80877d4088654daad0c859
040000 tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
# master^{tree} 表示 branch 分支上最新提交指向的 tree 对象。请注意 lib 子目录并非一个 blob 对象,而是一个指向别一个 tree 对象的指针:
$ git cat-file -p 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
100644 blob 47c6340d6459e05787f644c2447d2595f5d3a54b simplegit.rb
转载请务必保留此出处:http://blog.youkuaiyun.com/fgf00/article/details/51162823