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
相关推荐
仍然探索未知中13 小时前
Git分支管理
git
小妖66614 小时前
windows11 安装好后右键没有 git bash 命令
git
只做开心事14 小时前
Git 多人协作
git
freejackman14 小时前
Git从入门到精通
git·gitee·gitlab·github
兔子坨坨17 小时前
pycharm连接github(详细步骤)
windows·git·学习·pycharm·github
大大小小聪明1 天前
Git合并多个提交方法详解
git·github
Baoing_1 天前
Git 项目切换到新的远程仓库地址
git
暴躁哥1 天前
Git 版本控制系统入门指南
git
diving deep2 天前
IDEA中git对于指定文件进行版本控制
git
趁你还年轻_2 天前
记录一次git提交失败解决方案
git