配置账号信息

git config --global user.name "your username"
git config --global user.email "your email"

查看配置相关的信息

git config --list # 查看配置的信息
git help config # 获取帮助信息

配置自动换行

git config --global core.autocrlf input # 提交到git时是否自动将换行符转换为lf

配置密钥

ssh-keygen -t rsa -C "your email" # 生成密钥
ssh -T git@github.com # 测试是否成功

新建仓库

git init # 初始化本地仓库
git clone <url> # 克隆远程版本库
git add –all # 添加所有文件到暂存区(stage,index)
git status # 查看当前git状态
git remote add origin https://github.com/freshdgq/test.git # 关联到远程仓库地址
git push -u origin master # 第一次推送master分支的所有内容

修改和提交

git status # 查看git当前状态
git diff # 查看改动后和改动前的不同之处
git add . # 跟踪所有改动过的文件
git add <file> # 跟踪指定的文件
git mv <old> <new> # 文件改名
git rm <file> # 删除指定文件
git rm --cached <file> # 停止跟踪文件但不删除
git commit -m "message" # 提交修改的文件到当前分支
git commit --amend # 修改最后一次提交

状态与版本处理

git log # 查看提交的历史记录
git log -p <file> # 查看指定文件的提交记录
git blame <file> # 以列表方式查看指定文件的提交历史
git reset --hard HEAD  # 撤销工作目录中所有未提交文件的修改内容
git checkout HEAD <file> # 撤销指定的未提交文件的修改内容
git revert <commit> 撤销指定的提交
git reset --hard HEAD^ # 回退本地分支到上一个版本
git reset --hard HEAD~n # 回退本地分支到上n个版本
git checkout – readme.txt # 把readme.txt文件在工作区的修改全部撤销

分支与标签处理

git branch # 查看本地所有分支
git checkout <branch/tag> # 切换到指定分支或标签

git branch <new-branch> # 创建新分支
git checkout -b <new-branch> # 创建并切换到新分支
git branch -d <branch> # 删除本地分支

git merge <branch> # 合并指定分支到当前分支
git rebase <branch> # 衍合指定分支到当前分支

git tag # 列出所有本地标签
git tag <tagname> # 基于最新提交创建标签
git tag -d <tagname> # 删除标签

本地远程

git remote # 获得远程库列表
git remote v # 查看远程版本库信息
git remote show <remote> # 查看指定远程版本库信息
git remote add <remote><url> # 添加远程版本库
git remote rm <name> # 删除对应的远程库
git branch -u origin/master master # 本地跟踪远程

git fetch <remote> # 从远程库获取代码
git pull <remote><branch> # 拉取远程指定分支代码并快速合并
git push <remote><branch> # 推送代码到远程指定分支并快速合并
git push <remote>:<branch/tag-name> # 删除远程分支或标签
git push --tags # 上传所有标签

git pull origin master # 从远程的master分支拉取代码到本地
git push origin master # 推送最新修改到远程的主分支