Using GitHub to Share with SparkFun

Introduction

In early 2013, SparkFun made the decision to share all of the code and design files for our products via GitHub. While we’ve always striven to provide as much information on our products as possible (including complete source files; after all, we are all about Open Source!), in the past, that’s been a very one-way channel. Our customers can see the source, download it, and use it as they please, but there’s been no (easy) way for them to share changes with us (and, by extension, with the rest of the community).

By moving our product source to GitHub, we hope to change that. Obviously, the sheer number of products we have means that migrating them to GitHub is a slow process; in fact, we’re not really trying to get all of our older products moved over at all. New products (those we design in house, anyway) will always launch with a GitHub repo containing at a minimum the source files for the product and most likely containing example code, getting started information, and a (curated, but open) wiki that can be used to exchange information with other users about the product.

What is git?

git

Git is a source control package originally developed by Linus Torvalds for tracking changes during development of the Linux kernel. It’s intended to make collaboration on projects easier by allowing locally stored repositories to be synchronized against a remotely stored master copy.

As is so often the case, the choice of source control package tends to be a deeply personal and subjective matter. We chose git (and GitHub) because they offer good support for all the tools we use at a reasonable price and are free for our customers to use to interact with us.

So what’s GitHub, then?

[图片上传失败...(image-92d28e-1545479999803)]

GitHub is, at it’s most basic, a web-based collaboration tool based on the git source control package. It allows multiple users to access git projects (called “repositories”), track changes, manage revisions, and merge with each other’s different version.

The demarcation between git and GitHub can be fuzzy at times, until you get used to the tools. In general, things happening on the command line are using the git tool, and GitHub interactions will be done through the web page interface.

Suggested Reading

This tutorial isn’t meant to get you up to speed on how to use git, in general- it’s really just a step-by-step guide to show you how to make a change in a SparkFun repository and share that change with us. If you want to learn more about git and GitHub in general, we’ve got a great tutorial about GitHub. Git is a phenomenal tool, even if you’re not collaborating. Once you get the hang of it, it’s super easy to use and extremely powerful.

Gitting Started

We’re going to use the command line tools for git for this tutorial. While it may feel a bit archaic, the git command line interface has a couple of serious advantages:

  • The command line interface is truly multiplatform. Following these instructions on a Mac, PC, or Linux box will all yield the same results.
  • The command line interface is stable. There are lots of gui-based interfaces out there, and they change regularly. Even the official GitHub gui changes relatively often.
  • The command line interface is predictable. It doesn’t do things you don’t ask it to do, as some graphical interfaces do.

Install the GitHub Client

Visit the GitHub website, sign up for an account if you don’t already have one, and install the client. I’m not going to go into the details of doing this here, because there are better instructions on their website. Go ahead and get this taken care of; I’ll wait.

Once you’ve finished your installation, open a Git Shell window. Again, I’ll let you follow their instructions on how to do this. I’m working in Windows 7, but the appearance of the command line window should be pretty similar no matter which operating system you’re using. Regardless of the details (for example, the command line prompt you see), the git commands should behave the same way.

Once you’ve got the shell open, go ahead and type “git” and hit enter. The result should look something like this:

If you don’t see the command summary, you’re in the wrong kind of shell. Make sure that you launched a Git Shell, which should have installed with git.

Don’t worry too much about these commands. We’re really just concerned with whether or not you’re in the right place to start working with git.

Forking a Repository

Now that we’ve gotten our client installed, we need to grab the repository that we want to make our changes on. We’ll use the repository for the other GitHub tutorial for this example.

Fork Globally…

The first step is to fork the repository from SparkFun’s account to your own. While there are other ways to take a copy of the repository and work with it, forking is the preferred method, because it makes it easier to submit pull requests (which notify the original owner that you have changes that you’d like considered for inclusion) later.

Once you’re on the repository page, look for the “Fork this repo” button. You can see it in the image above; forking is core functionality of GitHub, so, even if the interface has changed slightly, you should be able to find it. Click the button and you’ll see something like this:

Looks just like the old version, no? If you’ll note, though, the account name is different–now it’s in my account rather than the SparkFun account.

…Code Locally

Now we need to make a local copy. While you can edit simple text files directly on GitHub, it’s reasonable to assume most of your work will be done on a local terminal. Pick a location to store the files that you’ll be working with (I store mine in a folder called “Projects” in my DropBox folder; git plays very well with DropBox!). Here’s a sequence of commands to get those files from GitHub to your local computer and store them in a directory of your choosing:

Let’s take the commands here one at a time:

  • cd projects - change directory to “projects”. This is where I keep, well, my projects.
  • git clone https://github.com/mhord/Github_Tutorial.git - Tell git you want to clone the repository located at this address. The address is case sensitive and git will choke if it’s not exactly right. Cloning creates a copy of the repository, complete with push and pull links back to the original on GitHub. We’ll talk about what this means in a bit.
  • cd Github_Tutorial/ - change directory to “Github_Tutorial”. You’ll need to be in this directory whenever you’re working with this repository.
  • ls - list the files in the directory.
  • ls -a - list all the files currently in the directory. Files and folders prepended with a ‘.’ will not normally show up when you type “ls”, so you need the -a switch to see them.

The .git directory contains all the “under-the-hood” stuff git needs to work its magic. Never, ever mess with it. .gitattributes contains some bookkeeping information telling git how to deal with certain file types; you don’t really need to worry about it now, as it’s not something you’ll be modifying and is not found in all repositories. .gitignore is a text file telling git which files it should skip when committing changes. A good example of files that you might want to exclude are .o files when building a software project, or the .b#x and .s#x files that EAGLE creates as back-ups when you’re editing board layouts or schematics.

Other files should be pretty self-explanatory–they’re the “meat”, the stuff you’re really interested in editing.

Committing, Pushing and Pulling

Now that you’ve got a local copy and a copy on your GitHub account, there are four things that you’ll need to know how to do in order to collaborate with SparkFun:

  • Commit - committing is the process which records changes in the repository. Think of it as a snapshot of the current status of the project. Commits are done locally.
  • Push - pushing sends the recent commit history from your local repository up to GitHub. If you’re the only one working on a repository, pushing is fairly simple. If there are others accessing the repository, you may need to pull before you can push.
  • Pull - a pull grabs any changes from the GitHub repository and merges them into your local repository.
  • Sync - syncing is like pulling, but instead of connecting to your GitHub copy of the forked repo, it goes back to the original repository and brings in any changes. Once you’ve synced your repository, you need to push those changes back to your GitHub account.

Committing

The first step in getting a change back to us is to commit it to your local repository. Look at this sequence of commands:

Again, one command at a time:

  • git status - checks the current status of the repository. This tells you a lot of things; at the moment, it’s telling you that you don’t have any uncommitted changes in your repository.
  • touch README.md - create a README file. The .md suffix is indicative that the file will be formatted in Markdown, which is widely used for formatting on the GitHub website.
  • git status - same command, second time. This time, you can see that there’s a new file, but it’s “untracked”. That means it won’t be included in the commit. You’ll need to add it, first.

[图片上传失败...(image-fdadd5-1545479999801)]

  • git add . - add all untracked files to the repository (at least, those untracked files which are not omitted by the .gitignore file). You can, of course, add files individually by replacing the . with the file name.
  • git status - now the README.md file is showing as a new file to be committed.
  • git commit -m "Add README.md" - commit the recent changes. The -m switch allows you to enter a short double-quoted commit message. Omitting that switch will cause git to open a text editor so you can enter a longer message; save and close the text editor when you’re done.

Pushing

Pushing sends your changes up to GitHub, so they can be shared with the rest of the world. It also serves as a hedge against data loss.

  • git status - our status tells us we have no local changes pending, but that the remote is one commit behind. This isn’t actually checking against the remote repository, just letting us know how many times we’ve committed since our last push.
  • git remote -v - check out where our remote target is. We’ll revisit this in a bit, when we start working on syncing to the original repository.
  • git push -u origin master - push the changes in branch master (we’re going to skip branches, in order to keep this tutorial kind of easy) to remote location origin, and remember the settings (-u).

Pulling

Pulling is the opposite of pushing–it retrieves changes from the remote location and applies them to your local repository. You probably won’t do it that much; it’s more useful in a group environment where more than one person is submitting changes to a single repository.

Visit the repository page again; you’ll see that, since we pushed it back to GitHub, the README.md file is now in the file list. We can change the text in that file right on GitHub, then pull the change back down to our local repository. Start by clicking the file name link. That’ll give you this page:

Since we didn’t put anything in the file after we created it, there’s nothing on the page. You can add something to the file by clicking the “Edit” button:

At the bottom of the page, you’ll find a commit window which lets you put in a description, an extended description, and commit the changes.

Now that you’ve made a change on the server which is not reflected in your local repository, we’ll need to pull it down.

  • git status - note that even though we know there’s a difference on the server, git status does not reflect that, because it only gives us status on the local repository.
  • git pull -u origin master - the syntax is the same as push, but the changes go in the other direction.

At this point, our local repository is in sync with our GitHub repository. But what about the original repository? What if something has changed there, and we want to bring those changes into our copy (both local and on GitHub)?

Syncing with the Original Repository

Syncing is the process of getting changes from the original remote repository, the one you forked from in the first place, and bringing them into your local repository so that you can ensure that you’re making changes on the most up-to-date version of the original. Since you don’t have access to change the directory on the SparkFun GitHub page, like I do, you’ll have to just follow along–without any changes to the original repository, a sync is meaningless.

I made a small change to one of the files in the repository, so now we’ve got a fairly common scenario: most of the repository matches, but I have a new file I want to send to SparkFun, and SparkFun has some changes that I need to include.

Adding the Remote Repository

Before you can pull it to your local repository, you need to add the original repository’s information to your local repository. Here’s how to do that:

  • git remote add upstream https://github.com/sparkfun/Github_Tutorial.git - add a remote endpoint to grab code differences from.
  • git remote -v - you can see here that we now have two different remotes to interface: our personal copy and SparkFun’s copy. This will allow us to keep our copy up-to-date with SparkFun’s in much the same way as we do with our own remote repository.

  • git fetch upstream - fetch works like pull, except it creates a new branch for the incoming data rather than attempting to merge it immediately. This is a “safe” operation; you’ll need to manually merge the incoming data.
  • git branch -va - the -va switch displays all local and remote branches. A branch is just what it sounds like–the change log branches off from the original, and changes to the branch are tracked independently of changes to other branches.

  • git merge upstream/master - merge attempts to reconcile differences between two branches, bringing the changes in the branch named (in this case “upstream/master”) into the currently active branch (since we haven’t changed it, “master”). For most simple changes, merge will complete automatically. If it doesn’t, this website has information about integrating an external merge tool with git, which is probably a good idea, since git’s internal merge tool leaves a lot to be desired.
  • git status - we’re ahead of our remote again, since we pulled changes in from the master repository to our local repository. That means we need to…
  • git push - now our GitHub repository is up-to-date with the latest changes in the SparkFun master copy. But what about the changes we made? How do we get them to the SparkFun repository?

At this point, you have all the tools and knowledge you need to fork a repository, make and commit changes to its contents, keep it up-to-date with SparkFun’s master copy, and push it to GitHub. Only one thing remains: submitting a pull request, to ask SparkFun to include your changes in our master repository.

Pull Requests

The term “pull request” is a little confusing to some people. After all, aren’t I asking to push files to your repository? In fact, no, you’re requesting the master repository’s owner to pull files from your repository. While the difference seems academic, it’s a good one to remember: you are actively requesting someone else to add something to their workflow, so don’t expect an instant response.

Pull Request Etiquette

As I mentioned above, a pull request, even a very important and well-structured one, is asking someone else to do work. To make it more likely that your pull request will be honored, here are some things you can do to make that work as easy as possible for the person at the receiving end:

  • Keep it simple: Don’t rewrite a library completely and expect us to accept the pull request. We simply don’t have time to validate something that large.
  • Keep it recent: Don’t submit a pull request if your repository is wildly out-of-date. If we have to muck through dozens of unrelated changes caused by age to figure out which change you’re submitting, we’ll probably reject the pull request.
  • Don’t be offended: If we don’t accept your pull request, please don’t take it personally. We’ll let you know why we rejected it, and we may make the suggested changes anyway, if it’s easier for us to include them manually than by accepting the pull request.

Submitting a Pull Request

We’re in a good position now to walk through a pull request: we’ve got changes in our repository that we want to send the SparkFun repository.

[[图片上传失败...(image-1ea358-1545479999801)]

](https://cdn.sparkfun.com/assets/b/e/7/3/5/51e6afb0ce395fec6f000000.png)

Click the “Pull request” button. Again, the interface may have changed, but the button will still be there, somewhere.

Click the “Click to create pull request for this comparison” button. That’ll bring up this form:

Put in a title, and a description. Be thorough. Your chances of having your pull request accepted are greatly increased if the icon in the lower right indicates that the pull can be automatically merged.

Click the “Send pull request” button to submit your pull request. You’re done! All that’s left is for us to accept (or deny) the pull request.

Thanks for taking the time to contribute!

Resources and Going Further

Git and GitHub are incredibly powerful tools which are gaining widespread momentum in the open source hardware community. If you’re interested in learning more about them, here are some resources you can turn to:

https://learn.sparkfun.com/tutorials/using-github-to-share-with-sparkfun?_ga=2.179030951.2069146407.1545478899-677141777.1430836854

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,311评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,339评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,671评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,252评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,253评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,031评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,340评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,973评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,466评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,937评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,039评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,701评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,254评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,259评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,497评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,786评论 2 345

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,292评论 0 10
  • 小馒头是个年仅9个月婴儿,是我四十出头侄女所生的二胎小孩。他个头不大,比起其他胖小子他只能算个小个子。他最大的特点...
    叶片儿阅读 582评论 2 1
  • 在生命的旅途中,每个人都有这样的经历吧!晴空万里之后,骤然来了一阵狂风暴雨,但骤雨不终朝,因此一切又回复了最初的样...
    止于丘隅阅读 317评论 5 2
  • 1. 了解序列化的意义 Java 平台允许我们在内存中创建可复用的 Java 对象,但一般情况下,只有当JVM处于...
    Mrsunup阅读 1,110评论 0 0
  • 他知道一个简朴的道理,四个字--物极必反。 素食主义者, 禅宗信仰者, 鲍勃迪伦的爱好者, 极简主义不是衣衫褴褛,...
    EMH阅读 1,731评论 0 0