git常用命令

发布于:2024-09-18 ⋅ 阅读:(135) ⋅ 点赞:(0)

初始化和配置

# 初始化一个新的 Git 仓库
git init

# 设置全局用户名
git config --global user.name "YourName"

# 设置全局电子邮件地址
git config --global user.email "youremail@example.com"

文件修改和暂存

# 将文件添加到暂存区
git add <file-path>

# 提交暂存区的文件到仓库,并附上提交信息
git commit -m "Commit message describing the changes"

查看提交历史

# 查看提交历史
git log

# 简化的一行格式显示提交历史
git log --oneline

# 图形化展示整个仓库的分支和提交历史
git log --graph --oneline --decorate --all

分支管理

# 列出所有分支
git branch

# 创建并切换到新分支
git checkout -b <new-branch-name>

# 切换到指定分支
git checkout <branch-name>

# 合并指定分支到当前分支
git merge <branch-name>

# 删除指定分支
git branch -d <branch-name>

远程仓库

# 添加远程仓库
git remote add origin <repository-url>

# 从远程仓库获取数据
git fetch origin

# 从远程仓库拉取数据并合并到当前分支
git pull origin <branch-name>

# 将当前分支的更改推送到远程仓库
git push origin <branch-name>

撤销和回滚

# 将 HEAD 指向特定的提交,并撤销之后的所有提交
git reset --hard <commit-hash>

# 创建一个新的提交,撤销之前的某个提交的更改
git revert <commit-hash>

# 撤销对文件的未暂存更改
git checkout -- <file-path>

标签管理

# 创建一个新的标签
git tag <tag-name>

# 创建一个带有注释的标签
git tag -a <tag-name> -m "Annotation message for the tag"

# 推送标签到远程仓库
git push origin <tag-name>

解决冲突

# 使用图形化工具解决合并冲突
git mergetool

子模块管理

# 添加子模块到仓库
git submodule add <repository-url> <path-to-submodule>

# 初始化和更新所有子模块
git submodule update --init --recursive

请确保替换 <file-path><commit-hash>(git log --oneline,用git log --oneline查看哈希值)<new-branch-name><branch-name><repository-url><tag-name><path-to-submodule> 为你的实际文件路径、提交哈希、新分支名称、分支名称、仓库 URL、标签名称和子模块路径。这些命令应该在 Git 仓库的根目录下执行。


网站公告

今日签到

点亮在社区的每一天
去签到