git常规操作

本地项目上传到远程git仓库

命令行工具 cd 至目标文件夹,通过此命令将该目录变成 git 可以管理的仓库

git init

文件添加至版本库,使用此命令添加至暂存区( “ . ” 表示文件夹下所有文件 )

git add .

文件提交至本地库( 引号内为提交说明 )

git commit -m 'init commit'

关联远程库( 远程库地址示例: https://gitee.com/a/b.git )

git remote rm origin // 删除本地已有仓库(如已绑定其他仓库)
git remote add origin 远程库地址

获取远程库与本地同步合并( 远程库不为空时必须做这一步,否则 git push 失败 )

git pull --rebase origin master

本地库内容推送至远程库( 本质是当前master分支推送到远程 )

git push -u origin master

拉取远程分支到本地

git init  // 新建文件夹并初始化仓库
git remote add origin 远程git地址   // 本地仓库与远程仓库建立连接
git fetch origin 远程分支名   // 远程分支拉取到本地
git checkout -b 本地分支名 origin/远程分支名   // 本地创建并切换分支
git pull origin 远程分支名   // 远程分支内容拉取到本地当前分支

版本回退

git reset --hard HEAD^  //  ^表示1个版本,^^表示2个版本,~100表示100个版本
git log -a  // 查看所有commit
git reset --hard commitid   // 回退到指定版本
git reflog  // 查看未来版本

分支未合并误删除文件找回

git log -g  // 找回之前提交的commitid
git branch recover_branch_丢失分支名 commitid  // 用这个commitid创建分支
git checkout recover_branch_丢失分支名  // 切换到当前分支,文件已恢复

创建删除分支

git checkout -b 分支名 // 创建本地分支
git push --set-upstream origin 分支名  // 推送创建远程分支(经过分支开发完并已提交)
git branch  // 查看本地分支
git branch -a  // 查看远程分支
git branch -d 本地分支  // 删除本地分支(未merge,-d 大写为 -D)
git push origin --delete 远程分支名  // 删除远程分支

修改远程分支名

git branch -m old_branch new_branch  // 修改本地old_branch名称为new_branch
git push origin new_branch  // 推送添加远程分支
git push origin --delete 远程分支名 // 删除远程分支

修改本地当前分支关联的远程分支

git branch --set-upstream-to origin/远程分支名

创建删除标签

git checkout 分支名  // 切换到当前分支
git tag 标签名  // 默认标签打在最新提交的commit上
git tag  // 查看所有标签
git log --pretty=oneline --abbrev-commit  // 查看历史的commit id
git tag 标签名 commitid  // 在对应的commitid上提交打标签
git show 标签名  // 查看标签信息
git tag -a 标签名 -m "说明文字" commitid  // 创建带有说明的标签(-a 指定标签名 -m 指定说明文字)
git push origin 标签名  // 推送标签至远程
git tag -d 标签名  // 删除本地标签
git push origin --delete 标签名  // 删除远程标签

warning

The file will have its original line endings in your working directory

解决方法:git config –global core.autocrlf false

原因:路径中存在 / 的符号转义问题,false就是不转换符号默认是true,相当于把路径的 / 符号进行转义,git add 的时候就有问题

速览:

git status: 查看当前所在分支及仓库状态
git checkout branch_name: 切换到 branch_name 分支
git checkout -b branch_name: 新建 branch_name 分支
git pull origin branch_name: 检出 branch_name 最新代码
git add file_name: 把 file_name 加入暂存区
git commit -m "description": 提交暂存区文件到本地仓库
git push origin branch_name: 把本地仓库代码提交到远程仓库
git merge branch_name: 把 branch_name 合并到当前分支
git merge --squash branch_name: 把多个 commit 合并成一个 commit 再合并
git log: 查看日志
git diff branch_name1 branch_name2: 查看 branch_name1 和 branch_name2 分支的差别
git reset --hard head 分支撤销merging状态

 

posted @ 2019-04-03 16:10  _senjer  阅读(379)  评论(0)    收藏  举报