1.1 了解帮助命令
git help: 查看命令git help add: 查看git add命令的具体解释
1.2 仓库初始化
git init: 创建.git, 适合在已存在项目追加版本控制git init projectname: 创建projectname/.git, 适合项目开始时加入版本控制
1.3 文件基本操作
git add filename/*: 添加文件 [产生暂存文件]git commmit -m "message": 将添加的文件提交到本地仓库 [产生提交文件]git rm filename: 移除文件,使用rm filename的有暂存git add -u .: 如果之前使用非 git 命令删除文件,可以使用这个命令把当前目录的文件重新遍历清除git rm --cache filename: 暂存但是不参与跟踪git mv filepath newfilepath: 移动文件git rm filepath && git add newfilepath: 移动文件,之前使用非 git 命令移动文件git add -A .: 如果之前使用非 git 命令移动文件,可以使用这个命令把当前目录的文件重新遍历移动,和rm命令类似git reset etc...: 历史提交管理 (回退,合并...),checkout 更关注文件
1.4 查看文件修改
git status: 查看文件信息git diff: 查看修改 [工作树和暂存文件]git diff --staged: 查看修改 [暂存文件和最近提交文件]git diff HEAD: 查看修改 [工作树和最近提交文件]git diff --word-diff: 查看修改的单词用颜色标出git diff --stat: 查看修改的文件名
参考资料 0. Git 学习(三)本地仓库操作——git add & commit 了解 git 版本库实现
1.5 查看提交 Log
git log: 显示提交信息git log --oneline: 显示提交 Messagegit log --stat: 显示提交文件名级详细修改信息git log --patch: 显示提交文件内容级详细修改信息git log --graph: 用图显示提交记录git log --graph --all --decorate --oneline: 去除冗余信息,更加直观显示每条分支每次提交git log --stat -- filename: 文件提交记录(不记录路径移动)git log --stat -M --follow -- filename: 看到完整的文件操作过程
1.6 忽略文件
touch .gitignore: 创建文件 (次级目录也可以创建)vim .gitignore: 编辑文件添加 ignore 文件。*.log | tmp/ | .sass-cache etc...git ls-files --others --ignored --exclude-standard: 查看被 ignore 的文件git reflog: 详细修改日志
1.7 分支操作
git branch branchname: 创建分支git branch: 显示分支git branch -d branchname: 删除分支git branch -D branchname: 删除未合并分支git checkout branchname: 切换分支git checkout commitID: 工作树切换到 commitID 时git checkout -- filename: 清理掉最后一次提交内容git checkout -b branchname: 创建新分支并且进入该分支git merge branchname: 和并 branchname 分支到目前所在分支 (合并时文件冲突要手动解决)git merge --abort: 清除工作目录和暂存区git merge squash branchname: 将合并的分支改变变成一个 commitgit rebase branchname: 将当前分支历史提交合并到 branchname 分支
1.8 远程操作
git remote add origin https://github.com/accountname/projectnamegit remote set-url origin newUrl: 改变 URLgit remote rm origin: 删除git remote -v: 查看 URLgit fetch origin: 抓取远程分支,本地会有一个remotehostname/branchname的分支,一般用于查看伙伴代码git pull origin: 和 fetch 类似,但是是取回远程更新和本地合并。相当于先 fetch 再 merge。git push origin: push 到远程仓库
参考资料 1. Git 远程操作详解