19.《Bioinformatics-Data-Skills》之
2021-06-11 本文已影响0人
DataScience
《Bioinformatics-Data-Skills》之github协作(2)
上文介绍了如何进行Github仓库创建与如何将本地工程push到仓库,今天我们学习一下剩下的操作:
- 作为同事如何对仓库进行克隆
- 交互地进行push与pull操作来协同工作
- 遇到conflict问题如何解决
- 了解github的fork与pull request特性
1. 对协同的github库进行克隆
假如你是你的同事,你可以通过下面的命令将仓库克隆到新的文件夹zmays-snp-partner
:
git clone git@github.com:HushWay/zmays-snps.git zmays-snp-partner
# Cloning into 'zmays-snp-partner'...
# remote: Enumerating objects: 26, done.
# remote: Counting objects: 100% (26/26), done.
# remote: Compressing objects: 100% (18/18), done.
# remote: Total 26 (delta 6), reused 25 (delta 5), pack-reused 0
# Receiving objects: 100% (26/26), done.
# Resolving deltas: 100% (6/6), done.
2. 交互进行push与pull操作
回到自己的身份,现在你在README.md文件上添加了一些信息,commit并push到远程库:
echo "样本数据测序于2021-6-8" >> README.md
git commit -a -m "添加样本测序信息"
git push origin master
# Counting objects: 3, done.
# Delta compression using up to 6 threads.
# Compressing objects: 100% (3/3), done.
# Writing objects: 100% (3/3), 360 bytes | 360.00 KiB/s, done.
# Total 3 (delta 1), reused 0 (delta 0)
# remote: Resolving deltas: 100% (1/1), completed with 1 local object.
# To github.com:HushWay/zmays-snps.git
# bb2fee4..326c4b4 master -> master
切换到同事身份,现在你需要通过pull命令将远程的改变同步到本地:
cd ../zmays-snp-partne
git pull origin master
# remote: Enumerating objects: 5, done.
# remote: Counting objects: 100% (5/5), done.
# remote: Compressing objects: 100% (2/2), done.
# remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
# Unpacking objects: 100% (3/3), done.
# From github.com:HushWay/zmays-snps
# * branch master -> FETCH_HEAD
# bb2fee4..326c4b4 master -> origin/master
# Updating bb2fee4..326c4b4
# Fast-forward
# README.md | 1 +
# 1 file changed, 1 insertion(+)
通过git log
命令(参数为了简化显示)确认当前为最新版本:
git log -n 2 --pretty=oneline --abbrev-commit
# 326c4b4 (HEAD -> master, origin/master, origin/HEAD) 添加样本测序信息
# bb2fee4 README文件版本重置
作为同事,你也可以修改与提交信息:
echo $'\n\n参考基因组版本为:version3' >> README.md
git commit -a -m "添加参考基因组信息"
# [master db64487] 添加参考基因组信息
# 1 file changed, 3 insertions(+)
git push origin master
3. 处理conflict
现在回到自己的身份,假如我们在同一文件的同一地方添加了信息,并进行push:
cd ../zmays-snp
echo $'\n\n参考基因组下载于www.asdfl.com' >>README.md
git commit -a -m "添加参考基因组信息(2)"
git push origin master
# To github.com:HushWay/zmays-snps.git
# ! [rejected] master -> master (fetch first)
# error: failed to push some refs to 'git@github.com:HushWay/zmays-snps.git'
# hint: Updates were rejected because the remote contains work that you do
# hint: not have locally. This is usually caused by another repository pushing
# hint: to the same ref. You may want to first integrate the remote changes
# hint: (e.g., 'git pull ...') before pushing again.
# hint: See the 'Note about fast-forwards' in 'git push --help' for details.
这里报了一个错误,但需要注意这里的错误可不是因为文件引起的,而是前面你的同事先提了一个push。先来后到,你得先将远端的库pull到本地:
git pull origin master
# From github.com:HushWay/zmays-snps
# * branch master -> FETCH_HEAD
# Auto-merging README.md
# CONFLICT (content): Merge conflict in README.md
# Automatic merge failed; fix conflicts and then commit the result.
这里的信息表明文件存在conflicts,需要手动处理。
我们可以通过以下4个步骤来解决冲突问题:
- 使用
git status
定位起冲突的文件
git status
# On branch master
# You have unmerged paths.
# (fix conflicts and run "git commit")
# (use "git merge --abort" to abort the merge)
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: README.md
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
#
# no changes added to commit (use "git add" and/or "git commit -a")
这里表明README.md被同时修改了。
- 手动处理冲突部分内容
查看文件,发现冲突的部分已经被明显地与其它部分隔开(如下)
cat README.md
# <<<<<<< HEAD
# 参考基因组下载于www.asdfl.com
# =======
# 参考基因组版本为:version3
# >>>>>>> db644875d66206bb9e21c6a261df21e76941ec7f
我们可以将文件冲突部分修改成下面的内容:
参考基因组版本为:version3,下载于www.asdfl.com
- 修改完冲突文件后进行
git add
git add README.md
- 最后通过
git status
命令确认冲突完全解决
git status
# On branch master
# All conflicts fixed but you are still merging.
# (use "git commit" to conclude merge)
#
# Changes to be committed:
#
# modified: README.md
解决完冲突之后尽快push文件到远程库,以便其它人进行接下来的修改:
git commit -a -m "解决README冲突问题"
git push origin master
4. Github其它特性:fork与pull request
Github的fork操作可以将别人的公开的库拷贝到自己的仓库下进行与主库相独立的开发。当你的对文件的修改意见想要与主库进行共享的话也可以提交一个pull request。主库的开发者会选择性地接受改动,以避免bug的存在。与我们前面介绍的方式不同的是,前面的方式你与同事作为共同开发者拥有相同的独立提交权限。