【GIt】Git常用命令

以下是一些常用的 Git 命令及其简要说明:

初始化仓库

  1. 初始化一个新的 Git 仓库

    sh 复制代码
    git init
  2. 克隆一个现有的 Git 仓库

    sh 复制代码
    git clone <repository-url>

文件状态

  1. 查看工作目录的状态

    sh 复制代码
    git status
  2. 查看文件的差异

    sh 复制代码
    git diff
  3. 查看暂存区与最近一次提交之间的差异

    sh 复制代码
    git diff --cached

添加和暂存更改

  1. 将文件添加到暂存区

    sh 复制代码
    git add <file-name>

    或者一次性添加所有更改的文件:

    sh 复制代码
    git add .
  2. 取消暂存文件

    sh 复制代码
    git reset HEAD <file-name>

提交更改

  1. 提交暂存区的更改

    sh 复制代码
    git commit -m "Your commit message"
  2. 直接提交工作目录中的所有更改(跳过暂存区)

    sh 复制代码
    git commit -am "Your commit message"

分支管理

  1. 创建新分支

    sh 复制代码
    git branch <branch-name>
  2. 切换分支

    sh 复制代码
    git checkout <branch-name>
  3. 创建并切换到新分支

    sh 复制代码
    git checkout -b <branch-name>
  4. 合并分支

    sh 复制代码
    git merge <branch-name>
  5. 删除分支

    sh 复制代码
    git branch -d <branch-name>

查看历史记录

  1. 查看提交历史

    sh 复制代码
    git log
  2. 查看简洁的提交历史

    sh 复制代码
    git log --oneline
  3. 查看图形化的提交历史

    sh 复制代码
    git log --graph --oneline

远程操作

  1. 查看远程仓库信息

    sh 复制代码
    git remote -v
  2. 拉取远程仓库的最新更改

    sh 复制代码
    git pull origin <branch-name>
  3. 推送本地更改到远程仓库

    sh 复制代码
    git push origin <branch-name>
  4. 添加新的远程仓库

    sh 复制代码
    git remote add <remote-name> <repository-url>
  5. 移除远程仓库

    sh 复制代码
    git remote remove <remote-name>

撤销操作

  1. 撤销最近一次提交,但保留更改到暂存区

    sh 复制代码
    git reset --soft HEAD~1
  2. 撤销最近一次提交,并丢弃所有更改

    sh 复制代码
    git reset --hard HEAD~1
  3. 撤销某个特定的提交,并创建一个新的撤销提交

    sh 复制代码
    git revert <commit-hash>
  4. 强制推送撤销后的历史记录到远程仓库

    sh 复制代码
    git push origin <branch-name> -f

这些命令涵盖了日常使用中最常见的 Git 操作。希望对你有所帮助!如果你有更多具体的需求或问题,欢迎继续提问。

相关推荐
权咚8 小时前
阿权的开发经验小集
git·ios·xcode
nightunderblackcat1 天前
新手向:Git下载全攻略
git
若无_1 天前
了解 .husky:前端项目中的 Git Hooks 工具
前端·git
想当花匠的小码农1 天前
Git 仓库“减肥”指南:从检测、清理到同步的全流程实践
git
R_.L2 天前
Git : 基本操作
git
python_1362 天前
git常见冲突场景及解决办法
git
洛小豆2 天前
Git 打标签完全指南:从本地创建到远端推送
前端·git·github
王道长服务器 | 亚马逊云2 天前
一个迁移案例:从传统 IDC 到 AWS 的真实对比
java·spring boot·git·云计算·github·dubbo·aws
嘟嘟可在哪里。2 天前
IntelliJ IDEA git凭据帮助程序
java·git·intellij-idea