Git常用操作指令

  1. 初始化配置

    配置全局用户名和邮箱

    git config --global user.name "账号"
    git config --global user.email "邮箱"

    查看配置信息

    git config --list

  2. 仓库初始化创建新的 Git 仓库:

    初始化新仓库

    git init

    克隆远程仓库

    git clone URL

  3. 状态查看实时了解仓库状态:

    查看工作区状态

    git status

    查看简化状态信息

    git status -s

    查看本地分支

    git branch

    查看远程分支

    git branch -r

    查看本地和远程所有分支

    git branch -a

  4. 添加和提交基本的版本控制操作:

    添加指定文件到暂存区

    git add <file-name>

    添加所有更改

    git add .

    提交到本地仓库

    git commit -m "commit message"

    添加并提交

    git commit -am "commit message"

    移除暂存区的提交

    git reset
    git reset <file>

    将提交推送到远程仓库

    git push

    将本地当前分支提交到远程指定分支remote-branch

    git push origin remote-branch

  5. 分支操作:

    创建新分支

    git branch <branch-name>

    切换分支

    git checkout <branch-name>

    创建并切换分支

    git checkout -b <branch-name>

    删除分支

    git branch -d <branch-name>

  6. 远程仓库操作与远程仓库交互:

    添加远程仓库

    git remote add origin <repository-url>

    查看远程仓库

    git remote -v

    推送到远程

    git push origin <branch-name>

    拉取远程更新

    git pull origin <branch-name>

  7. 分支合并:

    #提交brancheA某次的commit merge到指定分支branchB
    git log brancheA 获取到需要merge 的commit 的哈希值 hash
    git checkout branchB
    git cherry-pick hash
    git status 查看是否已经更新
    git push 将更新推送到远程

    合并分支

    git merge <branch-name>

    变基操作

    git rebase <branch-name>

    解决冲突后继续变基

    git rebase --continue

  8. 暂存操作临时保存工作进度:

    保存当前工作进度

    git stash

    查看存储的工作进度

    git stash list

    恢复最近的进度

    git stash pop

    删除所有进度

    git stash clear

  9. 日志查看查看提交历史:

    查看提交日志

    git log

    查看简化日志

    git log --oneline

    查看图形化日志

    git log --graph --pretty=oneline --abbrev-commit

  10. 差异比较比较:

    查看工作区和暂存区的差异

    git diff

    查看暂存区和最新提交的差异

    git diff --staged

    查看两个分支的差异

    git diff <branch1> <branch2>

  11. 撤销操作修正错误操作:

    撤销工作区的修改

    git checkout -- <file-name>

    撤销暂存区的修改

    git reset HEAD <file-name>

  12. 日常工作流程

    更新本地代码

    git pull origin main

    创建功能分支

    git checkout -b feature/new-feature

    推送到远程

    git push origin feature/new-feature

相关推荐
荔枝吻21 分钟前
忘记服务器密码,在Xshell7中查看已保存密码
运维·服务器·github
摇滚侠1 小时前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东2 小时前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
tod1133 小时前
TCP全连接队列与tcpdump抓包
网络·网络协议·tcp/ip·github·tcpdump
Luck_ff08103 小时前
百度指数数据采集与可视化平台 BaiduIndexHunter
github·开源软件
阿里嘎多学长4 小时前
2026-02-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
何中应10 小时前
使用SSH地址拉取远程仓库代码报下面的错误
git
子兮曰10 小时前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
何中应10 小时前
Git本地仓库命令补充
git
sun00770012 小时前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git