Git 命令便捷手册

2020-07-19  本文已影响0人  evanxlh

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

关闭 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

更新

直接下载含有submodule的仓库

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

Git分支

列出分支

分支创建与切换

删除分支

跟踪远程分支

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

分支合并

当我们想把自己的分支修改合并到远程分支,我们就要进行分支合并了。分支代码合并有两种方式: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

列出本地 tag

​ git tag -l

代码修改及提交

忽略不想要提交的文件

将代码加入 git 跟踪管理

提交本地修改

暂存修改的代码

Git Patch

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

创建 Patch

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

应用 Patch

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

代码恢复

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

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

进行恢复

git reset --hard commit_id_you_want_to_reset

成功的 Git 分支模型

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

A successful Git branching model

Git Feature Branch Workflow

意外发现

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

上一篇下一篇

猜你喜欢

热点阅读