What is GitHub and how to use it?
Last updated on
GitHub is a platform owned by Microsoft. It became the place of choice for many developers and companies working on open-source projects. You can host public projects for free on GitHub and enjoy the many great features that will make your coding life easier.
You can also create private repositories with a free account, of course. And you can pay if you need more resources . But Iâve been using GitHub for my open-source projects for years (the SDA and journalism libraries, and this course too ) and I never reached the limits of the free plan!
GitHub works with Git. In How to use Git?, we used Git to track changes to a small program that prints a greeting. Weâll build the same kind of program from scratch here, push it to GitHub, and translate the greeting into French.
GitHub account
To work with GitHub, you first need to create an account . So start with that.

A social network
When you sign in to GitHub, you can switch between Home and Feed. Home is your personal dashboard, where you can find the projects youâre working on.
GitHub is also a social network. In your Feed , you can see what people you follow are working on and catch up on open-source projects that interest you. Hereâs what my feed looked like when I wrote this lesson.

Users have a public profile showing their public activity, and you can follow them to see their activity in your feed. Hereâs my profile, for example . Follow me! đ¤
Personally, I like to follow other computational journalists or maintainers of open-source projects I use. You can update your profile and settings by clicking on the button in the top right corner.

Public repositories also have a public page. When you like a project, you can give it a star. For example, hereâs the page for the Simple Data Analysis library I maintain and weâve been using in previous lessons. Give it a đ!
You can also get notified when a project you use has updates. Open the bell menu to choose which updates you want to hear about. I usually subscribe to new releases for open-source projects I use.

Creating a repository
Create a new folder named my-first-repo and open it in VS Code. Inside that folder, create a file named main.ts, add the following line, and save the file.
console.log("Hello!");Open the VS Code terminal (Command + J on Mac or Control + J on Windows).
If youâre new to Git, install it first. Then run the commands below once to set your name, email address, and default branch. Replace the example name and email with your own details.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainInitialize a Git repository with git init. Then stage main.ts with git add and save it to your Git history with your first commit.
git init
git add main.ts
git commit -m "Add first greeting"
Your greeting is now saved in a commit on your computer. Letâs create a repository on GitHub to push it to.
On GitHub, click the + button in the top-right corner and choose New repository.

Name the repository my-first-repo and make it private so you can practice. Leave the options for adding a README, a .gitignore file, and a license off. That gives you an empty repository to push your existing project to. Click Create repository.

Your repository is now on GitHub. Itâs still empty, so GitHub takes you to a page with instructions for pushing the project from your computer to GitHub.
On the Quick setup page, select HTTPS. Find the block under push an existing repository from the command line and click the copy button in its top-right corner to copy all three commands.

Back in VS Code, open the terminal in your project folder. Paste the commands you copied and press Enter to run them. When the push succeeds, Git confirms that your local main is set up to track origin/main.

To push from your computer, GitHub needs to verify who you are and that your account has permission to send code to the repository you chose. Iâve already signed in on my computer, so you donât see that step in the screenshot.
On your first push, you may be asked to open a link and sign in to GitHub in your browser. Once you sign in, your computer can save a credential so GitHub can recognize your account automatically the next time you push.
If Git asks for a password in the terminal or you canât sign in, follow GitHubâs guide to setting up HTTPS credentials , then retry git push -u origin main.
These three commands pushed your code to GitHub and connected your local main branch to main on GitHub. From now on, on main, you can use git push to send changes from your computer to GitHub and git pull to bring changes from GitHub to your computer.
Refresh your repository page on GitHub. You should see main.ts on main, along with your latest commit.

Click main.ts to see your code and the message from the most recent commit that changed the file.

Pushing to the repository
When you create a new repository, itâs helpful to describe what the project does.
By convention, projects use a file named README.md to introduce the project. As youâll see, GitHub will recognize this file and use it to present your project.
In VS Code, create a file named README.md next to main.ts and add the text below.
# My first repository
Run `node main.ts` to see the greeting in the terminal.The .md extension stands for Markdown, a simple way to format text. For example, the # followed by a space at the start of the first line makes âMy first repositoryâ the title.
To see how the Markdown will look, right-click the README.md editor tab in VS Code and choose Open Preview.
Save the file, then stage, commit, and push it.
git add -A
git commit -m "Describe the project in README.md"
git push
Now if you refresh the repository page on GitHub, youâll see your formatted README below the file list. Youâll also see README.md in the list alongside your other files! đ

Creating issues
Letâs say weâd like our greeting to be in French instead of English. We could change it on the main branch, commit the change, and push it directly to GitHub. That would update main on GitHub. However, itâs often better to take a step back and write down what we want to do before jumping into the code.
On GitHub, you can do this by creating an issue. An issue gives you a place to describe the problem or suggest a change. In many open source projects, users open issues and maintainers discuss them with users before changing the code. This gives everyone a chance to decide whether the change would help and agree on what to do.
Open the Issues tab near the top left of your repository page. You havenât created an issue yet. Click the New issue button on the right to create your very first issue.

Then paste this text into the title field.
Translate greeting to FrenchPaste this text into the description field.
`main.ts` should print `Bonjour!` instead of `Hello!`.
The description uses Markdown, just like your README. I put backticks around main.ts, Bonjour!, and Hello! to format them as inline code. You can click Preview to see how the description will look.
Click Create to save the issue. GitHub gives it a number, such as #1.
You can now see your first GitHub issue!

Creating branches
Letâs say weâre ready to work on the issue we just opened. Itâs a good idea to work on a branch. A separate branch lets us make and test changes while keeping the code on main untouched. Weâll create the branch from the issue so GitHub links them.
Near the bottom of the issueâs right sidebar, find Development and click Create a branch.

GitHub suggests a branch name based on the issue number and title. For issue #1, thatâs 1-translate-greeting-to-french. The dialog also shows which repository will contain the new branch and which branch it will start from. Keep your repository as the destination and main as the source.
Under Whatâs next?, choose Checkout locally so you can work on the branch on your computer. Then click Create branch.

GitHub now shows two commands. Use the copy button, then paste the commands into the VS Code terminal for your project and run them.

The new branch is on GitHub, and your local Git repository hasnât heard about it yet. The first command, git fetch origin, gets the latest commits and branch information from GitHub without changing the files youâre working on. The second command, git checkout followed by the branch name, creates a local copy of that branch and switches you to it so you can start working.
After you run these commands in your projectâs VS Code terminal, look at the bottom left of VS Code. You should see the name of the branch you just created, 1-translate-greeting-to-french.

Now that weâre on the new branch on our computer, we can start making changes. In main.ts, change Hello! to Bonjour! and save the file.
console.log("Bonjour!");Then stage and commit the change, and push it to GitHub.
git add -A
git commit -m "Translate greeting to French"
git pushIn the previous lesson, we used git add to choose which changes to include and git commit to save a snapshot on our computer. Here, git push sends the new commits from our current branch to the matching branch in our GitHub repository. Thatâs how the French greeting gets saved on GitHub.

On GitHub, open your repository and select 1-translate-greeting-to-french in the branch selector. GitHub shows that this branch is one commit ahead of main.

You might have noticed the orange banner saying your branch had recent pushes, with a Compare & pull request button. You can ignore it for now. Weâll come back to it in the next section.
Click main.ts to see the French greeting, Bonjour!, saved on GitHub.

Now use the branch selector to switch back to main. Youâll see that main.ts still has the original English greeting, Hello!.

Branches let you keep several versions of your project on GitHub as you work on different changes. They also let people work in parallel on different issues, features, or bug fixes.
Checking what changed one file at a time can get tedious. When youâre ready to review your changes together, you can open a pull request, as weâll do in the next section.
Creating pull requests
A pull request proposes merging changes from one branch into another. To create one, open the Pull requests tab in your repository and click New pull request on the right.

Clicking Compare & pull request in the orange banner is a shortcut. Bear with me and follow the manual steps below this time. That way, youâll know what the shortcut does the next time you use it.
Keep main as the base branch, which will receive the change. In the compare menu, select 1-translate-greeting-to-french, the branch with your change.

Once you select the branch, GitHub shows the changes in the code. The line with Hello! is marked as removed in red, and the line with Bonjour! is marked as added in green. Click Create pull request to open the form.

The form asks for a title and gives you the option to add a description. If youâre working with a team, you can request a review from a teammate. You can also add labels or link the pull request to a project or milestone.
For this example, just use Translate greeting to French as the title and click Create pull request.

Your pull request is open! The Pull requests tab at the top of the repository lists all the projectâs pull requests. On your pull requestâs page, you can see its number and the commits that will go into main. You can leave comments under Conversation to discuss the changes.

You can see all the commits in the pull request under Commits. Most of the time, youâll want to open Files changed, where you can review the changes across all affected files in one place.

Return to Conversation. GitHub shows that there are no merge conflicts in this example. A conflict can happen when both branches change the same line of the same file in different ways and Git canât decide what to keep. Here, you can click the green Merge pull request button.

GitHub suggests a message for the new merge commit. You can edit the message and its optional extended description, or keep the suggested text. Then click Confirm merge.

GitHub now marks the pull request as Merged and the linked issue as Closed. Click Delete branch to remove the branch from GitHub. You can also set GitHub to delete branches automatically after merging.

Go back to the Code tab. On main, youâll see the new merge commit above the file list.

Open main.ts. The greeting on main now says Bonjour! đ

If you need to undo a merged pull request, GitHubâs Revert button can open a new pull request that reverses the changes.
Back in VS Code, switch to main and pull the changes.
git switch main
git pullOpen main.ts on your computer. It should now contain Bonjour!.

git pull brings changes from GitHub to your computer. Since you merged the pull request on GitHub, you need to pull to update your local main branch.
Congrats! You now know how to open an issue, create a branch linked to it, and open a pull request that connects the two so you can review your changes before merging them into main. đ
Updating .gitignore
Sometimes you want to share part of a project on GitHub and keep certain files on your computer, such as files containing passwords or other credentials, or very large data files.
Thatâs why Git repositories often include a .gitignore file. This is where you list the files and folders you want Git to ignore so they stay out of your commits and off GitHub.
To see how this works, create a file named password.txt in your project folder with some made-up text, such as potato.

In VS Code, password.txt is green with a U next to its name. U stands for untracked. Git sees the new file. To tell Git to ignore it, create a file named .gitignore in your project folder, add password.txt on its own line, and save the file.
password.txt
After a few seconds, password.txt should turn gray in VS Code. Git is now ignoring it. The .gitignore file itself is green with a U because itâs new. Stage, commit, and push .gitignore so Git keeps ignoring password.txt as you work on the project.
git add .gitignore
git commit -m "Ignore password.txt"
git push
Refresh the repository page. You should see .gitignore on GitHub. Your password.txt file stayed on your computer the whole time. Anyone who clones the repository or pulls the latest changes gets the same .gitignore rules.

If you accidentally push sensitive information, follow GitHubâs steps for removing it from a repository .
Cloning repositories
One nice thing about GitHub is that when you push your work to it, itâs saved in the cloud. This means that if you remove a project from your computer or get a new computer, you can get a copy from GitHub. It also makes it easy to check a colleagueâs code on your computer if they ask you for a review or for help. Because the project uses Git, you get a full copy of the repositoryâs history, including its branches.
Creating a copy of a repository on your computer is called cloning.
To clone a repository, open its page on GitHub, click Code, choose your clone options (the defaults are usually fine), and copy the URL.

Then, on your computer, create a new, empty folder with the same name as the repository. Open a terminal in that folder and type git clone, followed by a space, the URL you copied, another space, and a period.
git clone YOUR-REPOSITORY-URL .The period means âthis folder.â Git puts the repositoryâs files and history directly in the folder you created.
Thatâs it. Youâre ready to go!
Forking a repository
If everyone can clone a public repository (or a private one if they were invited to collaborate on it), not everyone is allowed to push to it and merge into it. And this is a good thing! Imagine the mess and the critical security problems! đ
However, if a repository is public, you can fork it. This creates your own copy of the repository in your GitHub account. You can then clone your fork to your computer, add or edit files in your local copy, and push your changes to your fork.
It works kind of like a branch of a repository. There is no impact on the original repository, but if you want, you can create a pull request aimed at the owners of the original repository.
For example, several GitHub users have forked the Simple Data Analysis library . I have no idea what they are doing with it and thatâs okay!
If you are curious, feel free to fork it too!

Forking is how a lot of open-source contributions and collaborations happen:
- Someone creates an open-source project.
- Someone else finds it interesting but wants to tweak or fix something. They fork it, update the code, and send a PR to the owner of the original repository.
- The owner checks the PR and, if happy, merges it.
- The contribution is now part of the project and everyone can benefit from it!
Conclusion
Wow, that was a lot! Knowing how Git and GitHub work helps you stay organized, whether youâre working alone or with others. When you commit and push regularly, you have more saved versions to go back to if you introduce a bug or make a mistake. Think of all the project_final_v1, project_final_v2, and project_final_final files youâve probably made over the years. With Git, you can keep that history in one place.
In this lesson, youâve learned new important Git commands:
git remote add origin URLto connect a local repository to GitHubgit pushto push code to a remote repository saved in the cloudgit fetch originto get commits and branch information from GitHubgit checkout BRANCHto switch to a branch, including one you just fetched from GitHubgit pullto retrieve the latest changes from a remote repositorygit cloneto copy a whole repository onto your computer
You have also learned about GitHub issues, pull requests, forks and .gitignore.
But⌠you might be tempted to keep creating branches locally and merging directly on your computer when working alone. Or even just to work in the main branch! đ¤
And thatâs totally fine. Just find the recipe that works for you. But keep in mind that creating issues and reviewing pull requests are great ways to stay focused and review your own work! Itâs also an opportunity to be comfortable with tools used by many teams and companies around the world.