git学习之路
git安装:
linux:
yum -y install git
源码安装,下载对应源码后,解码,依次执行命令:./config -> make -> make install
检查是否安装成功:type|whereis git
检查git版本: git --version
windows:
直接下载安装包安装
安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功!
检查git版本: git --version
最初使用:
1、建立用户及用户email
git config --global user.name "your name"
git config --global user.email "your email"
2、创建仓库
选择一个目录,注意不要用中文目录,执行:
git init
3、添加文件
git add file
4、提交
git commit -m '说明'
遇到问题:
如果不首先建立用户,添加文件后无法提交,请清洗仓库,添加用户,在创建仓库执行。
1、数据仓库状态查看:
git status
1)修改文件后查看,没有add等操作,会显示修改过的文件,并提示git add操作。
[root@zj gitCode]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: 1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
2)修改文件,git add后,提示修改过的文件,可放心提交
3)git commit提交后,没在显示已提交文件的信息
2、文件修改内容查看:
git diff filaname
此命令,会显示文件修改前后的内容,‘+’代表增加的内容,‘-’代表删除的内容。
3、版本回退
1)查看git的log
git log 每次多行显示,显示详细内容,如修改用户与时间等
git log --pretty=oneline 每次修改只显示一行,修改后版本哈希值和提交的说明
[root@zj gitCode]# git log --pretty=oneline
f7c0a78db60864ca41a6ebd0a39551b0248f5c85 update 1 file
f7c9c3613dbcb31c982e6b17eddb37cf703063ff update 1 file
b72a45438423249eff55d5c9f4d610903c624f7e add 1 file
2)版本变动
这里不能说版本回退,虽然大多时候都是用来回退版本。git会对每次修改进行快照,会保存其状态,每次版本提交都会生成一个十六进制的哈希值,在时间轴上线性排列。可以这样理解,有个HEAD指向版本快照节点,每次修改后HEAD自动指向最新快照节点。若想版本回退或则回退后想返回最新版本,改变HEAD指向版本快照节点即可。
执行命令:
git reset --hard 版本快照节点(一般为十六进制前几位,可以区分其他版本)
特殊命令:
git reset --hard HEAD^ 回退版本到最初状态
3)版本查看
若回退到以前版本,而无法通过git的log查看到后面的版本快照,这样就无法回到之后的版本了。此时可以通过查看版本改变日志,可以查看到之后的版本。
git reflog