【Git】工作中Git常用命令

159 阅读2分钟

日常操作

  1. 添加文件到版本库
    git add
    
  2. 添加单个文件到版本库
    git add test.text
    
  3. 添加某个后缀的文件
    git add *.html
    
  4. 提交版本库文件
    git commit -m 备注
    
  5. 推入仓库
    git push
    
  6. 从仓库拉取文件
    git pull
    

查看历史状态

  1. 当前状态
    git status
    
  2. 查看历史记录
    git log
    

撤销修改

  1. 撤销尚未提交的所有修改
    git checkout head .
    
  2. 撤销尚未提交的单个后缀文件
    git checkout head *.后缀
    

分支

  1. 查看分支
    # 查看本地所有分支
    git branch
    
    # 查看远程所有分支
    git branch -r
    
    # 查看本地和远程所有分支
    git branch -a
    
  2. 新建分支
    git branch 分支名称
    
    # 以远程分支为基础新建一个分支,并切换到该分支
    git checkout -b 分支名称 origin/远程分支
    
  3. 切换分支
    git checkout 分支名称
    
  4. 删除分支
    # 删除分支(这个命令如果分支没有被合并会删除失败)
    git branch -d 分支名称
    
    # 删除分支(这个命令是强制删除)
    git branch -D 分支名称
    
    # 删除远程已经不存在的分支
    git remote prune origin
    
  5. 合并分支
    # 如果发生冲突,就不会自动提交
    git merge 指定分支名称
    
  6. 重命名分支
    # 不会覆盖同名分支
    git branch -m 原来的分支名称 新的分支名称
    
    # 会覆盖同名分支
    git branch -M 原来的分支名称 新的分支名称
    
  7. 在现有分支与指定的远程分支之间建立追踪关系
    git branch --set-upstream 分支名称 远程分支名称
    

克隆

  1. 克隆项目
    git clone 项目的ssh或http地址
    # ssh
    
    git clone git@github.com:songxiaoQ/test.git
    # https
    git clone https://github.com/songxiaoQ/test.git
    
  2. 克隆指定分支的代码
    git clone -b 分支名称 项目的ssh或http地址
    

基本设置(初始化)

  1. 全局配置git账号
    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"
    
  2. 查询已配置git账号
    git config --global user.name
    git config --global user.email
    
  3. 移除全局配置账号
    git config --global --unset user.name
    git config --global --unset user.email
    
  4. 生成SSH公钥
    ssh-keygen -t rsa -C "you@example.com"
    
  5. 初始化git版本库
    git init
    

其他命令

  1. git里设置大小写敏感。Windows上的Git默认是大小写不敏感的

    git config core.ignorecase false