Git 相关操作整理
基础配置
git config --global user.name "yourname"
git config --global user.email "your@example.com"
git config credential.helper store //当前仓库配置
git config --global credential.helper store //全局配置
//执行之后会在用户主目录下的.gitconfig文件中多加 helper = store
//windows10 下当前用户路径:%USERPROFILE%
git credential-manager uninstall //把项目的用户名和密码删掉
基本操作
git init //为当前项目创建一个本地仓库(即需要事先有一个创建好的项目)
git status //查看git的状态
git add . //添加所有文件到“Stage”中
git add ./ //添加当前目录下的所有到“Stage”中
git commit //提提交“Stage”中的内容到本地仓库中
git clone 远端git地址 //默认克隆
git clone 远端git地址 文件夹地址(可选) //默认克隆到指定文件夹
git clone -b 远端分支名 远端地址 //克隆指定分支
git clone -b 远端分支名 远端地址 文件夹地址(可选) //克隆指定分支到指定文件夹
git pull
git pull 远端链接名 远端分支名
git pull origin master --allow-unrelated-histories //如果本地与远程是两个独立的仓库,则在pull时需要指定其“允许无关的日志合并”,否则会保存
git push -u origin master //如果是第一个提交到远程
git push
git push 远端链接名 远端分支名
git push 远端链接名 远端分支名 -f // 如果第一次推不上去代码,可以使用强推的方式
git branch //显示本地所有分支
git branch 新分支名 //新建分支
git branch -D 分支名 //删除指定分支
git checkout 分支名 //切换分支
git checkout -b 分支名 //新建并切换分支
git checkout -b 本地分支名 远端链接名/远端分支名 //新建分支拉取远程分支,并切换分支
git checkout --track origin/远程分支名
git checkout -t origin/远程分支名
设置远端地址
方式一:增加多个远端链接
git remote add origin2 地址2 // origin2可以自定义
git remote rm origin2 //删除远端地址
方式二:给一个远端链接,添加多个url
注意:这种方式,一个push推送两个地址
git remote set-url --add origin 地址 //给origin添加一个远程push地址,这样一次push就能同时push到两个地址上面
git remote -v //查看是否多了一条push地址(这个可不执行)
git push origin master -f // 如果第一次推不上去代码,可以使用强推的方式
git remote set-url --delete origin 地址
进阶操作
进阶操作
git branch -r //查看远程分支
git fetch //获取远程所有分支
git fetch [repo] [remote_branch_name]:[local_branch_name] //git fetch origin TargetBranch:tempBranch 从远程仓orgin仓的TargetBranch分支下载到本地,并新建一个tempBranch分支
git远程分支强制覆盖本地分支
**方式一:**reset --hard 参数
git fetch --all
git reset --hard origin/dev (这里dev要修改为对应的分支名)
git pull origin dev
**方式二:**pull --force参数
git pull --force <远程主机名> <远程分支名>:<本地分支名>
//git pull --force origin dev:dev
git 凭证存储 credential helper配置
//删除 manager/store配置(全局)
git config --global --unset credential.helper store
//或者
git config --global --unset credential.helper manager
//添加manager/store配置(全局)
git config --global credential.helper store
//或者
git config --global credential.helper manager
相关网站
License:
CC BY 4.0