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 "your@email.com"

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

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 "lyt@nicesonic.cn"

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

推送库

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

拉取库

克隆仓库到指定文件夹

bash 复制代码
git clone git@github.com:yourname/awesome-project.git my-local-folder

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

# 查看文件列表
ls -la

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

bash 复制代码
git pull
相关推荐
C++ 老炮儿的技术栈2 小时前
在 Scintilla 中为 Squirrel 语言设置语法解析器的方法
linux·运维·c++·git·ubuntu·github·visual studio
余很多之很多6 小时前
命令行和neovim的git操作软件-lazygit
git
猫头虎7 小时前
GitHub下载教程:2025年最新详解从GitHub上传、下载文件、子目录与完整项目【图文教程】
git·svn·gitee·开源·github·gitea·gitcode
i建模13 小时前
将远程 main 分支同步到 develop 分支的完整指南
git
即使再小的船也能远航1 天前
【Git】实用Git操作指南:从入门到高效协作
git
<但凡.1 天前
Git 完全手册:从入门到团队协作实战(4)
git·bash
SugarPPig1 天前
Git 创建一个完全没有提交历史的 master 分支
git
lb29172 天前
git的使用,推送仓库github
git·github
躲在云朵里`2 天前
Git的使用
大数据·git·elasticsearch
悟能不能悟3 天前
在 IntelliJ IDEA 中打开这个用于设置 Git 用户名(Name)和邮箱(Email)的特定弹窗
java·git·intellij-idea