参考文档 廖雪峰的git教程
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| //命令行安装git $ sudo apt install git
//初始化仓库 $ git init
设置你的名字和email地址 $ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
//克隆仓库 //Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。 $ git clone git@github.com:username/name.git $ git clone https://github.com/username/name.git
//添加文件到git仓库 $ git add readme.md //添加单个文件 $ git add . //添加所有文件 //把文件提交到仓库 $ git commit -m "describe your file"
//查看状态 $ git status //查看更新对比内容 $ git diff //查看历史记录 $ git log
//回退到上个版本 //上上个版本就是HEAD^^ //100个版本就是HEAD~100。 $ git reset --hard HEAD^
//查看命令历史 $ git reflog
//撤销修改 $ git checkout -- readme.txt
//删除文件 $ rm test.txt
//提交到远程库(github) $ git push
//创建新的分支(dev) $ git checkout -b dev //相当于以下两条命令 //新建分支 $ git branch dev //切换分支 $ git checkout dev //使用git branch查看分支
这时有两个分支,旧分支为master,新的分支为dev。 在更改了dev分支之后,需要把新的内容合并到master分支中去。 先把dev中的更新内容提交到版本库中,切换回master分支,然后合并分支。 $ git checkout master $ git merge dev
//删除分支 $ git branch -d dev
|
一些“技巧”
有没有经常敲错命令?比如git status?status这个单词真心不好记。
如果敲git st
就表示git status
那就简单多了,当然这种偷懒的办法我们是极力赞成的。
我们只需要敲一行命令,告诉Git,以后st
就表示status
:
$ git config --global alias.st status
好了,现在敲git st
看看效果。
当然还有别的命令可以简写,很多人都用co
表示checkout
,ci
表示commit
,br
表示branch
:
1 2 3
| $ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.br branch
|
以后提交就可以简写成:
$ git ci -m "bala bala bala..."
--global
参数是全局参数,也就是这些命令在这台电脑的所有Git仓库下都有用。