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
相关推荐
要站在顶端15 小时前
克隆大型仓库卡住(7%每次就卡住了)
git
五月底_16 小时前
上传大量文件到github repo
git·github
rannn_1111 天前
【Git教程】概述、常用命令、Git-IDEA集成
java·git·后端·intellij-idea
春日见1 天前
虚拟机上由于网络问题无法正常git clone
linux·服务器·网络·人工智能·git·ubuntu·debug
冉佳驹1 天前
Linux ——— Git的核心操作流程、进程状态及环境变量相关知识
linux·git·进程·环境变量·进程状态·fork
cherry有点甜·1 天前
【git】git为什么会出现双向合并
git
笨鸟不是菜鸟1 天前
gitignore文件如何添加忽略文件或文件夹
git
灰色人生qwer1 天前
git add . 添加超长文件名报错了怎么办?
git·python·elasticsearch
星海拾遗2 天前
git rebase记录
大数据·git·elasticsearch
ljh5746491192 天前
PhpStorm 2022.3 版本中,修改使用 Git 提交时看到弹出式的对话框模式
ide·git·php·phpstorm