Git的常用操作命令

Git是一个分布式版本控制系统,广泛应用于软件开发项目中来追踪和控制代码的修改历史。

Git常用命令如下:

配置用户信息

   # 设置全局用户名和邮箱
   git config --global user.name "Your Name"
   git config --global user.email "youremail@example.com"

初始化仓库

   # 在当前目录创建一个新的Git仓库
   git init

克隆仓库

   # 克隆远程仓库至本地
   git clone https://github.com/user/repo.git

添加文件到暂存区

   # 将某个文件添加到暂存区以准备提交
   git add <file_path>
   # 或者添加所有改动
   git add .

提交更改

   # 提交暂存区的内容到本地仓库,附带提交信息
   git commit -m "Initial commit or describe your changes here"

查看状态

   # 检查工作区和暂存区的状态
   git status

拉取远程更新

   # 获取远程仓库的最新改动并尝试自动合并到当前分支
   git pull origin <branch_name>

推送更改

   # 将本地分支的更改推送到远程仓库的对应分支
   git push origin <branch_name>

创建与切换分支

   # 创建并立即切换到新的分支
   git checkout -b new_branch
   # 切换回已有分支
   git checkout <existing_branch>
#### **查看分支**:

    # 显示所有本地分支
    git branch
    # 显示所有本地和远程分支
    git branch -a
#### **解决冲突与合并分支**:

    # 合并指定分支到当前分支
    git merge other_branch
#### **stash暂存未提交的更改**:

    # 暂存所有未提交的更改
    git stash
    # 恢复最近暂存的更改
    git stash pop
#### **查看提交历史**:

    # 显示提交历史记录
    git log
#### **Cherry-pick**:

    # 将指定提交应用到当前分支
    git cherry-pick <commit_hash>
#### **撤销更改**:

*

  ##### 取消暂存区的更改:

      git reset <file_path>  # 将指定文件从暂存区移除,但保留工作区的更改
      git reset HEAD <file_path>  # 类似于上述命令,取消暂存的同时恢复到HEAD版本

*

  ##### 回滚工作区的更改:

      git checkout -- <file_path>  # 抛弃工作区对指定文件的更改
#### **删除文件**:

*

  ##### 从版本库和工作区一起删除:

      git rm <file_path>
      git commit -m "Remove file"

*

  ##### 仅从版本库中删除(保留工作区文件):

      git rm --cached <file_path>
      git commit -m "Remove file from repository"
#### **重命名/移动文件**:

    git mv <old_file_path> <new_file_path>
    git commit -m "Rename/move file"
#### **查看不同版本间的差异**:

    git diff  # 查看尚未暂存的改动
    git diff --staged  # 查看已暂存但未提交的改动
    git diff <commit1> <commit2>  # 查看两个提交之间的差异
#### **回退到以前的提交**:

    # 回退到某一提交,并且丢弃之后的所有更改(谨慎操作)
    git reset --hard <commit_hash>
#### **标签管理**:

*

  ##### 创建标签:

      git tag <tag_name> <commit_hash>  # 标记特定提交
      git tag <tag_name>  # 标记当前HEAD指向的提交

*

  ##### 推送标签到远程仓库:

      git push origin <tag_name>

*

  ##### 删除标签:

      git tag -d <tag_name>
      git push origin :refs/tags/<tag_name>  # 删除远程标签
#### **stash栈操作**:

*

  ##### 存储未提交的更改并清理工作区(保存现场):

      git stash

*

  ##### 列出stash列表:

      git stash list

*

  ##### 应用stash中的某个更改:

      git stash apply <stash@{index}>

*

  ##### 永久应用并删除stash:

      git stash pop
#### **子模块操作**:

*

  ##### 添加子模块:

      git submodule add <repository_url> <path_to_submodule>

*

  ##### 更新子模块:

      git submodule update --remote
#### **交互式暂存(添加部分更改)**:

    git add -p
#### **Rebase(变基)**:

    # 将feature分支的更改重新应用到master分支上,保持线性历史
    git checkout feature
    git rebase master
    git checkout master
    git merge feature
相关推荐
uppp»18 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ew452182 小时前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
Moon.92 小时前
el-table的hasChildren不生效?子级没数据还显示箭头号?树形数据无法展开和收缩
前端·vue.js·html
垚垚 Securify 前沿站2 小时前
深入了解 AppScan 工具的使用:筑牢 Web 应用安全防线
运维·前端·网络·安全·web安全·系统安全
m0_748256144 小时前
SpringBoot
java·spring boot·后端
阿华的代码王国4 小时前
【从0做项目】Java搜索引擎(3)
java·搜索引擎·项目
Mr.朱鹏4 小时前
针对Feign客户端请求体参数处理问题
java·jvm·spring boot·spring·spring cloud·maven·intellij-idea
多想和从前一样5 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django