干货-Git的使用

Git的使用

hi, 大家好,我是爱吃香蕉的猴子,记录一下开发中Git使用的常用指令.


Git 配置

  • 教程
  • $ git config --global user.name “John Doe”
  • $ git config --global user.email [email protected]
  • 配置密钥:
    • ssh-keygen -t rsa -C 你的邮箱
    • 一路回车
    • cat ~/.ssh/id_rsa.pub
    • 复制ssh key到github / gerit

Git的常用指令

  • clone 代码:git clone + gitPath
  • git 初始化: git init
  • 查看用户:git config --get-all user.name
    • git config --global user.name “name”
    • git config --global user.email “email”
  • 删除配置:git config --unset --global user.name “name”
  • 查看修改状态: git status
  • 查看修改: git diff
  • 创建分支:git checkout -b branchA
  • 切换分支: git checkout branchB
  • 删除分支: git branch -D branchA
  • 增加新文件: git add new_file
  • 提交文件: git commit -a -m “patch name”
  • 应用补丁: git format-patch -M HEAD^
  • 推送Code:
    • git push origin 本地分支:远程分支
    • 本地目录关联repository:
    • git remote add origin
    • [email protected]:git_username/repository_name.git
      -取消本地目录下关联的远程库 git remote remove origin
  • 回退版本: git reset HEAD^
    • 切版本: git reset --hard [commitId]
    • 现场保存: git stash
    • 现场恢复: git stash apply
  • git apply patch / diff
  • 显示指令: git add -i
  • diff文件: git diff > change.diff .
  • 查看git log:
    • 显示一行:git log --oneline
    • git log --oneline --graph
    • 显示内容: git log -p / git log -p [path]
    • 检索 name / time :
    • git log --author=“Name” --after=“2018-05-21 00:00:00” --before=“2018-05-25 23:59:59”
  • 查看所有的分支: git branch -a
  • 查看分支和远程的关联: git branch -vv

针对应用代码冲突
- git am [patch] --rej ## 冲突的部分形成.rej的文件,然后手动修改
- git merge branchB, ## 就是将branchB中新增的部分merge过来,一般适用于,从other分支合并代码到主分支。
工作常用两个重要指令且有些难理解

  • git cherry-pick 命令的作用,就是将指定的提交(commit)应用于其他分支
    • 上面教程解释的很详细,我通俗可以理解,Master分支,cherry-pick feature分支的提交, 所以工作中很实用。
  • git rebase 代码集成的一种方式
    • 理解:
      • git rebase 可以解决本地和远程的统一问题,or git pull --rebase
      • git rebase 可以解决本地两个分支的同步问题
        • git checkout feature # 将feature的内容合并到master
        • git rebase master # 有冲突 解决冲突
        • git rebase -continue
        • git checkout master
        • git merge feature.

github

首次:
git init
git add README.md
git commit -m "first commit"
git remote add origin git remote add origin https://github.com/caoyongren/Document.git
git push -u origin master

已存在
git commit -a -s //写补丁描述
git format-patch -M HEAD^
git push -u origin master

                                                 Code的搬运工V1.0

如何写commit信息

# 50-character subject line
#
# 72-character wrapped longer description. This should answer:
#
# * Why was this change necessary?
# * How does it address the problem?
# * Are there any side effects?
#
# Include a link to the ticket, if any.
#
# Add co-authors if you worked on this code with others:
#
# Co-authored-by: Full Name <[email protected]>
# Co-reviewed-by: Full Name <[email protected]>
  • google
commit 4b7f43bcae20e5fe0f452098c70359e6d54885c5
Author: jackqdyulei <[email protected]>
Date:   Thu Oct 19 11:05:09 2017 -0700

    Reuse the BluetoothBatteryDrawable in QS detail
    
    BluetoothBatteryDrawable creates the drawable lazily using
    the latest(and most correct) context. So it could get the
    correct color from the theme.
    
    Also add iconScale field in BluetoothBatteryDrawable so the size
    of the battery icon could be scaled.
    
    Bug: 67377756
    Test: Manual - connected to bluetooth headphone and open the qs
    page and detail page. Retest it after changing it to dark theme.
    Change-Id: Ic6c871985a89a503221755eed4bc2605ab53f630
  • 上面的例子是我从github / google 上找到的,现在我们来分析一下,commit信息需要哪几个部分
      1. 50-character subject line # 一个简短的主题描述 一行
      1. 问题描述 # 每行不超过72个字母
      • 问题根本原因
      • 解决方案
      • 相关影响
      1. test: 就是自己测试的结果
      1. BugId featureId

                                                Code的搬运工V2.0

猜你喜欢

转载自blog.csdn.net/qq_20608169/article/details/112101675
今日推荐