1.Windows 安装

1.1.安装Git客户端

下载: https://git-scm.com/install/windows

安装: 一直 “下一步” 按需修改配置

1.2.安装Git小乌龟

下载: https://download.tortoisegit.org/tgit/2.4.0.0/TortoiseGit-2.4.0.2-64bit.msi

安装: 一直 “下一步” 按需修改配置

2.Centos 安装

# 1、安装Git客户端
yum install git -y
# 2、验证安装成功
git --version
# 3、查询安装位置
whereis git

3.Git 基本命令

3.1.基本命令
git clone            # 项目地址 克隆项目
git branch           # 列出本地分支  
git branch - a       # 列出所有分支  
git checkout -b b1.0.0 origin/b1.0.0  #检出并创建分支
3.2.项目导入
# 1、(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库
git init

# 2、设置用户名字
git config --global user.name "张三"
git config --global user.email "zhangsan@qq.com"

# 3、添加版本库
# 使用命令 git add .添加到暂存区里面去,不要忘记后面	   的小数点“.”,意为添加文件夹下的所有文件
git add .

# 4、提交到仓库
# 用命令 git commit告诉Git,把文件提交到仓库。引号内为提交说明
git commit -m 'init'

# 5、关联远程库
git remote add origin 你的远程库地址
# 如:git remote add origin 项目get地址

# 6、获取远程库与本地同步合并
# 如果远程库不为空必须做这一步,否则后面的提交会失败
git pull --rebase origin master

# 7、本地库推送到远程
# 使用 git push命令,实际上是把当前分支master推送到远程。
# 执行此命令后会要求输入用户名、密码,验证通过后即开始上传。
git push -u origin master

# 8、状态查询命令
git status
3.3.异常处理
# .gitignore规则不生效的解决办法
# 把某些目录或文件加入忽略规则,按照上述方法定义后发现并未生效,原因是.gitignore只能忽略那些原来没有被追踪的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未被追踪状态),然后再提交:
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
git config --global core.autocrlf true
3.4.SSH方式免密
3.4.1.生成秘钥
ssh-keygen -t rsa -C "zhangsan@qq.com"

# 一直Enter并记住生成的地址
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
3.4.2.获取 SSHkey
cd .ssh
ls
# 显示:id_rsa  id_rsa.pub
# 把显示的内容复制到GitHub的SSHkey配置即可
cat id_rsa.pub
3.4.3.测试免密
ssh -T git@github.com

The authenticity of host 'github.com (127.0.0.1)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,127.0.0.1' (RSA) to the list of known hosts.
Hi megoc! You've successfully authenticated, but GitHub does not provide shell access.
3.5.HTTP方式免密

说明:%HOME%,Windows一般为C:\users\Administrator,Window中不允许直接创建以"."开头的文件,借助 git bash 进行。

# 1、进入%HOME%目录
cd ~/

# 2、创建并编辑 .git-credentials 文件
vim .git-credentials
# https://{username}:{password}@github.com(登录账号,邮箱和密码)
# 例如:https://zhangsan:123456@gitee.com

# 3、执行配置命令
git config --global credential.helper store

# 4、查看配置结果
cat .gitconfig
# 内容:[credential] helper = store
Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐