Git远程仓库管理基础使用

安装 

Git - Downloads

官方指南

本地仓库

新建文件夹,右键打开Git:

git init //初始化

git status // 查看当前版本库状态

git add test.py // 将test.py文件加入仓库
git add . // 一次性添加当前路径下所有文件

git commit -m "information" // 提交改动信息

远程仓库

Gitee或者Github等新建仓库,获取仓库地址,如:https://gitee.com/lakuite/test.git 。

git remote add origin ​https://gitee.com/lakuite/test.git​
git pull origin master
git push -u origin master //推送本地master去origin

如果本地仓库已有过提交记录,新建了一个远程仓库,pull失败,则参考:git pull 提示错误 fatal: refusing to merge unrelated histories_每天都要努力学习的阿凯的博客-CSDN博客

有:

git pull origin master --allow-unrelated-histories

忽略文件

参考 git--指定不上传的文件夹 - 帅到要去报警 - 博客园

在仓库下新建 .gitignore 文件,添加:

target    // 忽略这个target 目录
log/*     // 忽略log下的所有文件
css/*.css // 忽略css目录下的.css文件

中文文件名乱码

参考git 中文文件名乱码_zhanlanmg的专栏-CSDN博客_git 中文文件名

git config --global core.quotepath false

返回历史仓库版本

参考:git 版本回退或者撤销上一次pull操作_阿珊和她的猫的博客-CSDN博客_git 回退上一次pull

查看历史版本,然后回退到选定版本处:

git reflog
git reset --hard <number>

<number>为历史版本中第一列的字符串。

新建分支

参考:【Git】push时error: failed to push some refs to ‘xxx.git’_想上天的小鱼的博客-CSDN博客

git checkout master                         // 切换到master分支
git checkout -b newBranch                   // 新建分支并切换到新分支newBranch
git push origin newBranch                   // 推送本地代码
git push --set-upstream origin newBranch    // 关联分支

取消每次密码输入

git config --global credential.helper store

拉取远程代码强制覆盖本地

git fetch --all && git reset --hard origin/master && git pull

重新链接仓库

当pull时报错:

可以通过重新链接仓库来解决。参考:GIT使用 | 重新关联远程仓库_透明水晶的博客-CSDN博客_git 重新连接远程仓库

git remote rm origin
git remote add origin xxx.git
git remote -v

删除仓库历史提交记录

参考:Git清除仓库所有历史记录_qq_22841387的博客-CSDN博客_git删除历史记录

使用新建分支提交,删除原分支并修改新分支名为原分支名的方法:

git checkout --orphan clean_log // 切换新分支clean_log
git add .
git commit -m "init"
git branch -D master            // 删除master分支
git branch -m master            // 修改当前分支名为master
git push -f origin master       // 强制提交

如果push时报错:

remote: GitLab: You are not allowed to force push code to a protected branch on this project.

在gitlab中找到:

Settings-->Repository-->Protected Branches-->Expand-->Unprotect: 

 等push成功后再修改回来:

更新.gitignore

在写代码的过程中更新了.gitignore文件后,更新内容可能会不起作用。

参考:.gitignore文件不起作用的解决方法 - 知乎

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

修改已提交的commit信息

参考:如何修改已提交的git commit信息_git上commit后想修改commit内容_攻城狮星河的博客-CSDN博客

git commit --amend
git log

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值