0%

Git 命令

学习 Git 命令

查看

  • 查看版本
    1
    git --version
  • 查看帮助文档
    1
    2
    git --help
    git clone --help

创建

  • 拉取版本库
    1
    2
    git clone git@domain.com:/repo.git
    git clone ssh://user@domain.com/repo.git
  • 初始化本地仓库
    1
    git init  # 将当前目录初始化
  • 创建版本库
    1
    git init --bare repo.git  # 在当前路径创建了一个名为 repo 的版本库

本地变更

  • 查看本地变更情况
    1
    git status  # 查看当前版本库中的变更情况
  • 文件变更对比
    1
    git diff  # 根据具体的使用情况对比相应文件的变化
  • 添加本地文件或者目录到版本管理中
    1
    2
    3
    git add .  # 将当前路径下所有文件或目录都添加到 待提交 中
    git add -p <file> # 将 file 添加到 待提交 中
    git add <dir> # 将 dir 目录下所有有变更的内容添加到 待提交中
  • 提交本地所有变更
    1
    2
    3
    git commit -a  # 需要编写提交说明
    git commit # 提交待
    git commit --amend #

提交历史日志

1
2
3
git log  # 从最开始查看所有提交的变更内容
git log -p <file> # 查看指定文件的变更
git blame <file> #

分支和标签

  • 分支
    1
    2
    3
    4
    git branch -av  # 查看所有存在的分支
    git checkout <branch> # 切换到已存在的分支
    git branch <net-branch> # 创建一个不存在的分支
    git checkout --track <remote/branch> # 在一个远程分支上创建一个跟踪分支
  • 标签
    1
    git tag <tag-name>  # 将当前提交以标签进行标记

更新和提交版本库

  • 查看远程版本库
    1
    2
    git remote -v  # 查看远程版本库列表
    git remote show <remote> # 查看指定远程版本库的信息
  • 添加远程版本库
    1
    git remote add <shortname> <url>  # 添加一个新的远程版本库,命名为 shortname
  • 更新
    1
    2
    git fetch <remote>  # 从远程版本库下载所有变更,但是并不合并到本地最新的版本中
    git pull <remote> <branch> # 从远程版本库下载所有变更,并且合并到最新版本中
  • 提交版本库
    1
    git push <remote> <branch>  # 将本地内容提交到远程版本库
  • 删除远程版本库中的分支
    1
    gir branch -dr <remote/branch>
  • 提交标签到版本库
    1
    git push --tags

合并和 Rebase

  • 合并分支到当前分支

    1
    git merge <branch>
  • rebase

    1
    git rebase <branch>  # 
  • Abort a rebase

    1
    git rebase --abort  # 
  • Continue rebase

1
git rebase --continue  # 
  • Use mergetool

    1
    git mergetool
  • Use

    1
    git add 

undo

1
2
3
4
5
6
git reset --hard HEAD
git checkout HEAD <file>
git revert <commit>
git reset --hard <commit>
git reset <commit>
git reset --keep <commit>