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

相关推荐
2501_9032386521 分钟前
Git Bash:Windows下的强大命令行工具
windows·git·bash·个人开发
神明木佑2 小时前
datalist 是什么?以及作用是什么?
git
那片海还在吗4 小时前
使用 Git、Postman、Newman、Jenkins 与邮件构建自动化接口测试及通知流程
git·jenkins·postman
heeheeai12 小时前
git 国内源
git
the_seventh_dog13 小时前
git | 团队协作开发注意事项
git
yinshuilan16 小时前
今日运维之-Mac笔记本python环境问题
运维·git·python·brew
辄也16 小时前
git拉取远程分支到本地分支
git
皮克斯的进化之路17 小时前
Git原理+使用(超详细)
git
wrjwww17 小时前
【Git学习笔记】Git常用命令
笔记·git·学习
大乔乔布斯17 小时前
WSL,Power shell 和CMD, Git bash的区别
开发语言·git·bash