Git配置、使用、上传大文件

目录

一、下载及安装git程序

二、git相关环境配置

三、创建存储库

四、github上传大文件


一、下载及安装git程序

  1. git官网下载对应自己系统的git应用程序,官网:https://git-scm.com/download/win
  2. 双击下载的git.exe进行安装,安装路径可以自行更改,在其中一个选项中可以选择Use Git and optional Unix tools from the Command Prompt这个选项,也可以选择默认方式。
  3. 安装完成后,在桌面中点击右键出现Git GUI here和Git Bash Here,表示安装成功。

注:win+R,输入cmd,打开命令行窗口,输入git也可检查是否安装成功(需要更改2中选项)

二、git相关环境配置

注:两种方式  ①鼠标右击选择Git Bash Here   ②cmd命令行窗口

1、在GitHub官网中注册一个GitHub的账号,官网地址:https://github.com/

2、为git配置用户名和密码,--global中存储了提交用户的name和email,手动设置可以使用如下指令

name配置:

git config --global user.name 注册GitHub的用户名

email配置:

git config --global user.email  注册GitHub使用的邮箱 

3、检查配置是否正确,输入以下命令,出现user.name=注册GitHub的用户名   user.email=注册GitHub使用的邮箱

git config --list

4、利用git生成ssh协议,输入:

ssh-keygen -t rsa

三次回车之后就会生成.ssh文件夹,路径如图所示

5、在GitHub中建立连接,具体是访问 https://github.com/settings/ssh/new,title内容任起,key内容复制id_rsa.pub文件中的内容,可用记事本打开进行复制。

 

6、最后检验是否完成连接可以在Git Bash中输入:

ssh -T [email protected]

显示以下内容表示连接成功:

三、创建存储库并上传代码

1. 创建远程仓库(例如 GitHub)

  1. 登录到你的 GitHub 账户

  2. 点击右上角的 "+" 按钮,然后选择 "New repository"。

  3. 填写仓库信息

    • Repository name: 输入仓库名称(例如:test)。
    • Description (可选): 输入仓库描述。
    • Public or Private: 选择仓库是公开还是私有。
  4. 点击 "Create repository" 按钮。

2 win+R, 输入cmd, cd进入需要同步到GitHub上的文件所在路径(例如cd C:\Users\Lenovo\Desktop\test)。

# 新建一个REMADE.MD文件
echo "# text" >> README.md

# 在当前目录,初始化一个新的Git仓库
git init
# 或者,新建一个目录,并将其作为git代码库
git init [project_name]

# 添加指定文件: git add [file1] [file2] ...
git add README.md
# 或者,添加当前目录,及子目录: 
git add .

# 提交代码,并添加提交时的信息: git commit -m ["描述你的提交"]
git commit -m "first commit"

# 添加到名为test的远程仓库  git remote add origin [email protected]:[user.name]/test.git
git remote add origin [email protected]:[user.name]/test.git

# 新建一个名为main的分支
git branch -M main

# 推送更改到远程仓库的main分支: git push -u origin [分支名]
git push -u origin main

更新代码

git remote add origin [email protected]:[user.name]/test.git
git add .
git commit -m "up-to-date"
git push -u origin [which_master]

四、github上传大文件

注:先把 .gitattributes 文件push到GitHub远程仓库

1 安装Git LFS扩展

sudo apt install curl
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt install git-lfs

2 检验安装是否成功

git lfs install

3 track 大文件

git lfs track <large_file>

 注:运行git lfs track <large_file>命令后生成 .gitattributes 文件,先将.gitattributes 文件push到GitHub远程仓库。

git add .gitattributes
git commit -m ''
git push origin main

4 按照正常顺序添加、提交、推送

git add <large_file>
git commit -m ''
git push origin main