您好,欢迎来到知库网。
搜索
您的当前位置:首页Using GitHub

Using GitHub

来源:知库网

What's a Repo?

Repo is short for repository. Think of a repo as a folder of files and all the changes made to the files are recorded. If there’s ever a problem with a file you can go back in time to figure out what changes you made. The most common use for repos are for managing large code projects but repo tracking is good for a variety of applications in the hardware world including PCB layouts, firmware, datasheets and documentation.

For example, let us imagine someone has created an Arduino sketch to demonstrate how to read an analog sensor.

<pre style="box-sizing: border-box; overflow: auto; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 13px; display: block; padding: 6px 10px; margin: 0px 0px 20px; line-height: 19px; word-break: break-all; overflow-wrap: break-word; color: rgb(51, 51, 51); background: rgb(248, 248, 248); border: 1px solid rgb(204, 204, 204); border-radius: 3px; max-height: 350px;">COPY CODE byte myValue = 0; myValue = analogRead(A0); </pre>

What is this git thing?

We’re going to cover a few things in this tutorial:

  • Download ZIP - How to get something from GitHub
  • Manage - How to manage your own stuff on GitHub
  • Pull Requests - How to improve something on GitHub
  • Wiki and Issues - There is lots more on GitHub including Wikis and Issue Tracking

Download ZIP

Just need to get some code from a public GitHub project? Here’s how to get something from GitHub:

On every project there’s an easy to use ‘Download ZIP’ button that will give you the entire contents of the project. This is useful if you just need to grab and go (you leech you). However, this is not the correct way if you plan to contribute back.

Right-clicking won’t work

Note: If you’re navigating around a project and see a file you’d like to grab, right clicking and selecting save-as will not get you the file. You will get an HTML file instead of the raw file you might be expecting. You should either use the ZIP download button or clone the repo to a local folder. Keep reading! We’ll show you how.

Managing Repos

During installation, it will ask you for things such as your login information, name and email address. All of this information will be associated with the commits you make.

During installation, it may ask you to scan for repos on your computer. I recommend you don’t do this. The scan can take a long time and because you’re reading this tutorial, you probably won’t have any repos.

Once you are logged in to GitHub, click on the ‘Fork’ button.

We have now created a ‘fork’ or a copy of this repo and is located within your GitHub account. Note the words in the upper left corner of the window “chipaddict/Github_Tutorial” and the words underneath “forked from sparkfun/Github_Tutorial”. This shows that you have this project on your account (your account name will be different).

Now you can make lots of changes to this repo without affecting the original project. This is helpful if someone has some example code that is close to what you need but needs lots of modifications for your plans.

Now that you have your own copy of the project online, click on the ‘Clone or Download' button then the ‘Open in Desktop’ button. If you are not logged in, it may take you to the Github Windows page.

Windows may ask you for permission to allow the link to launch and use the Github software. This is ok. The GitHub GUI will open and a download will begin.

Because we just forked this repo there are no changes. But if you click on any of the dots at the top we can go back in time and see what’s changed over time. You can see the file that was changed and what was added (in green) to the file.

Hey thanks Waleed! He suggested a change to the README and added a link back to this tutorial. Smart.

Even further back in time we can see there was a comment ‘Corrected a typo in print statement’ and below that we can see the lines of code that were altered - red was removed, green was added. We can do lots of fun stuff like ‘revert commit’ and ‘roll back this commit’ but for now, let’s see how revision tracking works.

Click on the gear in the upper right corner and selection ‘options’.

You should now know where your repos are stored. Navigate to that directory and open up the Github_Tutorial.ino file.

From the Arduino IDE or Windows Notepad let’s correct the variable declaration from byte to an int. Save the modification and return to the GitHub GUI.

GitHub has noticed that a file has changed! You should see a dot on the changes button:

You will see the main screen change as well showing the changes that git has detected:

Wow! The small change we made is now nicely highlighted.

Now let’s talk about how repos work. You have a local working copy, a local repo, and a global repo.

Local working copy: You generally write code, layout PCBs, and hack on documentation on your local computer on a local copy. Throughout the day you would use the GitHub window to ‘commit’ these changes to the a local repo. The changes you’ve made throughout the day are not known to the world, only to your local repo on your local computer.

Local repo: Now let’s commit the change we’ve made to our local repo. We must comment about what we changed before we can commit it. Thoroughly describe what you did and then hit ‘Commit to master’.

As a general rule, try to commit small changes, frequently. If you wait 4 months between commits it is going to be very difficult for you to remember why you changed 5 lines of a subfunction.

After pressing commit the Sync button appears.

We now have no uncommitted changes but we do have unsynced commits. This means we’ve committed the changes to our local repo but we have not yet pushed (synchronized) the changes to our global repo.

Global repo: Press ‘Sync’ This will push the changes that we made within our local repo up to GitHub and to our account.

Nice job Bob! We have successfully pushed this corrected code up to the global repo on GitHub. Now go online and look at your repo.

We can see the notes from the commit we made as well as the status of this fork vs the original sparkfun repo: “This branch is 1 commit ahead of sparkfun:master”.

The GitHub web interface is similar to the Windows GUI but adds many advanced options. Use the web for changing properties of the project; use the GUI for routine commits to the local repo and global syncs.

Beyond Free

That’s it for this section. These steps of forking a repo or creating your repo should allow you to create code projects, PCB layouts (we push Eagle files up to GitHub all the time), documents, images and even binary files.

In this section we found a bug and corrected it, but we have not yet let the original project know about the error. The next section will cover how to send corrections and improvements back to the original project through pull-requests.

Pull Requests

Repositories are great for managing and tracking changes made to code over time. But the real power comes into play when you collaborate with multiple people on a project. When people have multiple improvements, how do we combine them? Pull requests allow contributors to give back to the main project.

We’ve made some improvements to our version of the Github_Tutorial project. Now let’s click on the Pull Request button to let the owner of the project know about the improvements we’ve made.

Here’s where we describe the changes that we’ve made so that the owner of the main project (SparkFun is the owner in this example) knows what to expect.

As with most comments, be as verbose as possible. The changes you’ve made are obvious to you but to a project owner with thousands of lines of codes and dozens of pull requests it can become confusing.

Once you’ve written a note about the changes you’re proposing click on ‘Create pull request’.

Once we’ve sent the pull request, the owner of the main project is notified. Please don’t hesitate to send a pull request to SparkFun for this tutorial. We’d love to hear from you!

This is where the owner of the project can review the submitted code (sometimes called a patch). GitHub provides a great discussion system so that the patch can be discussed. You can even comment on individual lines of code.

Here’s what the pull request looks like from the owner’s point of view. The owner has the option to merge this pull request or discuss it.

In general, create pull requests that are smaller and more simple in nature. This will make it easier for the project owner to wrap their head around. It’s easier to accept pull requests that contain 5 or 10 changes, but a monumental task if you’ve completely rewritten 400 lines of code.

Wiki and Issues

There are some additional tools built into GitHub as well. Issue Tracking allows folks to post problems or issues with a given project. It’s kind of like a ticketing system or tech support but with the ability to comment on a specific line of code.

Try using Git and GitHub for your next project. There’s an undeniable learning curve but it will make it much easier to collaborate with people.

Resources and Going Further

Now that you’ve got repos under control we recommend you check out these tutorials:

  • Ready to up your game? Help us make the world better! Read how to improve our libraries and hardware.
  • Checkout the to see how we set it up. There’s also a separate repo for the .

Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务