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

相关推荐
b1ng5 小时前
新人程序员 Git 一站式指南
git·github
程序员的世界你不懂6 小时前
IDE 关联 Git 操作
ide·git
weixin_428498498 小时前
Git Submodule 介绍和使用指南
git
jingshaoqi_ccc19 小时前
GitKraken最后一个免费版本和下载地址
git·github·gitkraken·版本管理工具
乌云暮年19 小时前
Git简单命令
git·gitee·github·batch命令
用户1259265423201 天前
使用 Docker 搭建 Gitea 并实现 Git HTTP 自动登录
git
一只毛驴1 天前
谈谈对git stash的理解?
git
长风破浪会有时呀1 天前
Git 学习笔记
笔记·git·学习
中微子2 天前
Git Rebase 详解:概念、原理与实战示例
git
荔枝吻2 天前
【保姆级喂饭教程】Windows下安装Git Flow
windows·git·git flow