Skip to Content
4. Git & GitHubHow to use GitHub?

What is GitHub and how to use it?

By Nael Shiab

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.

Want to know when new lessons are available? Subscribe to the newsletter ✉️ and give a ⭐ to the GitHub repository to keep me motivated! Click here to get in touch.

GitHub account

To work with GitHub, you first need to create an account . So start with that.

The GitHub homepage with arrows pointing to the email field and the Sign up button.

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.

GitHub’s Feed page with arrows pointing to its URL, sidebar tab, and heading.

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.

Nael Shiab’s GitHub profile with an arrow pointing to the Follow button.

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.

The Simple Data Analysis repository with arrows pointing to the Notifications and Star buttons.

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.

main.ts
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 main

Initialize 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"

VS Code shows Hello! in main.ts and the terminal confirms the first commit.

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.

Arrows point to the plus button and the New repository option on the GitHub dashboard.

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.

The GitHub form shows my-first-repo set to Private, with no README, .gitignore, or license selected.

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.

Arrows point to HTTPS and the commands under “push an existing repository from the command line” on GitHub’s Quick setup page.

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.

The VS Code terminal shows the three commands from GitHub and confirms that main now tracks 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.

The private my-first-repo repository on GitHub shows main.ts on the main branch and the Add first greeting commit.

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

GitHub shows the Hello! greeting in main.ts, with the Add first greeting commit message above the code.

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.

README.md
# 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

VS Code shows the README.md file, its formatted preview, and a successful git push in the terminal.

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! 🎉

Arrows point to README.md in the file list and to the formatted README below it.

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.

Arrows point to the Issues tab and the New issue button. The list shows zero open and zero closed issues.

Then paste this text into the title field.

Translate greeting to French

Paste this text into the description field.

`main.ts` should print `Bonjour!` instead of `Hello!`.

The new issue form shows the title, the Markdown description, and the Create button.

💡

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!

GitHub shows issue #1, Translate greeting to French, marked Open, with the formatted description below.

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.

An arrow points to Create a branch under Development in the issue’s right sidebar.

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.

The branch creation dialog shows the suggested name, destination repository, main as the source, Checkout locally selected, and the Create branch button.

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 Checkout in your local repository dialog shows git fetch origin, git checkout 1-translate-greeting-to-french, and a copy button.

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.

The terminal confirms the switch to the new branch, and the VS Code status bar shows its name in the bottom left.

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.

main.ts
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 push
💡

In 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.

VS Code shows the French greeting in main.ts and the terminal confirms the push to the issue branch 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.

The GitHub branch selector shows the issue branch, which 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.

GitHub shows Bonjour! in main.ts on the issue branch.

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

GitHub shows Hello! in main.ts on the main branch.

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.

Arrows point to the Pull requests tab and the New pull request button on GitHub.

💡

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.

The compare menu is open with 1-translate-greeting-to-french available to select and main set as the base.

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.

GitHub shows one commit, the greeting change in main.ts, and the Create pull request button.

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.

The pull request form shows the title Translate greeting to French, an empty description, and the Create pull request button.

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.

The open pull request shows its title and number, the linked issue, the Merge pull request button, and the comment box.

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.

The Files changed tab shows Hello! in red on the left and Bonjour! in green on the right in main.ts.

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.

Arrows point to the Conversation tab, the message confirming there are no conflicts, and 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.

The merge form shows the suggested commit message, the extended description, and the Confirm merge button.

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.

The pull request is marked Merged, the linked issue is marked Closed, and GitHub offers to delete the branch.

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

The Code tab on main shows the new merge commit above README.md and main.ts.

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

On the main branch, main.ts now contains a console.log that prints 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 pull

Open main.ts on your computer. It should now contain Bonjour!.

In VS Code, the terminal shows git switch main and git pull, and main.ts now says Bonjour! on the main branch.

💡

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.

VS Code shows the new password.txt file containing potato and marked U for untracked.

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

In VS Code, .gitignore lists password.txt, which is now dimmed in the Explorer.

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

The VS Code terminal shows .gitignore being committed and pushed to GitHub. The ignored password.txt file is still visible in the Explorer.

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.

The GitHub repository shows the Ignore password.txt commit and .gitignore in the file list.

💡

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.

Arrows point to the Code button, the HTTPS option, and the copy button next to the clone 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!

An arrow points to the Fork button on the Simple Data Analysis repository page.

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 URL to connect a local repository to GitHub
  • git push to push code to a remote repository saved in the cloud
  • git fetch origin to get commits and branch information from GitHub
  • git checkout BRANCH to switch to a branch, including one you just fetched from GitHub
  • git pull to retrieve the latest changes from a remote repository
  • git clone to 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.

Enjoyed this? Want to know when new lessons are available? Subscribe to the newsletter ✉️ and give a ⭐ to the GitHub repository to keep me motivated! Get in touch if you have any questions.