Git学习
目录
- 一.什么是Git
- 二.Git有什么用
- 三.怎么使用Git
- 4.本地代码上传git步骤
- 问题解决
- 1.(master|REBASE 1/1)
- 2. Updates were rejected because the remote contains work that you do not have locally.
- 3.error: remote origin already exists.
- 4.Updates were rejected because the tip of your current branch is behind its remote counterpart.
- 5.error: failed to push some refs to 'XXX'
- 6.合并分支如何解决冲突[master|merge]
- 鸣谢
一.什么是Git
1.Git是目前世界上最先进的分布式版本控制系统
二.Git有什么用
- 多人协作开发代码
- 可以回退到历史版本
三.怎么使用Git
1.电脑安装Git,仓库可以使用Github或者Gitee
2.了解工作区间
-
Workspace:工作区,就是你平时存放项目代码的地方
-
Index / Stage:暂存区,用于临时存放你的改动,事实上它只是一个文件,保存即将提交到文件列表信息
-
Repository:仓库区(或本地仓库),就是安全存放数据的位置,这里面有你提交到所有版本的数据。其中HEAD指向最新放入仓库的版本
-
Remote:远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑用于远程数据交换
3.git管理的3种状态
- 已修改(modified)
- 已暂存(staged)
- 已提交(committed)
4.基本语法
4.1 创建全新的仓库,需要用GIT管理的项目的根目录执行:
git init
4.2 克隆远程仓库
git clone [url]
4.3
#查看指定文件状态
git status [filename]
#查看所有文件状态
git status
# git add . 添加所有文件到暂存区
# git commit -m "消息内容" 提交暂存区中的内容到本地仓库 -m 提交信息
4.4 有些时候我们不想把某些文件纳入版本控制中,比如数据库文件,临时文件,设计文件等
在主目录下建立".gitignore"文件,此文件有如下规则:
#为注释
*.txt #忽略所有 .txt结尾的文件,这样的话上传就不会被选中!
!lib.txt #但lib.txt除外
/temp #仅忽略项目根目录下的TODO文件,不包括其它目录temp
build/ #忽略build/目录下的所有文件
doc/*.txt #会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
4.5 git分支中常用命令
git branch #列出所有本地分支
git branch -r #列出远程所有分支
git branch [branch-name] #新建一个分支但依然停留再当前分支
git checkout -b [branch] #新建一个分支并切换到该分支
git merge [branch] # 合并指定分支到当前分支
合并分支步骤:
1.切换到要合并的分支上 git checkout [分支名]
2.执行marage git marge [分支名]
git branch -d [branch-name] #删除分支
4.本地代码上传git步骤
git init
git add .
git commit - m ""
git remote add origin 地址
git push -u origin master
问题解决
1.(master|REBASE 1/1)
git rebase --abort
2. Updates were rejected because the remote contains work that you do not have locally.
git pull --rebase origin master
3.error: remote origin already exists.
git remote -v
git remote rm origin
4.Updates were rejected because the tip of your current branch is behind its remote counterpart.
git push -f origin master
5.error: failed to push some refs to 'XXX'
解释:远程仓库和本地仓库存在差异
git pull --rebase origin master
6.合并分支如何解决冲突[master|merge]
1.编辑文件,删除特殊符号
2.修改文件,保留自己想要的部分
3.git add [文件名]
4.git commit -m "xxx"

浙公网安备 33010602011771号