Important: Git allows users to revert the last commit. However, other developers could have already retrieved the updated project, and deleting updates from one system could cause conflicts for other team members.
Using GitHub
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.
There’s a couple improvements that could be made to this code (analogRead returns an int not a byte!). If the code was just a file on someone’s website you’d have to send them an email and suggest the improvements. This is a bit tedious, and when a project gets longer than a few lines of code, email is not a viable way to collaborate on projects. GitHub allows one person to manage their own projects (also called revision or version control) and it also allows lots of people to work together on large projects (source code management).
What is this git thing?
Git is a software management tool designed for extremely large coding projects (such as Linux). Because the majority of work that we do at SparkFun is on smaller projects, we use only a fraction of its capabilities. While Git uses a command line interface, GitHub was created to give Git a slicker looking web interface. Furthermore, GitHub released a GitHub Desktop GUI for Windows (graphical user interface) that makes moving repos around even easier.
Download ZIP
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.
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.
What are the Benefits of GitHub?
Why use GitHub? There are a number of reasons.. The first is that it enables slick and easy collaboration and version control. This allows you to work on code with anyone from anywhere. Additionally, many employers use GitHub. So, if you plan on getting a job, you’ll look really good if you already know your way around GitHub. And don’t forget about the connections, learning, and portfolio aspects. GitHub is a robust learning and collaboration platform. Take time to explore it and see just how much it can expand your programming knowledge.
1. Sign up for GitHub
In order to use GitHub, you’ll need a GitHub account. You can create a free GitHub account here and start using GitHub right away. With a free account, you’ll get access to unlimited public and private repositories. You’ll also get bug tracking and project management features. The only downside is that you’ll only be allowed three collaborators for private repositories. If you’re still in the “GitHub for beginners” stage, signing up for a free account is a great place to start.
2. Install Git
GitHub runs on Git. What is Git? Git is a version-control system created by programming icon, Linus Torvald. He originally created Git to track changes to source code changes as he was developing the Linux operating system. Git helps programmers collaborate, coordinate work, and work together on complex code and development projects. Git tracks changes and helps teams work remotely on intricate programs. Click here to install Git on your computer. Some prefer to make changes using the command line in Git itself. For our purposes, though, we’ll focus on how to do things within GitHub.
3. Create a Repository
To do anything in GitHub, you’ll need to know how to first start a repository. A repository (or repo) is essentially synonymous with the word “project.” Quite simply, a repository stores everything pertinent to a specific project including files, images, spreadsheets, data sets, and videos, often sorted into files. It’s best to include a README file within your repository that has specific information regarding the given project. On GitHub, you can add a README file right when you create a new repository.
To create a new repository, you’ll hit “new repository” in the upper-right-hand corner. You can then name your repository, include a brief description, and check the box that says “initialize this repository with a README.” Finally, you’ll click “create repository.”
4. Create a Branch
Projects are multi-faceted and many program versions are required when you’re building. Branching enables you to edit multiple unique versions of a repository at once. Your repository automatically has a definitive branch called master. You can work on several different branches in order to make edits before eventually committing them to the master branch.
When a new branch is started, it’ll be a copy of the master branch until you edit it to make new changes. A branch typically goes through many steps and approvals before it is ever merged into the master branch. To start a new branch in GitHub, navigate to your new repository, click the dropdown that reads “branch: master,” type a branch name (like README-edits), and then hit “create branch.” Branches are ideal for new features or bug fixes.
5. Create and Commit Changes to a Branch
To make changes to a branch in GitHub, go to the code view for your newly created branch. Click the file you want to change, then hit the pencil icon in the upper right, make any necessary edits, describe your changes by writing a commit message, and then click “commit changes.” Each saved change is called a commit. Every individual commit has its own commit message which gives more details into why a specific change was done. The commit messages give a history of changes and help project contributors understand how the project has changed over time.
6. Open a Pull Request
In order for any branch to be merged into another person’s branch, you must open a pull request. A pull request is GitHub’s way of notifying relevant parties about your request to incorporate changes into their branch. A pull request will show in red and green the differences of the content between branches. You can make a pull request any time you complete a commit. For best results, when sending a pull request, you can use the “@” feature to mention specific people from whom you need feedback.
More About GitHub: The Open Source Community
There’s a lot to explore in GitHub. We’ve only scratched the surface so far, but we hope that brief GitHub tutorial was beneficial. You’ll quickly come to learn that GitHub is much more than just a version-control platform. You’ll find it’s almost a type of social media platform for programmers. The GitHub open source community gives you access to millions of open source projects. There you can get involved with a project or start one of your very own. Working on open source software is an awesome way to learn new skills and collaborate with bright programmers from whom you can learn a lot.
GitHub is a great place to not only expand your programming knowledge, but also showcase your best work. Your GitHub Profile is a great place for recruiters to find you and reach out about potential jobs and projects. Many people strive to gain GitHub followers by writing engaging and helpful blog posts, or even by creating podcasts or YouTube GitHub tutorials. The more followers you have, the more opportunities will be open to you, and the greater ability you’ll have to become a thought leader in the programming space.
She is not entirely convinced about the code, so she clicks on the plus icon that appears when she hovers that line of code, and writes a comment to Harry. Finally, she clicks on the Start a review button.
Step 2: How to address the review and create a code change
Step 3: How to implement the changes
Step 4: How to merge the pull request
If you want to see a graphical representation of the whole process up to this point, you can click on the Insights tab and then on the Network option. You will be able to actually see how the branching and merging were performed.
How to Handle Merge Conflicts in Git
Once the issue is created, Harry decides to start working on it. To do so, he branches out from the develop branch (by selecting that branch on the GitHub interface) a new one called 3-email-form (including the issue number at the front to make it clear how this branch will relate to the issues).
Before Harry could create a new pull request, Hermione decides to build a placeholder for the form on the index.html file on her own. In order to do so, she creates a new branch out of develop called 3-email-form-placeholder .
To work on the index.html file, she uses the GitHub online code editor (basically, a VSCode for the web). In order to open it, she just presses the . key on her keyboard and the GitHub page is transformed into a VSCode interface (like magic 😉).
Harry supposes that his branch is no longer reflecting the state of the develop branch and that the develop branch has changed because someone else merged changes affecting the index.html file on which he was working on. Nevertheless, he proceeds to create a pull request.
Conflicts ofter arise when teammates are working on different branches that affect a common file. A great way to prevent merge conflicts is to do a pull request on the develop branch, merge that updated develop branch into the branch you are working on, and then do a push followed by a pull request .
Step 14: Git Merge
After merging two branches, Git updates the current branch to reflect the merge, but the target branch isn’t affected. That means you have to use the git branch -d command to delete the obsolete target branch.
1. Run the git status command to ensure that HEAD is pointing to the correct merge-receiving (master) branch. If it is not, run git checkout master to switch to the master branch.
Step 15: Resolve Merge Conflicts
Create a pull request (PR) to inform a repository owner that they should review the changes you’ve made to their code. Then the owner can approve the pull request and merge the changes into the main repository.
7. Log in to your GitHub page. There is now a Create pull request option in your repository with the branch name we created in the command line. Click the Compare & pull request button.
You can accept the changes in the Pull requests tab on GitHub. When you merge the branches, delete the obsolete branch by clicking Delete branch to keep the repository clean.
References:
https://learn.sparkfun.com/tutorials/using-github/all
https://devmountain.com/blog/what-is-github-and-how-do-you-use-it/
https://www.freecodecamp.org/news/how-to-use-git-and-github-in-a-team-like-a-pro/
https://phoenixnap.com/kb/how-to-use-git