一.Git安装
-
在官网下载Git安装包后
配置用户信息
#初始化用户名 git config --global user.name "Peter" #初始化邮箱 git config --global user.email "12345@qq.com"
查看配置
git config --list
二.Git仓库与工作流
-
初始化版本库
git init
-
添加文件到版本库
git add 文件名 git add . //添加所有文件到版本库 git commit -m "描述"
-
查看仓库状态
git status #提交完成后会提示:nothing to commit, working tree clean
-
需求变更
#git add后想要回滚 git reset HEAD 文件名 git checkout -- 文件名 #git commit后想要回滚 git log git reset --hard 这里放查询到的版本的commit代码 #删除 git rm 第一天的需求.txt git commit -m "delete"
-
Git工作流
-
.gitignore文件夹
- 将不要的文件放在该文件夹中
三.远程仓库
-
创建SSH key
ssh-keygen -t rsa -C "12345@qq.com" #注意:上述邮箱为github的登陆邮箱号,且生成密钥时在 ~/.ssh/ 目录下执行命令,生成秘钥文件名字为"id_rsa" cat id_rsa.pub
-
在Github中加入上述生成的SSh-key,然后在终端连接
ssh -T git@github.com
四.仓库管理
-
克隆仓库
git clone 仓库地址 # SSH连接地址写法: 用户名@IP:路径 # 例如: git@39.97.107.103:/home/git/blog.git # 最终写法 git clone ssh://git@39.97.107.103:/home/git/blog.git
-
同步本地仓库至远程仓库
git push
-
绑定本地仓库到远程仓库
git remote add origin 远程仓库地址
-
拉取远程仓库变更到本地
git pull
五.标签管理
-
查看所有标签
git tag
-
创建标签
git tag 标签名
-
指定提交信息
git tag -a 标签名 -m "comment"
-
删除标签
#本地标签 git tag -d 标签名 #远程标签 git push origin :refs/tags/标签名
-
标签发布到远程仓库
git push origin 标签名
六.分支管理
-
创建新分支
git branch 分支名
-
查看所有分支
git branch
-
切换分支
git checkout 已存在的分支名 git checkout -b 要创建的新分支名
-
合并分支
#先切换到要合并的主分支 git merge 被合并的分支名 //merge后相当于已经commit
-
删除分支
git branch -d 要删除的分支名
七.Git + 个人服务器
-
参考链接:www.jianshu.com/p/73f354f3e…
sudo apt-get install git sudo adduser git cd /home/git # 上传id_rsa.pub cat /home/git/id_rsa.pub >> /home/git/.ssh/authorized_keys sudo chown -R git:git /home/git su git chmod 600 ~/.ssh/authorized_keys # 创建仓库 mkdir blog.git cd blog.git git --bare init # 钩子 cd hooks touch post-receive # post-receive写入以下内容 #!/bin/sh git --work-tree=/home/git/projects/blog --git-dir=/home/git/blog.git checkout -f # 加入执行权限 chmod +x post-receive exit chown -R git:git /home/git/blog.git # 建立文件夹 mkdir /home/git/projects mkdir /home/git/projects/blog chown -R git:git /home/git/projects # 客户端 git clone ssh://git@175.178.56.73:/home/git/blog.git
以上内容介绍了用命令行来进行Git的基本操作,在日常开发中可用图形化界面Git软件:Sourcetree