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

相关推荐
songyuc1 小时前
【Git】请帮忙解释一下“git reset”
git·elasticsearch
Komorebi_99995 小时前
使用Git创建自己的分支的操作指南
git
~~李木子~~8 小时前
git仓库管理
git
秦jh_8 小时前
【git】远程操作
git
tianming201915 小时前
Gogs迁移到Gitea不完全指南
git·后端
QT 小鲜肉17 小时前
【Git、GitHub、Gitee】GitLab的概念、注册流程、远程仓库操作以及高级功能详解(超详细)
git·qt·gitee·gitlab·github
你的人类朋友1 天前
✍️记录自己的git分支管理实践
前端·git·后端
wVelpro1 天前
git diff 输出空,但 git status 提示 Motified(M)解决办法
git
high20111 天前
【Git】-- Rebase 减少 Commit 次数指南
大数据·git·elasticsearch
曾帅1681 天前
git报错解决
git