【GIT】说一说 Git 的常见命令和实践

文章目录

  • [git --versions](#git --versions)
  • [git init](#git init)
  • [git config](#git config)
  • [git remote](#git remote)
  • [git status](#git status)
  • [git add](#git add)
  • [git commit](#git commit)
  • [git reset](#git reset)
  • [git feach](#git feach)
  • [git push](#git push)
  • [git log](#git log)
  • [git branch](#git branch)
  • [git merge](#git merge)
  • [git checkout](#git checkout)

背景

Git 是一个非常强大的版本控制系统,用于跟踪和管理代码更改。

下面是一些 Git 的基础命令,这些命令对于日常使用 Git 进行版本控制非常关键!

git --versions

bash 复制代码
# 查看版本
$ git --versions

git init

初始化 Git 文件

例如:

bash 复制代码
# 在当前目录下初始化一个新的 Git 仓库。
$ git init 

git config

配置 Git 用户信息

语法:

bash 复制代码
# 设置全局的用户名
git config --global user.name <name>

# 设置全局的邮箱地址
git config --global user.email <email> 

例如:

bash 复制代码
# 设置全局的用户名
$ git config --global user.name "Your Name"

# 设置全局的邮箱地址
$ git config --global user.email "your.email@example.com"

# 验证设置
$ git config --global --list

git remote

列出、关联、修改、移除远程仓库

语法:

bash 复制代码
# 添加远程仓库
git remote add <remote-name> <repository-url>

# 修改远程仓库的URL
git remote set-url <remote-name> <new-repository-url>

# 移除远程仓库
git remote rm <remote-name>

例如:

bash 复制代码
# 列出远程仓库
$ git remote

# 获取每个连接远程仓库的URL,可以使用 -v 或 --verbose 选项
$ git remote -v

# 添加远程仓库
$ git remote add origin https://github.com/pro/xxxx.git

# 修改远程仓库的 URL
$ git remote set-url originhttps://github.com/new/xxxx.git

# 移除远程仓库
$ git remote rm origin

git status

查看仓库状态

例如:

bash 复制代码
# 查看当前仓库的状态,比如哪些文件被修改了但还未提交
$ git status

git add

添加文件到暂存区

语法:

bash 复制代码
# 将指定文件添加到暂存区 
git add <文件名>

例如:

bash 复制代码
# 将指定文件添加到暂存区。
$ git add index.html

# 添加当前目录下所有修改过的文件到暂存区
$ git add .

git commit

提交更改

例如:

bash 复制代码
# 将暂存区的改动提交到仓库中,并附上提交信息。
git commit -m "fix: 测试修复":

扩展:

bash 复制代码
# 修改最近一次提交的提交信息
git commit --amend -m "新的提交信息"

注意,如果你已经将这个提交推送到了远程仓库,使用git commit --amend后,你需要使用git push --force或者git push --force-with-lease来更新远程仓库中的提交。但请谨慎使用,因为这会影响所有与你共享这个分支的开发者。

git reset

语法:

复制代码
# 回退到某个版本
$ git reset <HEAD>

例如:

bash 复制代码
# 版本回退
$ git reset  b233fb7

git feach

例如:

bash 复制代码
# 从远程仓库获取最新版本但不合并
$ git fetch

##git pull

bash 复制代码
# 从远程仓库拉取最新版本并合并到本地
$ git pull

git push

语法

bash 复制代码
# 将工作区域代码推送到远程
git push 

# 删除远程分支
git push origin --delete 《分支名>

例如:

bash 复制代码
# 将本地的 new-branch 分支推送到远程仓库(如果远程仓库中不存在 new-branch 分支,则创建它)。
$ git push -u origin new-branch

git log

查看提交历史记录

例如:

bash 复制代码
# 显示 Git 仓库的提交历史
$ git log

# 简洁显示 Git 仓库的提交历史
$ git log --oneline

# 显示 Git 仓库的提交历史,但会以简短的日期格式来显示每个提交的日期 YYYY-MM-DD
$ git log --date=short

# 显示 Git 仓库的提交历史,但会以简短的日期格式来显示每个提交的日期 YYYY-MM-DDTHH:MM:SS±ZZZZ
$ git log --date=iso

git branch

语法:

bash 复制代码
# 查看本地分支
git branch

# 查看本地与远程分支
git branch -a

# 创建新分支
git branch <分支名>

# 删除本地分支
git branch -d <分支名>

git merge

语法:

bash 复制代码
# 将指定分支合并到当前分支
git merge <分支名>

例如:

bash 复制代码
# 当前分支与 master 进行合并  
git merge master

# 
git merge master --squash

git checkout

语法:

bash 复制代码
# 撤销工作区的修改,使其回到最近一次 `git commit` 或 `git add` 时的状态
git checkout -- <文件名>

# 切换到指定分支
git checkout <分支名>

# 创建并切换到新分支
git checkout -b <新分支名>
相关推荐
Flynt1 天前
Chrome给AI编程助手塞了203篇前端规范,我拆包实测:检索0.77秒是真快,扫描器有点挑食
前端·chrome·ai编程
其实防守也摸鱼1 天前
用 Claude Code 重构遗留系统:从评估到落地的完整指南
前端·网络·人工智能·chrome·安全·重构
七分辣度1 天前
GitHub/Git 常用命令及使用场景
git·github
MyBili1 天前
Chrome多配置文件实战:工作生活身份彻底隔离管理
chrome·谷歌浏览器·使用技巧·效率工具·账号隔离·多配置文件·多身份管理
慧都小项2 天前
MyEclipse Inline Diff 实用指南:逐处核对 AI 修改再提交
git·ai编程·myeclipse·java 代码审查·ai 代码修改
德育处主任2 天前
让 Claude Code 在你睡觉时接着干活:tmux、权限策略与验收清单
git·ai编程·命令行
afumu3 天前
免费 API 满地都是,为什么还有人做「让网页 AI 干活」这种东西
javascript·chrome·浏览器
afumu3 天前
在 Chrome 扩展里抓网页 AI 的工具调用:patch fetch 和看 DOM,我两条路都跑过
javascript·chrome·浏览器
Frag0ut4 天前
Chrome 2026跨端同步全攻略:多设备无缝协同实战进阶指南
chrome·谷歌浏览器·跨端同步·多设备浏览器协同·密码多端同步·chrome接力浏览·多端数据流转