【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 <新分支名>
相关推荐
大猫会长31 分钟前
mac中创建 .command 文件,执行node服务
前端·chrome
小皮侠3 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
HalukiSan4 小时前
如何提交PR
git·gitlab·github
未来之窗软件服务5 小时前
chrome webdrive异常处理-session not created falled opening key——仙盟创梦IDE
前端·人工智能·chrome·仙盟创梦ide·东方仙盟·数据调式
涛哥码咖15 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
爱莉希雅&&&15 小时前
shell编程之awk命令详解
linux·服务器·git
baiyu3315 小时前
成为git砖家(12): 看懂git合并分支时冲突提示符
git
wu_aceo20 小时前
将本地项目提交到Gitee
git·gitee·提交·本地提交·上传git
随便取个六字1 天前
GIT操作 学习
git·学习
Joker`s smile1 天前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome