Git初学

三步走

Step1. 在 GitHub 创建一个新的 repository

Step2. 复制创建好的 repository 仓库地址,git clone 到本地,将所有的内容放到该项目文件夹中

git clone url

Step3. 管理项目仓库

首先将相关信息添加到 .git/config 文件中,主要是

[user]
	user = xxx
	email = xxx@xxx.com

然后使用以下命令完成提交:

git status
git add [filename1 filename2 dir1 dir2][.]
git commit -m "your comment of this commit"
git push -u origin your_branch_name([master][main][gh-pages][dev]...)  # 这里根据自己是否修改默认主分支名来确定,GitHub 现在默认的主分支名为 main

相关问题及解决办法

参考链接1:https://blog.csdn.net/mingjie1212/article/details/51689606

如果 .gitignore 不生效,将 cache 删除,重新提交。

git rm -r --cached .
git add .
# 之后操作同上

个人笔记(请不用查看后续内容!)

常用命令

git init  # 创建新的文件夹作为项目的路径,然后初始化本地项目
git config --global user.name "myname"  # 配置用户名
git config --global user.email "name@email.com"  # 配置邮箱
git remote add origion 远程仓库的 git 地址(ssh无密码验证,选这个/https)  # 绑定本地和远程仓库
git config --global credential.helper store  # 设置证书
git pull  # 拉取远程仓库的变化来同步本地状态
git add  # 同步本地仓库的变化到本地缓存区
git commit  # 确认本地缓存区的内容,可以准备 push
git push  # 提交本地仓库到远程仓库
git status  # 确定本地缓存区的状态

vscode 上传本地项目到 GitHub:https://www.cxyzjd.com/article/Le___Le/103585617

将远程项目合并到本地

mkdir test; cd test

git init
git remote -v
git remote add origin https://github.com/blainetse/gittest.git
git pull origin [main][master]  # 下载的 git 初始化本地仓库时默认使用的主分支为master,为保持一致,GitHub 也采用这个名字
# 如果失败 SSL... errno 10054
git config --global http.sslVerify "false"
git pull origin [main][master]

这个时候一般都能成功 pull 到本地仓库中了。以上所有的操作使用 git clone https://github.com/blainetse/gittest.git 一步到位!

之后就是在本地仓库中进行文件的管理。基本原理是这样:

  1. 本地创建一个空的文件夹,用于存放项目,然后使用 git init 初始化,之后就能通过本地安装的 git 版本控制进行项目管理了。
  2. pull 拉取代码托管中心的项目,使用 git clone 或者以上比较繁琐的那几步操作,将数据下载到本地。
  3. 在本地对代码进行修改,本地 git 管理,将需要同步上传的数据通过 git add file1 --> git add file2 --> git add ... --> git commit -m "comment" --> git push origin [main][master].

当然,以上只是最简单的操作,如果要实现更加复杂的功能,待后续…

不过这里最基本的还有一个概念 branch 分支,可以创建多个子分支,但是主分支只能有一个,即 [main][master]

通过 git checkout -b sub_branch 创建一个新的子分支并在该进入该分支,所有的操作只记录在当前的分支下面。

通过 git checkout branch_name 切换到相应的分支,可以将多个分支合并,对比等操作,最后将所有的分支信息汇总到 [main][master],通过统一的 git commit 进行"打包",将该次修改作为一个阶段进行一个状态点,上传保存到代码托管处(保留了相关的修改信息)git push origin [main][master]

下面解决问题中,有一篇提到了服务器与本地 git 的联系和不同,两者之间基本上没有太大的联系,总的来说,按照个人理解,事先在本地进行相关修改(保留了这些修改的过程以及相应的记录点),然后将这些所有的信息传递到代码托管出,托管处仅仅实现一个代存放的功能。

注意事项

无法上传同步的两大原因

  1. 主分支的名字很重要!!!

Git 中默认的分支为 masterGitHub 2020 年之后就将其更改为了 main,如果不一致,很容易出现找不到远程仓库,无法上传同步的错误!

  1. 证书验证确定是一个大问题,主要出现在提交到 gihub 的过程中出现,即将本地的改动同步到代码托管中心时出现的问题。
git push origin master

报的错误里面有 SSL... 和一个错误代码号,解决方法是:

git config --global http.sslVerify "false"

这里需要强调一点的是,要在新开的一个终端里面输入该命令,否则只针对当前环境生效一次,当再次进入时还是会报错,具体原因不是很清楚。

解决了以上问题后,基本上就没有什么大的障碍了,参考链接放在这里。

有关问题解决有参考价值的链接

fatal: unable to access 'https://github.com/blainetse/gittest.git/': OpenSSL SSL_read: Connection was reset, errno 10054

有关理论指导的参考链接

  git config --global credential.helper [cache][store]
  # 使用 cache 的方式
  git config --global credential.helper 'store --file ~/.my-credentials'

使用 cache 配置文件的话,在里面添加以下内容:

[credential]
     helper = store --file /home/guest/XieBailian/.git-credentials  # 缓存路径
     helper = cache --timeout 30000  # 有效时间

附录(仅个人回顾)

Stage1

git config --global http.sslVerify "false"

git init  // 创建新的文件夹作为项目的路径,然后初始化本地项目
git config --global user.name "myname"  // 配置用户名
git config --global user.email "name@email.com"  // 配置邮箱
git remote add origin code@github.git  // 绑定本地和远程仓库
git pull  // 拉取远程仓库的变化来同步本地状态
git add  // 同步本地仓库的变化到本地缓存区
git commit  // 确认本地缓存区的内容,可以准备 push
git push  // 提交本地仓库到远程仓库
git status  // 确定本地缓存区的状态

注意:主分支在 2020 年之后由 master 更改为了 main !!! 如果需要更改为之前的、或者自定义主分支,到 settings 里面的 Respomsitories 进行修改即可。

Stage2

git init
git remote add origin https://github.com/blainetse/mytest.git

如果要重新修改 remote origin,使用

git remote rm origin

删除指定的主机,通过

git remote -v

查看当前连接状态,然后重新使用

git remote add origion https://github.com/blainetse/mytest.git

即可。

以上这一段主要是修改 .git/config 文件中的配置。

git pull origin main

如果出现:

fatal: unable to access 'https://github.com/blainetse/mytest.git/': OpenSSL SSL_read: Connection was reset, errno 10054

解决方法:https://blog.csdn.net/weixin_43945983/article/details/110882074

git config --global http.sslverify "false"
git add filename  # 将要提交修改的文件放到 Storage
git commit -m "注释"  # 将修改正式提交,同步到 GitHub
git push -u origin main
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值