Git安装
下载 window git
-Msysgit https://git-scm.com/download/win
配置用户信息
- git config --global user.name “oym”
- git config --global user.email “1439620614@qq.com”
可通过 -git config --list 查看信息
Git仓库
- 初始化版本库
-git init - 添加文件到版本库
-git add
-git commit - 查看仓库状态
-git status
Git中文件的4中状态
- untracked : 未被追踪
- Modified : 表示工作区修改了某个文件但是还没有添加到暂存区
- Staged : 表示把工作区修改的文件添加到了暂存区但是没有提交到版本库
- Committed : 表示数据被安全的存储在本地库中
Git的3层结构
- working directory 工作区
- staging index 暂存区
- git directory(Respository)版本库
Git 基本命令
- git init - 初始化git仓库;出现 .git文件
- git add - git add filename : 将文件添加到暂存区
git add - git add :将工做目录下的所有修改的文件添加到暂存区 - git commit - git commit -m ‘description’ :将暂存区内的文件提交到版本库
git commit - git commit -am ‘description’ : 跳过git add 添加到暂存区的命令,直接将工作区所有已跟踪的文件提交 - git log : 主要用于查看提交历史,同时根据添加的参数不同也会有不同的用法
- git status :查看项目文件状态
Git 撤销操作
- git checkout – filename : 拉去暂存区的文件并将其替换工作区的文件
- git reset HEAD – filename : 拉取最近一次提交的版本库的这个文件到暂存区,该操作不影响工作区
- git commit – amend : 撤销上一次提交 并将暂存区的文件重新提交
Git 文件删除
- git rm --filename : 删除工作区及暂存区中的该文件,相当于删除后再执行 git add
- git rm --cached filename (在不小心将不需要追踪的文件添加到暂存区,想删除暂存的该文件,将其保留在工作区)
- git rm -f filename : 当工作区或者暂存区文件修改了,防止把修改误删除了
- git mv oldname newname : 相当于: 1. git rm oldname 2. git add newname
Git 分支
git 分支的创建,修改,切换,删除
- git branch : 查看分支
- git branch branchname : 创建分支
- git branch -m oldname newname : 修改分支名称
- git checkout [-b] branchname :切换分支
- git branch -D branchname : 删除分支
分支的合并
- 分支的指针 : HEAD 指针指向当前工作的分支 : 在切换分支时指向新的分支
- git diff : 比较工作区与暂存区文件的差异
- git diff --staged : 比较暂存区与版本库的文件差异
- git diff 版本号 版本号 : 比较分支内的两个版本的差异
- git diff 分支 分支 :比较两个分支的最新提交版本的差异
- git merge branchname (合并之前需要先切换到master分支):快速合并和冲突合并
储存变更
- git stash
- git stash list
- git stash apply stash@num : 如果apply后面不加名字则应用最近一次的储存信息
- git stash drop stash@num : drop可以运用储存的信息并且删除它,apply不具备删除功能
Git远程仓库
Github上的仓库
- 创建仓库
- clone仓库到本地
- 本地 push 到仓库
- pull 代码 到本地
- ignoring files
远程服务器上的仓库
- 用 taskmanager 来演示
- git init --bare
- git remote add name path : 用尽量简单的 name 代替 path(github文件共享地址) 进行信息传送
- git remote rm name
- git remote rename oldname newname
- git fetch
Git ssh免密登陆
- ssh-keygen :输入后在本地会自动创建文件存储私钥和公钥,把公钥粘贴到 github上,成功后即可免密登陆(及免密传输文件到github上共享)
- ssh-copy-id user@host :将本机的公钥复制到远程服务器的authorized_keys文件中
Git 帮助文档的使用
- git help + __ :可弹出相应内容的使用文档介绍