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
相关推荐
麻辣璐璐3 小时前
【新手小白版】Gerrit使用教程
git·gerrit·新手
AlexMercer10129 小时前
Ubuntu从零开始配置Git
c++·git·ubuntu·gitee
胡西风_foxww21 小时前
git 添加除了包含特定字符串的文件
git·字符串·文件·add·添加·特定·包含
parade岁月21 小时前
Git 凭据管理器原理与多账号管理指南
git
zzzyulin21 小时前
git note
git
六件套是我21 小时前
【解答疑惑】git执行cherrypick后到另一个分支,然后再合并会出现问题吗?
git
sulikey1 天前
从零配置一个规范的 Python Git 仓库(适用于 Gitee / GitHub)
git·python·pycharm·gitee·github
学渣676562 天前
【面向小白】git rebase全面总结,什么时候用rebase
git
小龙报2 天前
《算法每日一题(1)--- 第31场蓝桥算法挑战赛》
c语言·开发语言·c++·git·算法·学习方法
222you2 天前
idea整合Git
git