Git 工作区、暂存区和版本库概念:
工作区:电脑里能看到的目录。
暂存区:英文叫 stage 或 index。一般存放在 .git 目录下的 index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
版本库:工作区有一个隐藏目录 .git,这个不算工作区,而是 Git 的版本库
一、新建代码库
1. git init-----在当前目录新建一个Git代码库
2.git init 项目名称----新建一个目录,将其初始化为git代码库
二、配置
1.git config --list 显示当前的Git配置
2.git config --e[--global] 编辑git配置文件
三、增加/删除文件
1.git add. 添加当前目录的所有文件到暂存区
2.git add [dir] 添加指定目录到暂存区,包括子目录
3.git add [file1] [file2] .. 添加指定文件到暂存区
4.git rm [file1] [file2] ... 删除工作区文件,并且将这次删除放入暂存区
四、代码提交
1.git commit -m [message] 提交暂存区到仓库区
2.git commit [file1] [file2] ... -m [message] 提交暂存区的指定文件到仓库区
五、分支
1.git branch 列出所有本地分支
2.git branch -r 列出所有远程分支
3.git branch -a 列出所有本地分支和远程分支
4.git branch [branch-name] 新建一个分支,但依然停留在当前分支
5.git checkout -b [branch] 新建一个分支,并切换到该分支
6.git checkout - 切换到上一个分支
7.git merge [branch] 合并指定分支到当前分支
8.git branch -d [branch-name] 删除分支
9.git push origin --delete [branch-name] 删除远程分支
六、查看信息
1.git status 显示有变更的文件
2.git log 显示当前分支的版本历史
3.git log --stat 显示commit历史,以及每次commit发生变更的文件
4. git log -S [keyword] 搜索提交历史,根据关键词
5. git shortlog -sn 显示所有提交过的用户,按提交次数排序
6.git reflog 显示当前分支的最近几次提交
七、远程同步
1.git fetch [remote] 下载远程仓库的所有变动
2.git remote -v 显示所有远程仓库
3. git remote show [remote] 显示某个远程仓库的信息
4.git remote add [shortname] [url] 增加一个新的远程仓库,并命名
5.git pull [remote] [branch] 取回远程仓库的变化,并与本地分支合并
6.git push [remote] [branch] 上传本地指定分支到远程仓库
7.git push [remote] --all 推送所有分支到远程仓库