Git 命令及示例

本文详细介绍了Git的核心功能,包括初始化仓库、添加和提交文件、分支管理、与远程仓库交互、撤销更改以及使用标签和高级命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. 基础命令

2. 分支管理

3. 远程操作

4. 撤销修改

5. 标签

6. 高级命令


1. 基础命令

  • git init 初始化一个新的Git仓库。 示例:git init
  • git clone [url] 克隆一个仓库到本地。 示例:git clone https://github.com/user/repo.git
  • git add [file] 将文件添加到暂存区。 示例:git add README.md
  • git commit -m "[commit message]" 提交暂存区的内容到仓库。 示例:git commit -m "Initial commit"
  • git status 查看仓库当前的状态。 示例:git status
  • git log 查看提交历史。 示例:git log
  • git diff 查看未暂存的文件更新了哪些部分。 示例:git diff

2. 分支管理

  • git branch 列出所有本地分支。 示例:git branch
  • git branch [branch-name] 创建新分支。 示例:git branch feature-x
  • git checkout [branch-name] 切换到指定分支。 示例:git checkout feature-x
  • git merge [branch] 将指定分支合并到当前分支。 示例:git merge feature-x
  • git branch -d [branch-name] 删除分支。 示例:git branch -d feature-x

3. 远程操作

  • git remote add [shortname] [url] 添加一个新的远程仓库。 示例:git remote add origin https://github.com/user/repo.git
  • git fetch [remote-name] 从远程仓库下载新分支与数据。 示例:git fetch origin
  • git pull [remote-name] [branch-name] 从远程仓库提取数据并尝试合并到当前分支。 示例:git pull origin master
  • git push [remote-name] [branch-name] 将本地分支的更新推送到远程仓库。 示例:git push origin master

4. 撤销修改

  • git checkout -- [file] 放弃单个文件的修改。 示例:git checkout -- README.md
  • git reset [file] 从暂存区移除文件,但不影响文件当前的修改。 示例:git reset README.md
  • git reset --hard [commit] 撤销到指定提交,放弃所有修改。 示例:git reset --hard 0d1d7fc32

5. 标签

  • git tag 列出所有标签。 示例:git tag
  • git tag [tag-name] 创建一个新标签。 示例:git tag v1.0.0
  • git push [remote-name] [tag-name] 推送一个标签到远程仓库。 示例:git push origin v1.0.0

6. 高级命令

  • git stash 暂时存储未提交的修改,以便清理工作目录。 示例:git stash
  • git rebase [branch] 重新排列提交,可以用来清理提交历史。 示例:git rebase master
  • git cherry-pick [commit] 选择一个提交,将其作为一个新的提交引入当前分支。 示例:git cherry-pick 4a202b3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值