Git 学习笔记
Git 是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理
Git 官网 在这里
Git 在线学习网站 在这里
工作原理
workspace 工作区,以文件形式保存在硬盘里
index/stage 暂存区,git add 后保存在暂存区
repository 本地版本库, git commit 后保存在本地仓库
remote 远程版本库, git push 后保存到远程仓库
分支管理策略
# 主分支,只用于发布生产环境的版本,不应该直接做修改或提交新功能
master
# 开发分支,包含了当前正在进行的所有功能和任务。
develop
# 功能分支,在develop上创建,用于独立分开发新功能,并在功能测试完成后合并回develop。
feature
# 发布分支,在develop上创建,用于**最终的**测试和bug修复,最终测试完成后合并到 master 和 develop
release
# 紧急修复分支,在master上创建,用于修复生产环境紧急的问题,修复后合并回master 和 develop
hotfix
常用命令
初始化
#在本地初始化一个仓库
git init
#从远程克隆一个仓库保存在本地
git clone
配置
#查看默认配置信息
git config --list
# 设置全局用户信息
git config --global user.name "newName"
git config --global user.email "newName"
查看信息
# 查看有变更的文件
git status
# 查看当前分支版本历史
git log
# 显示某个文件的版本历史,包括文件改名
git log --follow [file]
git whatchanged [file]
# 显示操作记录
git reflog
# 显示指定文件是什么人在什么时间修改过
git blame [file]
代码管理
# 工作区 -> 暂存区
git add . //添加所有文件
git add [file1] [file2] 添加指定文件
git add [dir] 添加指定目录文件
# 暂存区 -> 本地仓库
git commit -m [message]
git commit [file1] [file2] -m [message]
# 工作区 -> 本地仓库(不包括新增文件)
git commit -a -m [message]
# 修改上一次commit
git commit --amend -m [message]
git commit --amend [file1] [file2] ...
# 停止追踪文件
git rm --cached [file]
分支管理
# 查看所有分支,包括本地分支和远程分支
git branch -a
# 新建分支
git branch [branch] 停留在当前分支
git branch [branch] [commit] 指向指定commit
git branch --track [branch] [remote-branch] 指定远程分支建立关系
git checkout -b [branch] 切换到新建分支
git checkout -b [branch] [commit] 指向指定commit
git checkout [commit] 指定commit新建一个临时分支
# 切换分支
git checkout [branch]
git checkout - 上一个分支
# 删除分支
git branch -d [branch-name] 删除本地分支
git push origin --delete [branch-name]
git branch -dr [remote/branch]
# 合并分支
git merge [branch]
# 建立与远程分支的关系
git branch --set-upstream [branch] [remote-branch]
撤销操作
# 撤销工作区
git checkout .
git checkout [file1] [file2]
# 撤销工作区+暂存区
git checkout -f 所有修改
# 撤销指定commit的指定文件到暂存区+工作区
git checkout [commit] [file]
# 撤销暂存区 到 工作区
git reset --mixed
# 撤销本地仓库(上一次commit)和 暂存区 , 工作区不变
git reset HEAD^
git reset [commit]
# 撤销本地仓库(上一次commit), 暂存区和工作区不变
git reset --soft HEAD^
git reset --soft [commit]
# 撤销本地仓库(上一次commit)、暂存区 和 工作区
git reset --hard HEAD^
git reset --hard [commit]
远程分支管理
# 显示所有远程仓库
git remote -v
# 添加远程仓库,并命名
git remote add [shortname] [url]
# 拉取远程仓库所有commit
git fetch [remote]
# 拉取远程仓库所有commit并合并
git pull [remote] [branch]
# 上传本地指定分支到远程仓库
git push [remote] [branch]
# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force
# 推送所有分支到远程仓库
git push [remote] --all
标签管理
# 查看tag
git tag
git show [tag]
# 新建tag
git tag [tag]
git tag [tag] [commit]
# 删除tag
git tag -d [tag] 删除本地tag
git push origin :refs/tags/[tagName] 删除远程tag
# 提交tag
git push [remote] [tag]
git push [remte] --tags
缓存区管理
# 显示缓存区信息
git stash list
git stash show [stash_id]
# 暂存区和工作区 -> 缓存区
git stash
git stash save [message]
# 缓存区(暂存区+工作区) -> 工作区
git stash apply [stash_id] 不删除stash
git stash pop [stash_id] 删除stash
# 缓存区(暂存区+工作区)-> 暂存区 + 工作区
git stash apply --index [stash_id]
git stash pop --index [stash_id]
# 删除stash
git stash drop [stash_id]
git stash clear