Github速查笔记

Github注册配置

注册

按网站操作提示进行,https://github.com/signup

配置SSH登录

生成密钥

# yuanqimanong 替换自己的名字
ssh-keygen -t rsa -b 4096 -C yuanqimanong

# 一路回车 记住密钥保存的位置
# C:\Users\Administrator\.ssh

# id_rsa      私钥
# id_rsa.pub  公钥

添加公钥到Github

登录Github,在SettingsSSH and GpG keys中添加id_rsa.pub的内容到SSH keys

测试

ssh -T git@github.com

Git Bash设置

%USERPROFILE%下增加.profile文件,写入如下内容

GIT_SSH为本机实际地址,这个代码主要目的是启动GitBash后自启动ssh agent服务

SSH_ENV="$HOME/.ssh/environment"
GIT_SSH="D:\Program Files\Git\usr\bin\ssh.exe"

function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cygwin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi

TortoiseGit设置

软件设置 → 网络 → SSH客户端 → 选定Git\usr\bin\ssh.exe会自动选定

IDEA设置

  1. 在设置里搜索 github,GitHub登录,IDEA会自动处理;使用 ssh 克隆 Git 仓库
  2. Subversion → SSH→ 设置私钥

Github使用

基本认知

  • github分为3个部分:RemoteLocalDisk
  • Remote:为远程仓库,main(主分支)为master
  • Local:为本地仓库。
  • Disk:为本地磁盘。

基本命令

# 设置签名
## 项目级别 保存在 .git/config中
git config user.name xxx1
git config user.email xxx1@xx.com
## 全局用户级别
git config --global user.name xxx2
git config --global user.email xxx2@xx.com

# 初始化
git init

# 添加提交文件
git add .
git add <changed_files>

# 添加提交信息
git commit -m "msg"

# 查看历史记录
git log

# 检出最后一次提交的文件
git checkout HEAD <changed_files>

# 查看config
## 系统级别
git config --system --list
## 全局用户级别
git config --global --list
## 项目级别
git config --local --list

# 设置和取消代理(解决 Failed to connect to github.com port 443:connection timed out)
## 设置代理(端口号自行修改)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
## 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

最佳实践

###克隆远程仓库到本地

首次cloneRemoteLocalDisk 全都保持一致。

git clone https://github.com/example/example/git

创建分支

准备修改代码前,要先建立一个新的 feature branch 。目的是保证主分支的代码安全。

Remote

  • main(master):未改变

Local

  • main(master) :未改变
  • new-feature :未改变

Disk

  • 当前为将要修改的 new-feature 分支
git checkout -b new-feature

修改代码

修改代码后,需要查看本次修改。确认好要修改的文件

git diff .

添加本次修改的代码到 暂存区

git add <changed_files>

推送代码到本地仓库

暂存区 的代码,提交到 Local 中。

Remote

  • main(master):未改变

Local

  • main(master):未改变
  • new-feature:存在 new-commit

Disk

  • 当前为 new-feature 分支:执行提交命令,将 new-commit 提交到 Local
git commit

推送代码到远程仓库

new-commit 提交到Remote中。

Remote

  • main(master):未改变
  • new-feature:存在 new-commit

Local

  • main(master):未改变
  • new-feature:存在 new-commit

Disk

  • 当前为 new-feature 分支:存在 new-commit
git push origin new-feature

若当主分支与其他分支代码冲突时

  1. 发现远程和本地仓库代码存在版本冲突

Remote

  • main(master):改变了,存在 other-commit
  • new-feature:存在 new-commit

Local

  • main(master):未改变
  • new-feature:存在 new-commit

Disk

  • 当前为 new-feature 分支:存在 new-commit

other-commitnew-commit 存在代码差异。为了保证代码依然有效,需要切换到主分支验证下。

  1. 切换本地仓库主分支

Remote

  • main(master):存在 other-commit
  • new-feature:存在 new-commit

Local

  • main(master):未改变
  • new-feature:存在 new-commit

Disk

  • 当前为 main(master) 分支:为 Local 下,未改变的 main(master)
git checkout main
  1. 同步远程仓库代码

Remote

  • main(master)other-commit
  • new-featurenew-commit

Local

  • main(master)other-commit
  • new-featurenew-commit

Disk

  • 当前为 main(master) 分支:为 Local 下,已同步的 main(master)
git pull origin master
  1. 切回 new-feature 分支

Remote

  • main(master)other-commit
  • new-featurenew-commit

Local

  • main(master)other-commit
  • new-featurenew-commit

Disk

  • 当前为 new-feature 分支:new-commit
git checkout new-feature
  1. 尝试在 main 分支上 合并代码

Remote

  • main(master)other-commit
  • new-featurenew-commit

Local

  • main(master)other-commit
  • new-featureother-commitnew-commit

Disk

  • 当前为 new-feature 分支:在other-commit 基础上尝试合并new-commit提交
git rebase main

如果出现了rebase conflict 就需要人工干预,测试代码是否可以合并。

成功的话,则会将代码正常合并到 Local 中。

  1. 推送本地的 new-feature 到远程的 new-feature分支中

Remote

  • main(master)other-commit
  • new-featureother-commitnew-commit

Local

  • main(master)other-commit
  • new-featureother-commitnew-commit

Disk

  • 当前为 new-feature 分支:other-commitnew-commit
git push -f origin new-feature
  1. New pull request

发起一个pull request 给主分支

远程仓库合并分支

Squash and merge:将某个分支上的所有改变合并成一个,并提交到主分支。

删除合并后的分支

  1. 远程仓库合并主分支完毕后,delete branch 删除其 new-feature 分支。

  2. 切换 Localmain(master)。删除 new-feature 分支

    # 切换main分支
    git checkout main
    
    # 删除new-feature分支
    git branch -D new-feature
  3. 同步远端仓库代码

    git pull origin master

    远程仓库同步到本地


Remote

  • main(master)update-commit

Local

  • main(master)update-commit

Disk

  • 当前为 main(master) 分支:update-commit

Github速查笔记
https://元气码农少女酱.我爱你/3c3471b0e1e0/
作者
元气码农少女酱
发布于
2023年5月2日
许可协议