美文网首页
Git 命令便捷手册

Git 命令便捷手册

作者: evanxlh | 来源:发表于2020-07-19 16:21 被阅读0次

    Git 命令便捷手册

    Git配置

    在开始Git之旅之前,我们需要设置一下Git的配置变量,这是一次性的工作。这些设置会在用户文件(用户主目录的.gitconfig)或系统文件(eg. /etc/gitconfig) 中做永久的记录。

    配置用户信息

    git config --global user.name "Evan Xie"

    git config --global user.email your_email@example.com

    查看配置信息

    git config -l

    git config --system --list

    git config --global --list

    git config --local --list

    设置命令别名

    以给Git命令设置别名,这样命令就可以用别名代替了。如果去掉sudo, 只在本用户的全局配置中添加Git命令别名。

    sudo git config --system alias.st status

    sudo git config --system alias.ci commit

    sudo git config --system alias.co checkout

    sudo git config --system alias.br branch

    开启颜色显示

    git config --global color.ui true

    编辑 Config

    • 打开进行编辑.git/config文件

      git config -e

    • 打开进行编辑.gitconfig文件

      git config -e --global

    • 打开进行编辑etc/gitconfig文件

      git config -e --system

    关闭 SSL 认证

    添加私有仓库可能出现这个问题,可以用以下命令来解决:

    git config --global http.sslVerify false

    Git 命令自动补齐

    使用命令自动补齐功能后,我们在敲 git 命令时,就可以用 tab 键来完成自动补齐,甚是方便。

    安装命令自动补齐工具:

    1. 从命令行终端安装 bash-completion

      brew install bash-completion

    1. 查看 bash-completion 信息并按照提示更新 ~/.bash_profile 文件

      brew info bash-completion

    我的 Terminal 提示为:

    Add the following line to your ~/.bash_profile:
      [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
    
    1. 更新 ~/.bash_profile

      我的 macos 版本为:Catalina 10.15.6, ~/.bash_profile 更新内容为:

      # Setting PATH for Python 3.8
      # The original version is saved in .bash_profile.pysave
      PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
      export PATH
      
      [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
      
    2. 下载 git-completion.bash 文件 (可以从 Git 工程 contrib/completion 目录中下载), 并将其复制到 /usr/local/opt/bash-completion/etc/bash_completion.d 这个目录下 ( 这个目录可以从 第 2 步中的 brew info bash-completion 提示中获得)。

    3. brew unlink bash-completion

    4. brew link bash-completion

    5. 重新打开 Terminal , 既可体验命令自动补齐。

    Git 仓库

    克隆仓库到本地

    git clone git@github.com:ReactiveX/RxSwift.git

    查看远程仓库地址

    git remote -v

    更改Git远程仓库

    git remote set-url origin git@github.com:ReactiveX/RxSwift.git

    将本地工程推送到远程空仓库

    git remote add origin git@github.com:evanxlh/BluetoothCentral.git
    git push -u origin master
    

    Git Submodule

    添加 Submodule

    ​ git submodule add git://github.com:xxx/project.git deps/mysql

    初始化

    ​ git submodule init

    更新

    • git submodule update

    • git submodule update --init --recursive

    直接下载含有submodule的仓库

    git clone git@github.com:xxx/pod-project.git --recursive

    Git分支

    列出分支

    • 列出所有本地分支

      git branch

    • 列出远程本地分支

      git branch -r

    • 列出远程分支和标签

      git ls-remote

    分支创建与切换

    • 创建 hotfix 分支

      git branch hotfix

    • 创建并切换分支

      git checkout -b hotfix

      git checkout -b <new-branch> <existing-branch>

    • 切换到hotfix分支

      git checkout hotfix

    删除分支

    • 删除本地分支

      git branch -D hotfix

    • 删除远程hotfix分支

      git push origin --delete hotfix

    • 重命名分支

      git branch -M hotfix1

    跟踪远程分支

    跟踪远程分支,就可以在当前分支上直接使用 git push, git fetch, git pull 了。有以下几种方式:

    • 方法一:

      git checkout --track origin/branch_name

    • 方法二: 通过修改 config 文件来完成

      git config -e, 然后向 config 中添加以下语句:

      [branch "branch_name"]
        remote = origin
        merge = refs/heads/branch_name
      
    • 方法三:

      git push --set-upstream origin develop

    分支合并

    当我们想把自己的分支修改合并到远程分支,我们就要进行分支合并了。分支代码合并有两种方式:git mergegit rebase。接下来我们结合实例来说明它们各自的合并流程,假如我们要将自己的工作分支 me 的修改合并到 develop 分支:

    git rebase

    rebase 能够按代码提交的时间顺序在目标分支上进行重演,就算合并产生冲突也能完好的保留历史结点。它的流程为:

    1. 切换到 develop 分支,git pull 与服务器同步
    2. 切换到自己的工作分支: git checkout me
    3. 将自己的工作分支的修改 rebase 到 develop分支: git rebase develop
    4. 如果有冲突,解决冲突,并提交代码
    5. 再次切换到 develop 分支,将自己的工作分支合并到 develop 分支: git merge me,然后 git push 就行

    git merge

    如果不是 fast forward 时,git merge 会自动额外的一个 merge commit。尤其在有冲突的情况下,会把它人的 commits 合成一个 merge commit 。所以在非 fast forward 合并时,强烈推荐使用 git rebase

    merge 的流程为:

    1. 切换到 develop 分支,git pull 与服务器同步

    2. 执行合并: git merge me

    3. 如果有冲突,解决冲突,并提交代码

    4. 将合并结果推送到远程服务器 git push

    Git 标签

    删除 tag

    • 删除本地所有tags,保持跟服务tag一致

      git fetch origin --prune --tags

    • 将本地 tag: 1.0 推送到服务器

      git push origin 1.0

    • 将本地所有 tag 推送到服务器

      git push --tags

    • 删除本地 tag

      git tag -d 1.0

    列出本地 tag

    ​ git tag -l

    代码修改及提交

    忽略不想要提交的文件

    • 将不想要提交的文件加入 .gitignore 中

      vim .gitignore

    • 提交 .gitignore

      git add .gitignore

      git commit -m "Add .gitignore"

    将代码加入 git 跟踪管理

    • 将指定文件加入修改跟踪区(可以是单个或多个文件)

      git add file1 file2

    • 将所有文件加入修改跟踪区

      git add .

    提交本地修改

    • 提交 git 修改跟踪区里的内容

      git commit -m "human readable comment"

    • 提交所有本地修改

      git commit -a -m "human readable comment"

    暂存修改的代码

    • 将当前的修改保存到Git栈中,备份当前的工作区内容

      git stash

    • 从Git栈中读取最近一次保存的内容,并且恢复工作区的相关内容

      git stash pop

    • 显示Git栈中所有的备份

      git stash list

    • 清空Git栈中所有备份

      git stash clear

    • 取出版本号为stash@{1}的暂存内容,但并未将此记录从Git栈中清除

      git stash apply stash@{1}

    Git Patch

    详情可参考这里:Git命令解析-patch、apply、diff

    创建 Patch

    创建好的 patch 会保存在你的当前工作目录下。

    • 生成最近的1次commit的patch

      git format-patch HEAD^

    • 生成最近的2次commit的patch

      git format-patch HEAD^^

    • 生成最近的3次commit的patch

      git format-patch HEAD^^^

    • 生成最近的4次commit的patch

      git format-patch HEAD^^^^

    • 生成两个commit间的修改的patch (包含两个commit. <r1>和<r2>都是具体的commit号)

      git format-patch <r1>..<r2>

    • 生成单个commit的patch

      git format-patch -1 <r1>

    • 生成某commit之后的所有修改patch(不包含该commit)

      git format-patch <r1>

    • 生成从根到r1提交的所有patch

      git format-patch --root <r1>

    应用 Patch

    ​ git apply 0001-Add-rspec-to-gemfile.patch

    代码恢复

    reflog 是 git 提供的一个内部工具,用于记录对git仓库进行的各种操作。

    查看操作日志,以下两种方式都可以

    • git reflog show
    • git log -g

    进行恢复

    git reset --hard commit_id_you_want_to_reset

    成功的 Git 分支模型

    如果你还在探索如何高效的利用分支开发时,这里我强烈推荐下以下两篇文章:

    A successful Git branching model

    Git Feature Branch Workflow

    意外发现

    即将发布本文时,意外发现个 Git的奇技淫巧 ,英文版请点这里:Git Tips

    相关文章

      网友评论

          本文标题:Git 命令便捷手册

          本文链接:https://www.haomeiwen.com/subject/mxcrkktx.html