关于git这一篇就够了
标题:史上最详细Git使用教程、有了他你将解决所有git问题
- 1.更新最新内容
- 2.分支常见命令
- 3.上传文件
- 4.配置用户名
- 5.已存在的文件夹上传
- 6.已存在的 Git 版本库
- 7.转移代码仓库,在不删除代码的情况下更改远程地址
- 8.git pull 每次更新都需要密码
- 9.常见异常情况
1.更新最新内容
# 拉取代码
git pull
# 从远程获取代码库
git fetch
#功能:我的覆盖别人的
git checkout --ours 文件路径
#功能:别人的覆盖我的
git checkout --theirs 文件路径
2.分支常见命令
# 查看当前属于哪个分支
git branch
# 创建分支
git branch +名称
# 删除本地分支/-D强制删除
git branch -d/-D +名称
# 切换分支
git checkout + 名称
# 创建并切换分支
git checkout -b + 名称
# 提交本地test分支作为远程的test分支
git push origin dev:dev
# 提交完编写代码提示本地连接与远程服务器没有连接,需要进行本地与服务器之间进行关联
git branch --set-upstream-to=origin/dev dev
3.上传文件
# 创建一个空的本地仓库
git init
# 将所有项目中的文件夹添加到缓存中
git add .
# 将缓存中的文件commit到git仓库中
git commit -m "first commit 项目"
# 本地仓库和远程仓库建立连接
git remote add origin "路径"
# 下载最新代码
git push origin master
4.配置用户名
# 配置用户名
git config --global user.name "Your Name"
# 配置邮箱
git config --global user.email "email@example.com"
# 可查看当前用户信息以及其他的一些信息
git config --list
# 每次git pull都需要验证用户名密码 执行完毕只需要在输入一次即可,下下次就不会有这样的问题
git config --global credential.helper store
5.已存在的文件夹上传
# 跳转文件夹
cd existing_folder
# 初始化git
git init
# 关联远程项目地址
git remote add origin 项目路径
# 添加所有更改文件
git add .
# 提交更改的文件,并且添加备注
git commit -m "Initial commit"
# 同步本地代码与服务器代码一致
git push -u origin master
6.已存在的 Git 版本库
# 跳转文件夹
cd existing_repo
# 重命名远程服务代码名称
git remote rename origin old-origin
# 关联到新的git仓库地址
git remote add origin 项目路径
# 同步 所有的远程仓库代码
git push -u origin --all
# 同步所有远程仓库的标签
git push -u origin --tags
7.转移代码仓库,在不删除代码的情况下更改远程地址
# 查看本地代码关联的git仓库地址
git remote -v
# 删除本地关联的git仓库地址
git remote rm origin
# 本地代码关联新的仓库地址
git remote add origin 新地址
# 再次查看本地代码关联的git仓库地址
git remote -v
# 切换远程分支与本地分支绑定,如果第一次没有成功需要等待一会,本地与远程关联需要一点时间
git branch --set-upstream-to=origin/new new
8.git pull 每次更新都需要密码
# 独立项目保存密码
git config credential.helper store
# 全局git保存密码
git config --global credential.helper store
# 删除全局保存密码
git config --global --unset credential.helper store
9.常见异常情况
用户认证失败,异常情况:fatal:Authentication failed for ‘http://xxx.xxx.xxx/xxx.get’
解决方案:
git config --system --unset credential.helper
拉取文件冲突,异常情况:our local changes to the following files would be overwritten by merge
解决方案
# 保存本地代码版本
git stash
# 更新代码
git pull
# 将之前版本代码弹出,冲突代码会合并,之后修改冲突代码
git stash pop