原英文版: Contribute to repositories on GitHub
向Github开源项目贡献代码
前言
以最近接触的测试开源项目Robottelo为例,整理向GitHub开源项目贡献代码的步骤、方法及注意事项,备查,未完待续…
Git配置
- 创建SSH key
$ ssh-keygen -t rsa -C "youremail@example.com" cat ~/.ssh/id_rsa.pub - 登陆GitHub页面 -> Account Settings -> SSH and GPG Keys -> SSH keys -> New SSH key -> Add SSH key,创建SSH key
- 配置 username and email
$ git config user.name “username” $ git config user.email “username@emil.con” $ git config --list
创建Env环境
- 克隆仓库
git clone git@github.com:SatelliteQE/airgun.git - 创建独立的env环境(避免与本机当前的环境产生冲突)
virtualenv (--no-site-packages) venv source venv/bin/activate (venv) $
贡献代码
复刻仓库
- 查看remote的仓库
(master) $ git remote -v
origin git@github.com:hkx303/robottelo.git (fetch)
origin git@github.com:hkx303/robottelo.git (push)
- 将Robottelo作为远程仓库
(master) $ git remote add upstream git@github.com:SatelliteQE/robottelo.git
- 检查remote仓库
(master) $ git remote -v
origin git@github.com:hkx303/robottelo.git (fetch)
origin git@github.com:hkx303/robottelo.git (push)
upstream git@github.com:SatelliteQE/robottelo.git (fetch)
upstream git@github.com:SatelliteQE/robottelo.git (push)
- 拉取最新的upstream的代码
(master) $ git fetch upstream
- 变基:复刻的master分支的代码 将分支历史并入upstream主线
(master) $ git rebase upstream/master
创建分支
- 在本地创建分支
(master) $ git checkout -b branch_name
或
(master) $ git branch branch_name
(master) $ git checkout branch_name
- 查看更改情况
(branch_name) $ git status
(branch_name) $ git diff
- 在本地仓库中提交代码,更新详细的提交信息
(branch_name) $ git add <all modified or new files>
(branch_name) $ git commit
Add more information about what the entire commit is about.
提交代码
- 使用 pycodestyle-3工具来检查代码风格
pip install pycodestyle-3 - 检查代码
pycodestyle-3 --show-source --first <filename> - 提交代码
(branch_name) $ git push origin branch_name - 通过github的链接提交申请
更新提交申请
- 更新提交申请
(branch_name) $ pycodestyle-3 --show-source --first <filename> (branch_name) $ git add <files> (branch_name) & git commit -m "messages" (branch_name) $ git push origin branch_name
无法合并
- 使自身的master分支与上游分支同步
(branch_name) $ git checkout master (master) $ git fetch upstream (master) $ git rebase upstream/master (master) $ git push - 使我们的工作目录与master分支同步
(master) $ git checkout branch_name (branch_name) $ git rebase master - 提交分支
(branch_name) $ git push origin branch_name - 提交时提示commit历史被修改——使用force
(branch_name) $ git push origin branch_name --force
撤销提交
- 已经更改文件但需要恢复这些更改,并且不需要保留更改
(branch_name) $ git checkout -- FILE - 已经更改文件,运行了 git add FILE 命令,但想要恢复,同时需要保留所做的更改
(branch_name) $ git reset HEAD FILE - 已经做出更改,且commit了所有的更改,但想要恢复:
- 需要保留所作的更改
(branch_name) $ git reset --soft HEAD~- 不需要保留所作的更改
(branch_name) $ git reset --hard HEAD~
注意: HEAD~ 代表最后一次commit。如果想要恢复最近的3次commit,把HEAD~ 更改为 HEAD~3.
Git的其他命令
查看历史
- 查看git提交历史
git log - 查看git提交历史,减少输出信息
git reflog - 查看git历史命令
git reflog - 查看git分支图表
git log --graph
关于分支
- 合并指定分支至当前分支
git merge <branch_name> - 删除分支
git branch -d <branch_name>
Git remove
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
Git stash
- 把当前工作现场“储藏”起来,等以后恢复现场后继续工作:
$ git stash - 查看stash列表
$ git stash list - 恢复,不删除stash内容
$ git stash apply - 恢复的同时删除stash内容
$ git stash pop - 恢复指定的stash
$ git stash apply stash@{0}
cherry-pick
- 复制一个特定的提交到当前分支
$ git cherry-pick 4c805e2
本文详细介绍如何向GitHub上的开源项目贡献代码,包括Git配置、创建环境、复刻仓库、创建分支、提交代码、处理合并冲突等关键步骤。适用于新手开发者了解GitHub协作流程。
701

被折叠的 条评论
为什么被折叠?



