git 基本操作
配置用户信息
git config --global user.email "dxj1718874198@gmail.com"
git config --global user.name "YongKJ"
初始化本地 Git 仓库
git init
查看 Git 仓库状态
git status
git status -s #得到一种更为紧凑的格式输出
文件添加到暂存区
git add file1
git add -A # 提交所有变化
git add -u # 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add . # 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
git add * # 会忽略.gitignore把任何文件都加入
将本地暂存的修改提交到版本库
git commit -m "add file1" file1
git commit -m '提交信息' # -m 参数是输入提交信息
git commit -a -m '提交信息' # -a 参数可以把还没有执行add命令的修改一起提交
查看文件的修改内容
git diff file1
查看git仓库提交日志
git log
git log --stat # 在git log 的基础上输出文件增删改的统计数据
git log --oneline # 简化git log的默认的输出,仅仅输出commit hash 前7个字符串和commit
查看所有分支的所有操作记录
git reflog # 包括已经被删除的 commit 记录和 reset 的操作
丢弃工作区的修改
git checkout -- file1
撤销暂存区的修改(unstage),重新放回工作区
git reset HEAD
版本回退
git reflog
git reset --hard ID号码
误删,还原
git checkout file1
删除版本库中文件
git rm file1
git commit -m "del file1"
创建分支
git branch <branch_name>
查看分支
git branch # 查看所有分支
git branch -v # 查看所有分支详情
切换分支
git checkout <branch_name> # 切换到指定分支
git checkout -b <branch_name> # 创建并切换到指定分支
删除分支
git branch -d <branch_name> # 删除一个干净的分支(即相对当前分支而言该分支没有新的提交记录)
git branch -D <branch_name> # 强制删除一个分支,该分支有没有合并到当前分支的提交记录
注意:删除分支前都需要先切换到其他分支才能进行删除操作
分支恢复
git reflog # 查找该分支指向的commitId
git branch <branch_name> <hash_val> # 根据指定commit创建新分支
重命名分支
git branch -m <branch_name> newname
分支合并
git merge <branch_name> # 将指定分支合并到当前分支,如果两个分支没有产生分叉情况,那么会进行快速合并,即fast-forward方式
分支合并细节
git merge --no-ff -m "msg" <branch_name> # 合并分支时禁用Fast forward模式
分支暂存
git stash # 将工作暂存
git stash list # 所有的暂存状态
从暂存区中恢复:
- 先恢复,而后再删除暂存
git stash apply
git stash drop
- 恢复的同时也将stash内容删除
git stash pop
本地仓库推送到远程仓库
git remote add origin 远程仓库地址 # 将本地仓库和远程仓库进行关联
git push -u origin master # 把本地仓库的项目推送到远程仓, 第一次推送
git push origin master # 第一次推送后,直接使用该命令即可推送修改
git push origin 本地分支:远端希望创建的分支
git push origin master:my_remote_new_branch # 远端即可创建新的分支my_remote_new_branch,提交本地修改
配置远程仓库地址
git remote -v # 查看远程仓库地址命令
git remote set-url origin url # 直接修改远程仓库地址
git remote rm origin # 删除本地远程仓库地址
git remote add origin url # 然后添加新的仓库地址
配置访问代理地址
git config --global http.proxy 'http://127.0.0.1:58591'
git config --global https.proxy 'socks5://127.0.0.1:51837'
git config --global --unset http.proxy
git config --global --unset https.proxy
克隆远程仓库
git clone 远程仓库地址 # 从远程服务器克隆一个一模一样的版本库到本地,复制的是整个版本库
获取最新版本远程仓库
git fetch
git pull # 从远程获取最新版本并merge(合并)到本地,git pull = git fetch + git merge,git fetch更安全一些
清理本地仓库(以Hexo静态博客为例)
# 由于现在的.git文件夹里累积了太多辣鸡数据,甚至已经超过博客本身的大小了,于是打算从头开始重新部署一下博客的所有静态资源。
git checkout --orphan new_branch # 新建空白分枝
npm run build # 生成静态文件
git add . # 添加文件到暂存区
git commit -m '提交信息' # 提交分枝更改
git branch -D master # 删除旧主分枝
git branch -m master # 改名为主分枝
git push -f origin master # 强制推至上游
# 干净的博客出现了!