git 命令集

仓库初始化与配置

bash 复制代码
git init:​初始化本地 Git 仓库。

git clone <url>:​克隆远程仓库。

git config --global user.name "用户名":​设置全局用户名。

git config --global user.email "邮箱":​设置全局邮箱。

git config --global core.editor vim:​设置默认编辑器为 vim

状态与提交

bash 复制代码
git status:​查看当前工作区和暂存区状态。

git add <file>:​将文件添加到暂存区。

git add .:​将所有更改添加到暂存区。

git commit -m "提交信息":​提交暂存区的更改。

git commit --amend:​修改上一次提交。

git log:​查看提交历史。

git log --oneline:​以简洁形式查看提交历史。

git log --graph --oneline --all:​以图形化形式查看所有分支的提交历史

分支管理

bash 复制代码
git branch:​列出本地分支。

git branch -a:​列出所有分支(包括远程)。

git branch <branch-name>:​创建新分支。

git checkout <branch-name>:​切换到指定分支。

git checkout -b <branch-name>:​创建并切换到新分支。

git merge <branch-name>:​将指定分支合并到当前分支。

git branch -d <branch-name>:​删除本地分支。

git branch -D <branch-name>:​强制删除本地分支。

远程仓库操作

bash 复制代码
git remote add origin <url>:​添加远程仓库。

git remote -v:​查看远程仓库信息。

git fetch:​从远程仓库获取最新的提交。

git pull:​获取远程仓库的更新并与当前分支合并。

git push:​将本地分支推送到远程仓库。

git push origin <branch-name>:​将指定分支推送到远程仓库。

git push origin --delete <branch-name>:​删除远程分支

撤销与重置

bash 复制代码
git reset <commit>:​重置当前分支到指定提交。

git reset --hard:​重置当前分支到最后一次提交,丢弃所有未提交的更改。

git checkout -- <file>:​撤销工作区对文件的修改。

git checkout <commit_id>:回退到旧版本
git switch -:返回到最新版本

git revert <commit>:​创建一个新的提交,撤销指定提交的更改

暂存与恢复

bash 复制代码
git stash:​将当前工作区的更改暂存起来。

git stash list:​查看所有暂存的更改。

git stash apply:​恢复最近一次暂存的更改。

git stash apply stash@{0}:​恢复指定的暂存更改。

git stash drop stash@{0}:​删除指定的暂存

其他实用命令

bash 复制代码
git diff:​查看工作区与暂存区的差异。

git diff --staged:​查看暂存区与上次提交的差异。

git tag:​列出所有标签。

git tag <tag-name>:​创建新标签。

git show <commit>:​查看指定提交的详细信息。

git reflog:​查看引用日志,记录了所有的 HEAD 变动。

git gc:​清理无用的文件和优化本地仓库。


简洁教程

在git平台上传新项目

在代码托管平台(如 GitHub/GitLab/Gitee)上操作:

登录你的账号,点击 New Repository。

输入仓库名称(如 my-project),不要勾选 "Initialize with README"(保持空仓库)。

创建完成后,复制仓库的 HTTPS 或 SSH URL(如 https://github.com/yourname/my-project.git)。

bash 复制代码
cd my-project
git init
echo "# My New Project" >> README.md
git add .

# 全局配置(对所有仓库生效)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# 或在项目目录下单独配置(仅对当前仓库生效)
git config user.name "Your Name"
git config user.email "[email protected]"

git commit -m "Initial commit"
git remote add origin https://github.com/yourname/my-project.git
git push -u origin main
bash 复制代码
git config --global user.name "lyt"
git config --global user.email "[email protected]"

完成后,你的项目已上传到远程仓库!如果需要后续更新代码,只需:

推送库

bash 复制代码
git add .
git commit -m "Update: add new feature"
git push

拉取库

克隆仓库到指定文件夹

bash 复制代码
git clone [email protected]:yourname/awesome-project.git my-local-folder

# 进入项目目录
cd my-local-folder

# 查看文件列表
ls -la

拉取完成后,你的本地文件夹已与远程仓库同步!后续更新代码只需:

bash 复制代码
git pull
相关推荐
极小狐42 分钟前
极狐GitLab 的压缩和合并是什么?
运维·git·ssh·gitlab·github
极小狐8 小时前
极狐GitLab 合并请求依赖如何解决?
运维·git·ssh·gitlab·github
ikkkkkkkl11 小时前
Git基本操作
git
互联网搬砖老肖13 小时前
git 的基本使用
大数据·git·elasticsearch
程序猿chen15 小时前
量子跃迁:Vue组件安全工程的基因重组与生态免疫(完全体终局篇)
前端·vue.js·git·安全·面试·前端框架·跳槽
SunTecTec16 小时前
Idea 配置 Git
git
chxii17 小时前
2.4.5goweb项目上传到csdn的git仓库
git
清风徐来QCQ17 小时前
git和github的使用指南
git·github
serene9418 小时前
IntelliJ IDEA 2025.2 和 JetBrains Rider 2025.1 恢复git commit为模态窗口
java·git·intellij-idea