git实践

代理

# 全局, 只代理github
git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy http://127.0.0.1:7890
# 全局, 代理所有
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy https://127.0.0.1:7890

# 查看代理配置
git config --list 
git config --global --get http.https://github.com.proxy
git config --global --get https.https://github.com.proxy
git config --global --get http.proxy
git config --global --get https.proxy

# 删除代理配置
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
git config --global --unset http.proxy
git config --global --unset https.proxy

新项目

#初始化本地仓库
git init
#添加到暂存区和索引区
git add .
#配置
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
#提交到本地库
git commit -m'comment'
#设置主分支
git branch -M main
#添加远程仓库
git remote add origin https://gitee.com/xcrj/cloud2020.git
#推送到远程main分支
git push -u origin main

分支切换

已经位于需要切换的基础分支上

# 先位于基础分支,切到dev_xcrj
git checkout -b dev_xcrj
# 推送本地分支到远程仓库,将在远程仓库中创建分支
git push -u origin dev_xcrj

分支设置

代码管理平台

  • 默认合并分支:例如,dev_xcrj, dev_xcrj1, dev_xcrj2 都默认合并到dev分支
  • 保护分支:dev分支,管理员角色可push,管理员角色可合并,合并前需要通过代码评审
  • 保护分支:例如,master 不可push+管理员可merge,dev管理员可push+管理员可merge

提交修改的文件

git add 添加 修改(M) 增加(A) 的文件,不能add 删除(D)的文件
直接commit

切换远程分支

方式一:自动关联

git checkout xcrj

方式二:手动关联

# 从本地分支切换到新分支
git checkout -b xcrj
# 关联新本地分支和远程分支
git branch --set-upstream-to=origin/xcrj_mongo xcrj_mongo
# 拉取最新的代码
git pull

切换远程仓库

# pull原仓库主分支最新代码
git pull
# 更换远程仓库地址
git remote set-url origin http://x.x.x.x:8080/wgms/xcrj-sys.git
# 推送代码到远程仓库主分支
git push --set-upstream origin dev
# 本地从dev切换新分支xcrj
git checkout -b xcrj
# 推送本地分支到远程仓库
git push -u origin xcrj

多分支开发

  • merge模式开发

原则

  • 只在自己的分支上开发
  • 只commit自己的代码
  • 只提交小粒度的代码

总结

# 克隆代码到本地仓库
git clone http://xxx.git
# 查看本地分支
git branch
# 切换到本地主分支
git checkout dev
# 下拉代码到主分支,保证主分支是最新代码
git pull origin dev
# 从本地主分支切换自己分支
git checkout -b dev_xcrj
# 查看修改的内容,确认修改的内容
git status -s
# 所有文件,git status -s 查出,添加到暂存区(索引区)
git add .
# 指定文件,git status -s 得出
git add 文件全路径
# 提交到本地分支
git commit -m'本次提交注释'
# 第一次push,推送并创建远程分支
git push --set-upstream origin dev_xcrj
# 非第一次push,推送
git push

步骤

  1. 创建本地自己的分支
#克隆代码到本地仓库
git clone http://xxx.git
#查看本地分支
git branch
#切换到本地主分支
git checkout dev
#下拉代码到主分支,保证主分支是最新代码
git pull origin dev
#从本地主分支切换自己分支
git checkout -b dev_xcrj
#将本地自己分支推送到远程仓库
git push --set-upstream origin dev_xcrj

  1. 完成1次开发提交代码
#查看本地分支,是否在自己分支
git branch
#查看更新,是否是自己修改的文件
git status
#下拉远程主分支代码,保证本地自己分支最新
git pull origin dev
##没有冲突,有冲突需要先解决冲突
#添加到暂存区(索引区)
git add 需要提交的文件
#提交到本地分支
git commit -m'comment'
#提交到远程自己分支
git push
  1. 提交merge-request
    mr

完成功能开发提交代码

原则

  • 不同的开发人员开发不同的模块/功能,避免冲突
  • 不同的开发人员有自己的开发分支,有相同的主分支
  • 每次commit 1个完整的,简单的,经过详细测试的功能点
#查看本地分支,是否在自己分支
git branch
#查看更新,是否是自己修改的文件
git status -s
#查看差异,是否是自己修改的内容,在工作区-暂存区-本地分支-远程分支之间比较差异
git diff
#下拉远程主分支代码,保证本地自己分支最新
git pull origin dev

##没有冲突
#添加到暂存区(索引区)
git add .
#提交到本地分支
git commit -m""
#提交到远程自己分支
git push

##有冲突需要解决冲突
#解决冲突

#添加到暂存区(索引区)
git add .
#提交到本地分支
git commit -m""
#提交到远程分支
git push

脏工作目录

将工作空间和暂存区中的修改放到脏工作目录

#保留在脏工作区
git stash save 'comment'
#弹出栈顶内容
git stash pop

错误add

# add之后,全部重置
git reset HEAD
# add之后,重置部分文件
git reset HEAD fileName

错误commit

# 回到上1个结点
git reset HEAD^

合并commit结点

总结:

  • rebase
  • squash
  • comment

已知:有commit 1, commit 2, commit 3共3次提交,现在需要将commit 2 commit 3合并为1个提交

$ git log --oneline
bd71466 (HEAD -> main) commit 3
7079afd commit 2
9dc2c86 (origin/main, origin/HEAD) commit 1

操作

# 1. git界面
# rebase 重建 
# git rebase -i <最近的不需要合并提交>,本次测试是commit 1
git rebase -i 9dc2c86

# 2. vi界面
# -i进入输入模式》修改最下面1个pick为squash
# pick表示使用这个commit,squash表示使用这个commit但合并到以前的commit
# pick-使用commit 2》squash-合并commit 3到以前的commit
pick 7079afd commit 2
squash bd71466 commit 3
# esc进入命令模式》:wq,vi保存退出
:wq

# 3. vi界面
# -i进入输入模式》为合并的commit填写comment信息
your commit message

# 4. push
# 如果已经提交到远程分支 -f=force
git push -f
# 如果没有提交到远程分支
git push

git rebase -i 9dc2c86 命令结果图:
在这里插入图片描述
esc》:wq保存退出后结果图
在这里插入图片描述

为合并的commit填写commit comment(注释)
在这里插入图片描述
rebase结果图
在这里插入图片描述

删除远程分支commit

#本地分支reset
git reset HEAD^
#强制推到远程分支 -f=force 
git push -f
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值