【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 "[email protected]"

# 验证设置
$ 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 <新分支名>
相关推荐
belldeep2 小时前
如何阅读、学习 Git 核心源代码 ?
git·学习·源代码
我不是秃头sheep3 小时前
Git安装教程及常用命令
git
T0uken5 小时前
【Python】UV:单脚本依赖管理
chrome·python·uv
sduwcgg11 小时前
git经验
git
麻雀无能为力11 小时前
git的使用
git
算法歌者14 小时前
Visual Studio 项目 .gitignore 文件指南
git·visual studio
江边垂钓者14 小时前
git cherry-pick和git stash命令详解
git
Lw老王要学习15 小时前
Linux架构篇、第五章git2.49.0部署与使用
linux·运维·git·云计算·it
爱学习的张哥15 小时前
专栏项目框架介绍
git·fpga开发·udp·ddr·gt收发器
Aric_Jones17 小时前
lua入门语法,包含安装,注释,变量,循环等
java·开发语言·git·elasticsearch·junit·lua