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,在
Settings
→SSH 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设置
- 在设置里搜索 github,GitHub登录,IDEA会自动处理;使用 ssh 克隆 Git 仓库
- Subversion → SSH→ 设置私钥
Github使用
基本认知
github
分为3个部分:Remote
、Local
和Disk
。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
最佳实践
###克隆远程仓库到本地
首次
clone
,Remote
、Local
和Disk
全都保持一致。
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
若当主分支与其他分支代码冲突时
- 发现远程和本地仓库代码存在版本冲突
Remote
:
main(master)
:改变了,存在other-commit
new-feature
:存在new-commit
Local
:
main(master)
:未改变new-feature
:存在new-commit
Disk
:
- 当前为
new-feature
分支:存在new-commit
。
other-commit
和 new-commit
存在代码差异。为了保证代码依然有效,需要切换到主分支验证下。
- 切换本地仓库主分支
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
- 同步远程仓库代码
Remote
:
main(master)
:other-commit
new-feature
:new-commit
Local
:
main(master)
:other-commit
new-feature
:new-commit
Disk
:
- 当前为
main(master)
分支:为Local
下,已同步的main(master)
。
git pull origin master
- 切回
new-feature
分支
Remote
:
main(master)
:other-commit
new-feature
:new-commit
Local
:
main(master)
:other-commit
new-feature
:new-commit
Disk
:
- 当前为
new-feature
分支:new-commit
git checkout new-feature
- 尝试在
main
分支上 合并代码
Remote
:
main(master)
:other-commit
new-feature
:new-commit
Local
:
main(master)
:other-commit
new-feature
:other-commit
→new-commit
Disk
:
- 当前为
new-feature
分支:在other-commit
基础上尝试合并new-commit
提交
git rebase main
如果出现了
rebase conflict
就需要人工干预,测试代码是否可以合并。成功的话,则会将代码正常合并到
Local
中。
- 推送本地的
new-feature
到远程的new-feature
分支中
Remote
:
main(master)
:other-commit
new-feature
:other-commit
→new-commit
Local
:
main(master)
:other-commit
new-feature
:other-commit
→new-commit
Disk
:
- 当前为
new-feature
分支:other-commit
→new-commit
git push -f origin new-feature
- New pull request
发起一个
pull request
给主分支
远程仓库合并分支
Squash and merge
:将某个分支上的所有改变合并成一个,并提交到主分支。
删除合并后的分支
远程仓库合并主分支完毕后,
delete branch
删除其new-feature
分支。切换
Local
的main(master)
。删除new-feature
分支# 切换main分支 git checkout main # 删除new-feature分支 git branch -D new-feature
同步远端仓库代码
git pull origin master
远程仓库同步到本地
Remote
:
main(master)
:update-commit
Local
:
main(master)
:update-commit
Disk
:
- 当前为
main(master)
分支:update-commit