【代码管理】git使用指南(新手向)

git init # 初始化仓库
git clone URL # 克隆远程仓库
git status # 查看状态
git add . # 添加修改
git commit -m "说明" # 提交
git push # 推送
git pull # 拉取
git log # 查看提交记录
git remote -v # 查看远程仓库
git branch # 查看分支
git checkout -b dev # 创建并切换到 dev 分支
git merge dev # 合并分支

一、安装与配置

1. 安装 Git

检查是否安装成功:

bash 复制代码
git --version

2. 初始配置

配置用户名和邮箱(提交记录用来标识身份):

bash 复制代码
git config --global user.name "你的GitHub用户名" 
git config --global user.email "你的GitHub注册邮箱"

查看配置:

bash 复制代码
git config --list

如果输错了,可以删除或修改:

bash 复制代码
git config --global --unset <usr.email> # 错误配置项 
git config --global --edit # 手动编辑配置文件-推荐

二、常用 Git 操作流程

1.初始化本地仓库

bash 复制代码
git init

2.连接远程仓库

bash 复制代码
it remote add origin https://github.com/你的用户名/你的仓库名.git 

如果地址写错了:

bash 复制代码
git remote set-url origin 新地址

查看远程仓库:

bash 复制代码
git remote -v

3.常用操作

bash 复制代码
git status # 查看状态 
git add . # 添加所有修改 
git commit -m "说明" # 提交到本地仓库 
git push # 推送到远程 
git pull # 拉取远程最新代码
复制代码
第一次推送时需要绑定远程分支:
bash 复制代码
git push -u origin main

三、常见问题与解决方法

1. 远程仓库已存在

复制代码
error: remote origin already exists. 

解决:

bash 复制代码
git remote set-url origin https://github.com/xxx/xxx.git

bash 复制代码
git remote remove origin git remote add origin https://github.com/xxx/xxx.git

2. 换行符提示

bash 复制代码
warning: LF will be replaced by CRLF

这是换行符不同导致的,不影响使用。

解决方案:

bash 复制代码
git config --global core.autocrlf false # 禁止自动转换

3. 没有上游分支

bash 复制代码
fatal: The current branch main has no upstream branch.

解决:

bash 复制代码
git push -u origin main

4. 网络错误(Connection reset / Couldn't connect to server)

  • 原因:国内直连 GitHub 不稳定

  • 解决方法:

    1. 检查仓库地址 (必须是 .xxx.git 而不是 .xxx

    2. 配置代理

      bash 复制代码
      git config --global http.proxy http://127.0.0.1:7890 
      git config --global https.proxy http://127.0.0.1:7890
    3. 改用 SSH(推荐,免密、稳定):

      bash 复制代码
      ssh-keygen -t rsa -C "你的邮箱" 
      git remote set-url origin git@github.com:你的用户名/仓库名.git 
      ssh -T git@github.com
相关推荐
大卫小东(Sheldon)9 小时前
GIM 2.0 发布:真正让 AI 提交消息可定制、可控、可项目级优化
git·rust·gim
知识即是力量ol12 小时前
研发实战:Git 规范化开发全流程指南
git·gitee·github
我是一只代码狗12 小时前
idea创建分支
git
知识即是力量ol14 小时前
Git 快速入门 (实习生视角)
git·gitee·github
Dontla14 小时前
Git撤销上一次提交(撤销提交,Git回退提交)git reset、git revert
git
wdfk_prog15 小时前
解决 `git cherry-pick` 引入大量新文件的问题
大数据·git·elasticsearch
fu的博客17 小时前
Git从删库到跑路
git·gitee·github
要加油哦~17 小时前
git 报错 | husky - pre-commit hook exited with code 1 解决
git
知识即是力量ol17 小时前
Git 实战指南:从分支管理到冲突解决
git·github·源代码管理
weixin_462446231 天前
Git 本地忽略 application-dev.yml 的最佳实践:不提交 .gitignore,不影响团队协作!
git