1.建立仓库
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
$ git init #把当前目录变成Git可以管理的仓库
Initialized empty Git repository in /Users/michael/learngit/.git/
2.版本控制
添加文件到Git仓库,分两步:
- 使用命令
git add <file>
,注意,可反复多次使用,添加多个文件; - 使用命令
git commit -m <message>
,完成。
git add readme.txt #把文件添加到仓库
git add file2.txt file3.txt
git commit -m "wrote a readme file" #把文件提交到仓库
3.时光机
![企业微信截图_20180629104312.png-44kB](http://static.zybuluo.com/JasonChiu/8j3lsbingxy2mt770svz28rc/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20180629104312.png)
企业微信截图_20180629104312.png-44kB
HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令
git reset --hard commit_id
。穿梭前,用
git log
可以查看提交历史,以便确定要回退到哪个版本。要重返未来,用
git reflog
查看命令历史,以便确定要回到未来的哪个版本。
4.远程仓库
- 关联远程仓库
git remote add origin git@github.com:michaelliao/learngit.git
- 本地库推送到远程
git push origin master
- 克隆一个仓库
git clone git@github.com:michaelliao/gitskills.git
5.分支管理
git branch #查看分支
git branch <name> #创建分支
git checkout <name> #切换分支
git checkout -b <name> #创建+切换分支
git merge <name> #合并某分支到当前分支
git branch -d <name> #删除分支,合并完成后就可以删除分支了
6.常用命令
git status #查看仓库当前状态
git diff <file> #查看修改内容
git log --pretty=oneline#历史记录
git reset --hard HEAD^ #回溯到上一个版本
git tag v1.0 #打标签
git show v1.0 #查看标签信息