Git 常用命令总结(持续更新...)


前言

在使用 Git 时,尤其是通过集成开发环境 (IDE) 的图形界面操作 Git 后,当我们又想使用 Shell 来管理的时后,我们可能会发现自己不太容易记住一些命令。

以下是我整理的一些关于Git命令的总结 (Summary of commonly used Git commands):

一、基本操作

  1. 初始化仓库

    git init
    
  2. 克隆仓库

    git clone <repository>
    
    // 克隆一个仓库
    git clone https://github.com/Herbert0501/gitignore.git
    // 指定分支
    git clone -b 分支名 https://github.com/Herbert0501/gitignore.git
    
  3. 查看状态(查看当前工作目录和暂存区的状态)

    git status
    
  4. 添加文件到暂存区

    git add <file>
    
    // 将所有变化添加到暂存区
    git add .
    
  5. 提交修改

    git commit -m "commit msg"
    
  6. 查看提交历史

    git log
    

二、分支操作

  1. 查看分支

    git branch
    
  2. 创建新分支

    git branch <new-branch>
    
    // 例如为模块新增功能-删除功能
    git branch 20240521-feature-delete
    
  3. 切换分支

    git checkout <branch>
    
    // 切换到主分支 (main)
    git checkout main
    
  4. 创建并切换到新分支

    git checkout -b <new-branch>
    
  5. 删除分支

    git branch -d <branch>
    
  6. 合并分支

    git merge <branch>
    
    // 1.切换到主分支(main)
    git checkout main
    // 2.将20240521-feature-delete分支合并到main
    git merge 20240521-feature-delete
    

三、远程仓库

  1. 查看远程仓库

    git remote -v
    
  2. 添加远程仓库

    git remote add <name> <url>
    
  3. 拉取远程仓库

    git pull <remote> <branch>
    
  4. 推送到远程仓库

    git push <remote> <branch>
    
    // 强制推送, 将本地覆盖到远程
    git push -f <remote> <branch>
    
  5. 删除远程分支

    git push <remote> --delete <branch>
    

四、撤销修改

  1. 撤销未暂存的修改

    git checkout -- <file>
    
  2. 撤销已暂存的修改

    git reset HEAD <file>
    
  3. 撤销提交

    git revert <commit>
    
  4. 重置提交

    git reset --soft <commit>
    git reset --hard <commit>
    
  5. 将文件从暂存区中删除,但保留工作目录中的文件

    git rm --cached <file1> <file2>
    
    // 1. 将文件添加到.gitignore
    file1
    file2
    // 2. 使用命令删除
    git rm --cached file1 file2
    // 3. 提交更改(删除远程文件,保留本地文件)
    git add .
    git commit -m "something modify"
    

五、标签

  1. 列出标签

    git tag
    
  2. 创建标签

    git tag <tag>
    
  3. 删除标签

    git tag -d <tag>
    
  4. 推送标签

    git push <remote> <tag>
    
  5. 推送所有标签

    git push <remote> --tags
    

参考 Git 官方文档

总结

如果您觉得今天的文章对您有帮助,我相信您一定会喜欢我的博客。
哈利の小屋 - Kang Yao Coding - 努力有时候战胜不了天分,但至少能让别人看得起你
在那里,我会定期更新关于计算机类的文章,并与您分享更多实用的经验和知识。欢迎您来访问和留言交流。

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值