Git的使用教程
一、Git 安装与配置
1. 安装
Git sudo apt install git
下载地址:https://git-scm.com/download/win
2. 基本配置
git config --global user.name "Your Name"
git config--global user.email "your.email@example.com"
git config --global core.editor "vim" # 设置默认编辑器
3. 查看配置
git config --list
二、Git 基本操作
1. 创建仓库
初始化新仓库 git init
克隆现有仓库 git clone https://github.com/user/repo.git
2. 添加与提交
添加文件到暂存区 git add filename # 添加单个文件
git add . # 添加所有修改
git add *.c # 添加所有.c文件
提交到本地仓库
git commit -m "描述信息"
3. 查看状态
git status # 查看当前状态
git status -s # 简洁状态显示
4. 查看历史
git log # 查看完整历史
git log --oneline # 简洁历史
git log -p # 带差异的历史
5. 版本回退
git reset --hard HEAD^ #回到上一个版本
git reset HEAD #回到上一个版本
git reset --hard 754c40d # 重置到初始提交 754c40d为版本号

git push --force origin master # 强制推送到远程
6. 覆盖
git push --force origin master #状态覆盖远程仓库(丢弃远程仓库的所有内容)
三、远程仓库操作
1. 添加远程仓库
git remote add origin https://github.com/user/repo.git
git remote set-url origin https://github.com/user/repo.git #修改远程仓库
2. 推送与拉取
git push origin main # 推送本地分支到远程
git push origin master # 推送本地分支到远程
git pull origin main # 拉取远程更新
git fetch origin # 获取远程更新但不合并
3. 查看远程
git remote -v # 查看远程仓库信息
4.推荐使用 Git 凭据存储:
git config --global credential.helper store
                    
                
                
            
        
浙公网安备 33010602011771号