【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 <新分支名>
相关推荐
wjs04011 小时前
Git常用的命令
java·git·gitlab
原野风霜32411 小时前
Git使用总结
git
青草地溪水旁11 小时前
Git Bash 中 Git 命令的实用主义指南
git·bash
至善迎风12 小时前
版本管理系统与平台(权威资料核对、深入解析、行业选型与国产平台补充)
git·gitee·gitlab·github·svm
上单带刀不带妹17 小时前
Git rm 命令与系统 rm 命令的区别详解
git
我的收藏手册20 小时前
性能监控shell脚本编写
前端·git·github
Yvonne爱编码1 天前
简述ajax、node.js、webpack、git
前端·git·ajax·webpack·node.js·visual studio
兔子坨坨1 天前
25年下载chromedriver.140
chrome
As33100101 天前
Chrome 插件开发入门指南:从基础到实践
前端·chrome
2501_920047031 天前
git在Linux中的使用
linux·git·elasticsearch