GitHub 使用笔记

Git设置

1
2
3
4
5
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email <username>@users.noreply.github.com
$ git config --global color.ui auto #提高命令的可读性
$ git config --global http.proxy socks5://127.0.0.1:7890
#设置代理

注:<username>@users.noreply.github.com<username>为github用户名,这个服务可以防止邮箱泄露

此文件会在~/.gitconfig中

1
2
3
4
5
6
7
8
[http]
proxy = socks5://127.0.0.1:7890

[user]
email = <username>@users.noreply.github.com
name = xxx
[color]
ui = auto

设置SSH Key

生成ssh key

1
$ ssh-keygen -t rsa -C "<username>@users.noreply.github.com"

添加到github中: Settings–SSH and GPG Keys–Add SSH Key,粘贴 id_rsa.pub

验证:

1
ssh -T git@github.com

提示成功:

1
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

Git简单操作

基本操作

  • clone仓库

    这个帖子 , github取消了使用http+帐号密码的命令行认证 , 验证身份需要ssh key+ssh方式(如下)

1
$ git clone git@github.com:xxx/Hello-World.git
  • hi.txt

  • 查看状态(未提交)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ git status 
    位于分支 main
    您的分支与上游分支 'origin/main' 一致。

    未跟踪的文件:
    (使用 "git add <文件>..." 以包含要提交的内容)
    hi.txt

    提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
  • 提交

    • 通过 git add命令将文件加入暂存区( 在 Index 数据结构中记录文件提交之前的状态 )
    • 再通过 git commit命令提交。
    1
    2
    3
    4
    5
    $ git add hi.txt 
    $ git commit -m "First commit"
    [main 7817ced] hi.txt
    1 file changed, 1 insertion(+)
    create mode 100644 hi.txt
    • git log 查看日志
    1
    2
    3
    4
    5
    6
    7
    $git log

    commit 7817ced43274a11acfd5df302e6b35fd52351cf5 (HEAD -> main)
    Author: xxx <xxx@users.noreply.github.com>
    Date: Fri Jan 21 14:12:58 2022 +0800

    hi.txt
    • 只显示提交信息的第一行 $ git log --pretty=short
    • 只显示指定目录、文件的日志 $ git log README.md
    • 显示文件的改动 $ git log -p
    • git push提交到远程
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ git push                   
    枚举对象中: 4, 完成.
    对象计数中: 100% (4/4), 完成.
    使用 8 个线程进行压缩
    压缩对象中: 100% (2/2), 完成.
    写入对象中: 100% (3/3), 283 字节 | 283.00 KiB/s, 完成.
    总共 3 (差异 0),复用 0 (差异 0)
    To github.com:ouyen/hi-test.git
    e94beb7..7817ced main -> main
    • git init——初始化仓库
    • git diff——查看更改前后的差别

分支的操作

master 分支— Git 默认创建的分支

  • git branch 将分支名列表显示

    1
    2
    $ git branch
    * master
  • git checkout -b——创建、切换分支

    • 切换到 feature-A 分支并进行提交

      1
      2
      $ git checkout -b feature-A
      Switched to a new branch 'feature-A'

      此命令等同于 :

      1
      2
      $ git branch feature-A
      $ git checkout feature-A

      现在:

      1
      2
      3
      $ git branch
      * feature-A
      master
  • 切换回上一个分支 (- 代替上一个分支名)

    1
    2
    $ git checkout -
    Switched to branch 'feature-A'
  • git merge——合并分支

    1
    2
    3
    4
    5
    6
    $ git checkout master
    Switched to branch 'master'
    $ git merge --no-ff feature-A
    Merge made by the 'recursive' strategy.
    README.md | 4 ++++
    1 file changed, 4 insertions(+)
  • git log --graph——以图表形式查看分支

  • 本地分支关联一个远程分支 : 后面是远程分支(没有会自动创建)

1
git push origin feature-A:feature-A

更改提交的操作

  • git reset——回溯历史版本

  • 回溯到创建 feature-A 分支前 (hash可以通过git log查看 , 输入 4 位以上就可以执行)

    1
    2
    $ git reset --hard fd0cbf0d4a25f747230694d95cac1be72d33441d
    HEAD is now at fd0cbf0 Add index
  • 创建 fix-B 分支

    1
    2
    3
    4
    $ git checkout -b fix-B
    $ vi README.md
    $ git add README.md
    $ git commit -m "Fix B"

    目标 : 主干分支合并 feature-A 分支的修改后,又合并了 fix-B 的修改。

  • 推进至 feature-A 分支合并后的状态

  • git reflog 命令,查看当前仓库执行过的操作的日志。

    1
    2
    3
    $ git checkout master
    $ git reset --hard 83b0b94
    HEAD is now at 83b0b94 Merge branch 'feature-A'
  • 消除冲突

    1
    2
    3
    4
    $ git merge --no-ff fix-b    
    自动合并 README.md
    冲突(内容):合并冲突于 README.md
    自动合并失败,修正冲突然后提交修正的结果。

    此时README.md为:

    1
    2
    3
    4
    5
    6
    7
    8
    $ cat README.md
    # hi-test

    <<<<<<< HEAD
    ## feature-A
    =======
    ## fix-b
    >>>>>>> fix-b

    vi README.md解决冲突

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ cat README.md
    # hi-test

    ## feature-A

    ## fix-b

    $ git add *

    $ git commit -m "Fix conflict"
    [main 59e1d9f] Fix conflict

    git commit --amend——修改提交信息

    查看git log:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    *   commit 45e11a5b422d9eb789ae4ee679341094a124807c (HEAD -> main)
    |\ Merge: 7a27296 867fc3e
    | | Author: ouyen <ouyen@users.noreply.github.com>
    | | Date: Fri Jan 21 19:30:31 2022 +0800
    | |
    | | Merge branch 'fix-B'
    | |
    | * commit 867fc3edd779fb32096561afae0a3f3e5bd51add (fix-b)
    | | Author: ouyen <ouyen@users.noreply.github.com>
    | | Date: Fri Jan 21 19:10:43 2022 +0800
    | |
    | | FixB
    | |
    * | commit 7a2729633efe6f92ad2367fc28f9e5a6b23b4bc7
    |\ \ Merge: 0fc2d10 0bd75de
    | |/ Author: ouyen <ouyen@users.noreply.github.com>
    |/| Date: Fri Jan 21 14:41:36 2022 +0800
    | |
    | | Merge branch 'feature-A' into main
    | |
    | * commit 0bd75de4595faf544b2cd39d81e72e793cfaf6a6 (feature-A)
    |/ Author: ouyen <ouyen@users.noreply.github.com>
    | Date: Fri Jan 21 14:36:13 2022 +0800
    |
    | F-A
    |
    * commit 0fc2d10b3b8d8f3c7dd5bedc5b6593ee17cc40ef (origin/main, origin/HEAD)
    | Author: ouyen <ouyen@users.noreply.github.com>
    | Date: Fri Jan 21 14:27:13 2022 +0800
    |
    | hi.txt
    |
    * commit 7817ced43274a11acfd5df302e6b35fd52351cf5
    | Author: ouyen <ouyen@users.noreply.github.com>
    | Date: Fri Jan 21 14:12:58 2022 +0800
    |
    | hi.txt
    |
    * commit e94beb7569914b999c1611abe26727deab22627d
    Author: ouyen <60745334+ouyen@users.noreply.github.com>
    Date: Fri Jan 21 14:08:22 2022 +0800

    Create README.md
  • git rebase -i ——压缩历史

    1
    2
    3
    4
    5
    6
    7
    8
    $ git checkout -b feature-C
    $ vi README.md
    - feature-A
    - fix-B
    - faeture-C
    $ git commit -am "Add feature-C"
    $ vi README.md(修正拼写错误)
    $ git commit -am "Fix typo"
    • 更改历史

      选定当前分支中包含HEAD(最新提交)在内的两个最新历史记录为对象,并在编辑器中打开。

      1
      $ git rebase -i HEAD~2

      1
      2
      3
      pick 7a34294 Add feature-C
      pick 6fba227 Fix typo
      # ...

      改为

      1
      2
      3
      pick 7a34294 Add feature-C
      fixup 6fba227 Fix typo
      #...

      提示如下,rebase成功

      1
      Successfully rebased and updated refs/heads/feature-C.

      将feature-C合并到master

      1
      2
      $ git checkout master
      $ git merge --no-ff feature-C

推送到远程仓库

1
2
$ git remote add origin git@github.com:xxx/xxx.git #添加远程仓库
$ git push -u origin master # 推送给远程仓库
  • 推送至 master 以外的分支

    1
    2
    $ git checkout -b feature-D
    $ git push -u origin feature-D

从远程仓库获取

1
2
3
4
5
6
$ git clone git@github.com:github-book/git-tutorial.git
$ git branch -a #查看branch信息
$ git checkout -b feature-D origin/feature-D #获取远程的feature-D分支
$ vi xxx ...
$ git commit -am "Add feature-D"
$ git push

获取最新的远程仓库分支

1
$ git pull origin feature-D

添加一个子git库:

1
git submodule add https://github.com/AveYo/MediaCreationTool.bat.git MediaCreationTool

Removing sensitive data from a repository

github : https://docs.github.com/cn/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

摘抄如下 :

Using the BFG

The BFG Repo-Cleaner is a tool that’s built and maintained by the open source community. It provides a faster, simpler alternative to git filter-branch for removing unwanted data.

For example, to remove your file with sensitive data and leave your latest commit untouched, run:

1
$ bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA

To replace all text listed in passwords.txt wherever it can be found in your repository’s history, run:

1
$ bfg --replace-text passwords.txt

After the sensitive data is removed, you must force push your changes to GitHub. Force pushing rewrites the repository history, which removes sensitive data from the commit history. If you force push, it may overwrite commits that other people have based their work on.

1
$ git push --force

See the BFG Repo-Cleaner’s documentation for full usage and download instructions.

https://rtyley.github.io/bfg-repo-cleaner/

more abour git

GitHub

github cli

官方文档 安装指南

Debian, Ubuntu Linux, Raspberry Pi OS (apt)

Install:

1
2
3
4
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh

zsh 自动补全(参考链接)

Check if the completion folder is in your fpath, this should be the case with Oh-My-Zsh:

1
2
3
4
print -l $fpath | grep completion
/home/andrej/.oh-my-zsh/custom/plugins/git-flow-completion
/home/andrej/.oh-my-zsh/completions
/usr/share/zsh/vendor-completions

Create the completions folder if not it is not existing:

1
mkdir ~/.oh-my-zsh/completions

Create auto-completion script for GitHub CLI:

1
gh completion -s zsh > ~/.oh-my-zsh/completions/_gh

Add this to your ~/.zshrc (Source: https://cli.github.com/manual/gh_completion)

1
2
3
# github cli
autoload -U compinit
compinit -i

使用

  1. 使用gh auth login 登陆github

  2. 个人常用的一些gh命令(随缘更新)

    gh gist xxxx --public 将xxxx文件上传到gist 若不加public参数则为secert

关于github随记:

BTS: 管理 Issue 的系统称为 BTS(Bug Tracking System,BUG 跟踪系统)。当今具有代表性的 BTS 有 Redmine 、Trac 、Bugzilla 等。

GFM : GitHub Flavored Markdown Spec (github的markdown规范,官方文档)
GFM中一些extension
+ 表格
+ 任务列表Tasklist

1
2
- [ ] foo
- [x] bar
  • emoji 使用 冒号 “:”

pull request

-------------end-------------