【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 操作。希望对你有所帮助!如果你有更多具体的需求或问题,欢迎继续提问。

相关推荐
jstart千语9 小时前
【Git】连接github时的疑难杂症(DNS解析失败)
git·github
工具罗某人9 小时前
TortoiseGit使用图解
git
Zhuai-行淮10 小时前
vscode和git 踩坑
ide·git·vscode
这颗橘子不太甜QAQ11 小时前
Husky使用技巧
javascript·git·代码规范
fanTuanye11 小时前
Git基本使用(很详细)
git·github
忍者扔飞镖11 小时前
git
git
李菠菜13 小时前
解决Windows系统下Git克隆时报错“unable to checkout working tree”的方法详解
windows·git
island131413 小时前
【git#4】分支管理 -- 知识补充
大数据·git·elasticsearch
船长@Quant14 小时前
协作开发攻略:Git全面使用指南 — 引言
git·版本控制·源代码管理·协作开发
极小狐15 小时前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab