一篇了解 Git 使用方法

一:安装

cpp 复制代码
Windows下载安装:
https://git-scm.com/

Linux下载安装:
sudo apt update
sudo apt install git

安装完毕之后第一次使用会设置基本配置:

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

// 查看配置
git config --list

二:git 基本核心命令

cpp 复制代码
# 克隆仓库到本地
git clone <repo_url>
 
##### Git 文件有三个主要区域:工作区、暂存区、仓库  #####
# 查看文件状态
git status
# 将文件的修改从​​工作区​​添加到​​暂存区​​
git add <file>
# 将​​暂存区​​的文件快照永久保存到​​仓库​​的历史记录中
git commit -m "提交信息"
# 将代码提交到仓库
git push

# 查看提交历史
git log
# 查看工作区和暂存区的差异
git diff

### 撤销与回退 ####
# 丢弃工作区的修改​​,恢复到最近一次 git commit或 git add时的状态。
git restore <file>
# 将文件从​​暂存区​​撤出,但保留工作区的修改
git restore --staged <file>

三:分支管理操作

cpp 复制代码
########### 分支管理 ###########
# 1. 查看所有本地分支
git branch

# 2. 查看远程仓库所有分支
git branch -r

# 3. 创建一个新分支
git branch <branch_name>

# 4. 切换到指定分支
git switch <branch_name>

# 5. 创建并切换到新分支
git switch -c <new_branch_name>

# 6. 将指定分支合并到​​当前所在分支
git merge <branch_name>

# 7. 删除一个已合并的分支
git branch -d <branch_name>

# 8. 强制删除一个分支
git branch -D <branch_name>

四:其他 git 功

cpp 复制代码
# 从远程拉取更新并合并到本地
git pull

# 将本地分支推送到远程仓库
git push -u origin <branch_name>

# 获取指定分支
# 1.创建并切换到本地 zsl 分支,并将其与远程的 origin/zsl 分支关联
git switch -c zsl origin/zsl
# 2.克隆时直接指定分支
git clone -b zsl <您的仓库URL>
相关推荐
Unity粉末状在校生13 小时前
Git解决fatal: Could not read from remote repository.的问题
git
少年攻城狮14 小时前
Obsidian系列---【如何使用obsidian同步到git?】
git
do better myself16 小时前
网站源码如何部署和加入GIT仓库的
git
爱学英语的程序员18 小时前
Git 提交 LF will be replaced by CRLF the next time Git touches it 报错
git
qq_3391911419 小时前
服务器git pull每次都要输入密码,linux 设置git登录,linux设置git只输入一次账户密码
git
一颗小行星!1 天前
快速理解 Git submodule
git
A-Jie-Y1 天前
Git基础-核心概念与常用命令
git
夜珀1 天前
Git基础修炼手册:在AtomGit上玩转版本控制
git
golang学习记1 天前
Zed IDE官宣新招:Git Graph 正式支持!
ide·git
要记得喝水1 天前
适用于 Git Bash 的脚本,批量提交和推送多个仓库的修改
git·elasticsearch·bash